#!/usr/local/bin/perl
# A perl script
#
# $Date: 1996/07/22 14:40:26 $
# $Revision: 1.1 $
# (C) by Valentino Kyriakides
#
# Unformat 'espretty' ANSI, HTML, LaTeX, MIME, MIF and RTF class format files
# back into an pure ASCII class representation!
#
# NOTE: The regular espression substitutions herein used, are based *highly*
#       on 'espretty's' output formaters codings. So if you make any changes 
#       to 'espretty's' format classes string outputs, you must also do the 
#       appropriate changes to the below 'perl' substitutions!!!

#
# Initial settings
#
require "getopts.pl";
&Getopts ("f:");
$prog = $0;
@fmts = ("ansi","html","latex","mif","mime","rtf");

#
# Test for correct command line '-f' and formater options
#
if (!$opt_f) {
	die ("Usage:  $prog -f [ansi | html | latex | mime | mif | rtf] <file...> \n \
unformat 'espretty' formated <files...> back into '*.e' class files!\n\n");
} else {
	$format = $opt_f;
}
$i = 0;                          # array index variable
$correct = "no";
while ($correct eq "no") {       # for checking format options
	if ($fmts[$i] eq $format) {   # found?
		$correct = "yes";          # yes
	} elsif ($i < 7) {            # more formats to look at?
		$i = $i + 1;
	} else {                      # no more formats, format not found
		die ("valid formats are: ansi | html | latex | mime | mif | rtf \n");
	}
}
&main_start;                     # call the 'main' subroutine


#
# The main file processing subroutine
#
sub main_start
{
	foreach $file (@ARGV) {
		unless (-T $file) {
			warn "Can't process binary file: $file\n";
			next;
		}
		$dummy = $file;
		@fpart = split(/\./,$dummy);
		$outfile = $fpart[0];
		$outfile = "$outfile.e";
		
		open(OUT,">$outfile") || do { warn  "Can't create $outfile!\n"; next; };
		open(FILE, $file) || do { warn "Can't open $file: $!\n"; next; };
		while (<FILE>) {
			if ($format eq "ansi") { 
				&ansi_file;    
			}
			elsif ($format eq "html") { 
				&html_file;
			}
			elsif ($format eq "latex") { 
				&latex_file;
			} 
			elsif ($format eq "mif") { 
				&mif_file;
			}
			elsif ($format eq "mime") { 
				&mime_file;
			}
			else {        
				&rtf_file;
			}
			print OUT $_;
		}
		close(FILE);
		close(OUT);
	}
}


#
# format processing subroutines start here!
#
# NOTE: These regular espression substitutions are based *highly* on
#       'espretty's' output formaters coding. So if you make changes 
#       to 'espretty's' format classes string outputs, you must also  
#       do the appropriate changes to the below 'perl' substitutions!
#


#
# Remove espretty's 'latex' tags via Regular expression substitutions!
#
sub latex_file
{
	s/(% Produced by espretty for SmallEiffel)\n//g;
	s/^\\documentstyle\{article\}\n//g;
	s/(\\oddsidemargin1.5cm)\n//g;
	s/(\\evensidemargin1.5cm)\n//g;
	s/(\\textwidth16cm)\n//g;
	s/(\\begin\{document\})\n//g;
	s/\\frenchspacing\n//g;
	s/(\\def\\~\{\\hspace\*\{1ex\}\})\n//g;
	s/(\\def\\\|\{\$\|\$\})\n//g;
	s/(\\def\\<\{\$<\$\})\n//g;
	s/(\\def\\>\{\$>\$\})\n//g;
	s/(\\hspace\{-4ex\})\n//g;
	s/(^\\end{document}\n)//g;
	s/(\\~)/ /g;
	s/(-\\,)/-/g;
	s/(\\\{)/{/g;
	s/(\\\})/}/g;
	s/(\\$)/\$/g;
	s/\\&/&/g;
	s/\\#/#/g;
	s/\\%/%/g;
	s/\\_/_/g;
	s/\\^/^/g;
	s/\\\\//g;
	s/\\\|/|/g;
	s/\\</</g;
	s/\\>/>/g;
	s/\{\\bf //g;
	s/\{\\em //g;
	s/\}/~/g;
	s/(\{.*)~/$+/g;
	s/~//g;
}

#
# Remove espretty's 'html' tags via Regular expression substitutions!
#
sub html_file
{
	s/<HTML>\n//g;
	s/<HEAD>\n//g;
	s/<TITLE>Produced by espretty for SmallEiffel<\/TITLE>\n//g;
	s/<\/HEAD>\n//g;
	s/<BODY>\n//g;
	s/<CODE>\n//g;
	s/<\/CODE>\n//g;
	s/<\/BODY>\n//g;
	s/<\/HTML>\n//g;
	s/&nbsp;/ /g;
	s/<B>//g;
	s/<\/B>//g;
	s/<I>//g;
	s/<\/I>//g;
	s/<BR>//g;
}

#
# Remove espretty's 'mime' tags via Regular expression substitutions!
#
sub mime_file
{
	s/<nl>//g;
	s/<bold>//g;
	s/<\/bold>//g;
	s/<italic>//g;
	s/<\/italic>//g;
	s/<lt>/</g;
}

#
# Remove espretty's 'ansi' tags via Regular expression substitutions!
#
sub ansi_file
{
	s/\c[\[1m//g;
	s/\c[\[0m//g;
}

#
# Remove espretty's 'rtf' tags via Regular expression substitutions!
#
sub rtf_file
{
	s/\{\\rtf0\\ansi\\deff0\{\\fonttbl\\f0\\froman Times-Roman;\}\n//g;
	s/\{\\colortbl \\red0\\green0\\blue0;\}\n//g;
	s/\{\\stylesheet\{\\fs24 \\snext0 Normal;\}\}\n//g;
	s/\\paperw.+\\gutter0\\widowctrl\n//g;
	s/\\sectd\\f0\n//g;
	s/\\sect\n//g;
	s/\}rtf//g;
	s/\\~/ /g;
	s/\\_/-/g;
	s/\\par //g;
	s/\\b //g;
	s/\\b0 //g;
	s/\\i //g;
	s/\\i0 //g;
	s/\\\\//g;
	s/(\\\{)/{/g;
	s/(\\\})/}/g;
}

#
# Remove espretty's 'mif' tags via Regular expression substitutions!
#
sub mif_file
{
	s/<MIFFile 3.00> # Generated by spretty v1.1\n//g;
	s/<Para\n//g;
	s/  <PgfTag `EProgList'>\n//g;
	s/> # end of Para\n//g;
	s/# End of MIFFile//g;
	s/    <Font \n//g; 
	s/      <FTag `Bold'>\n//g;
	s/      <FFamily `Times'>\n//g;
	s/      <FVar `Regular'>\n//g;
	s/      <FWeight `Bold'>\n//g;
	s/      <FAngle `Regular'>\n//g;
	s/      <FSize  12.0 pt>\n//g;
	s/      <FPairKern Yes >\n//g;
	s/      <FTag `Emphasis'>\n//g;
	s/      <FTag `'>\n//g;
	s/      <FAngle `Regular'>\n//g;
	s/      <FWeight `Regular'>\n//g;
	s/      <FAngle `Italic'>\n//g;
	s/    > # end of Font\n//g;
	s/    <String `//g;
	s/'># end\n//g;
	s/    <Char HardSpace >\n/ /g;
	s/    <Char HardReturn >//g;
	s/  <ParaLine\n//g;
	s/  > # end of ParaLine\n//g;
	s/\\\\/\\/g;
	s/\\>/>/g;
	s/\\xd5/'/g;
	s/\\xd4/`/g;
}
