#!/bin/perl -w
##!/Utils/bin/perl5
#
# Usage: vimhtml2latex  < [infile]  > [outfile]
#
# Version 1.1, 30 mar 2001, by B.W. van Schooten
#
# Converts Vim's generated HTML syntax colouring to Latex.  The Latex
# generated may be included in a Latex document using \input{filename}. You
# do need to define the colours, for example by including the following bit
# of Latex at the top of your document:
#
# \definecolor{shwhite}{rgb}{1,1,1}
# \definecolor{shgreen}{rgb}{.4,1,.4}
# \definecolor{shmagenta}{rgb}{1,.4,1}
# \definecolor{shlightbrown}{rgb}{1,.6,0}
# \definecolor{shcyan}{rgb}{.25,1,1}
# \definecolor{shlightblue}{rgb}{.5,.7,1}
# \definecolor{shyellow}{rgb}{1,1,.25}
# \definecolor{shlightred}{rgb}{1,.7,.7}
#
# It assumes a dark background.  The default colour is white, the other
# colours are given in the table below, and should be modified if you're using
# different colours than the usual Vim colours.
#
# It assumes the HTML to be converted is enclosed between <PRE> and </PRE>
# tags, placed on separate lines.
#
# It assumes a tabsize of 4 spaces.

#<FONT COLOR=#60ff60> col2
#<FONT COLOR=#ff80ff> col1
#<FONT COLOR=#ffa500> col4
#<FONT COLOR=#40ffff> col3
#<FONT COLOR=#80a0ff> col0
#<FONT COLOR=#ffff60> col6
#<FONT COLOR=#ffa0a0> col5

%colors=(
	"60ff60" => "shgreen",
	"ff80ff" => "shmagenta",
	"ff00ff" => "shmagenta",
	"ffa500" => "shlightbrown",
	"40ffff" => "shcyan",
	"80a0ff" => "shlightblue",
	"ffff60" => "shyellow",
	"ffa0a0" => "shlightred",
);

$nextcol=0;


#print "\\documentclass{article}\n";
#print "\\usepackage{color}\n";
#print "\\definecolor{lightbrown}{rgb}{1,0.7,0}\n";
#print "\\definecolor{lightred}{rgb}{1,0.7,0.7}\n";


#print "\\begin{document}\n";

print "\\color{shwhite}\n";
#print "\\pagecolor{black}\n";

#print "\\tiny\n";

print "\\begin{tabbing}\n";
print"xxxx\\=xxxx\\=xxxx\\=xxxx\\=xxxx\\=xxxx\\=xxxx\\=xxxx\\=xxxx\\=xxxx\\=xxxx\\=\\kill\n";

$in_body=0;

while (<>) {
	if (/\<\s*\/PRE/i) { $in_body=0; }
	if ($in_body != 0) {
		# take care of indenting: convert spaces or tabs found at the beginning
		# to `\>'s
		while (s/^\t// || s/^\s\s\s\s//) {
			print "\\>";
		}
		# parse
		@tok=split /(\<[^>]*\>)/;
		foreach $t (@tok) {
			if ($t =~ /\<[^>]*\>/) {
				if ($t =~ /\<font\s+color=#([0-9a-f]*)/i) {
					if (not defined $colors{$1}) {
						$colors{$1} = "col$nextcol";
						$nextcol++;
					}
					$colid = $colors{$1};
					# extra spaces emulate HTML spacing around style changes
					print "\{\\color\{$colid\}";
				} elsif ($t =~ /\<\/font>/i) {
					print "\}";
				} elsif ($t =~ /\<b\>/i) {
					# extra spaces emulate HTML spacing around style changes
					print "\{\\bf";
				} elsif ($t =~ /\<\/b\>/i) {
					print "\}";
				}
			} else {
				$t =~ s/\n//g;
				$t =~ s/ /\\ /g;
				$t =~ s/\\/\\verb+\+/g;
				#$t =~ s/\t/\\>/g;
				$t =~ s/{/\\{/g;
				$t =~ s/}/\\}/g;
				$t =~ s/_/\\_/g;
				$t =~ s/#/\\#/g;
				$t =~ s/\$/\\\$/g;
				$t =~ s/\%/\\\%/g;
				$t =~ s/\^/\\verb+^+/g;
				$t =~ s/&quot;/"/g;
				$t =~ s/&amp;/\\&/g;
				$t =~ s/&lt;/\</g;
				$t =~ s/&gt;/\>/g;
				print $t;
			}
			#print $t."\n";
		}
		print "\\\\\n";
	}
	if (/\<\s*PRE/i) { $in_body=1; }
}

print "\\end{tabbing}\n";
#print "\\end{document}\n";


