#!/usr/bin/perl
######################################################################
#                                                                    #
# Code2HTML                                                          #
# ---------                                                          #
#                                                                    #
# written 1999 by Palfrader Peter palfrader@writeme.com              #
#                                                                    #
# Version 0.1                                                        #
#                                                                    #
# Do not distribute!                                                 #
#                                                                    #
# The final version will be distributed under the GPL                #
#                                                                    #
######################################################################

	$langmodename = "c++";

	open FILEHANDLE, "</home/weasel/code2html.config";

	while (<FILEHANDLE>)
	{
		$_ =~ s/\n//g;
		$_ =~ s/^[ \t]*(.*)[ \t]*/$1/; #remove beginning and trailing spaces / tabs
		if ($_ eq "[$langmodename]") { goto langmodeLOOPEND };
	};
langmodeLOOPEND:
	
	while (<FILEHANDLE>)
	{
		$_ =~ s/\n//g;
		$_ =~ s/^[ \t]*//; #remove beginning spaces / tabs

		if (substr($_, 0, 1) eq "[") { goto LOOPEND };
		
		if (($_ ne "")&&(substr($_, 0, 1) ne "#"))
		{
			if ($word_delimiters eq "")
			{
				#masquerade regex specail chars
				$_ =~ 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;
				
				$word_delimiters = "([".$_." ^\$\t\n])"; # begin, end of file, <space>, <tab>, <lf> are always word del.
			}
			else
			{
				local %t;

				$_ =~ s/[^,]*,//; $tmp = $&; $tmp =~ m/[0-9]/;     $t{"type"}    = $&;    # 1, 2
				$_ =~ s/[^,]*,//; $tmp = $&; $tmp =~ m/[0-9]/;     $t{"worddel"} = $&;    # 0, 1
				$_ =~ s/[^,]*,//; $tmp = $&; $tmp =~ m/[0-9]/;     $t{"strip"}   = $&;    # 0, 1
				$_ =~ s/[ \t]*"([^"]|\\")*"[ \t]*,//; $tmp = $&; $tmp =~ m/".*[^\\]"/; $tmp = $&; $tmp =~ s/([^\\]|^)"/$1/g; $tmp =~ s/\\"/"/g; $t{"regex1"}   = $tmp;
				if ($t{"type"} == 2)
				{$_ =~ s/[ \t]*"([^"]|\\")*"[ \t]*,//; $tmp = $&; $tmp =~ m/".*[^\\]"/; $tmp = $&; $tmp =~ s/([^\\]|^)"/$1/g; $tmp =~ s/\\"/"/g; $t{"regex2"}   = $tmp;}
				$_ =~ s/[ \t]*"([^"]|\\")*"[ \t]*,//; $tmp = $&; $tmp =~ m/".*[^\\]"/; $tmp = $&; $tmp =~ s/([^\\]|^)"/$1/g; $tmp =~ s/\\"/"/g; $t{"html1"}   = $tmp;
			                                    	  $tmp = $_; $tmp =~ m/".*[^\\]"/; $tmp = $&; $tmp =~ s/([^\\]|^)"/$1/g; $tmp =~ s/\\"/"/g; $t{"html2"}   = $tmp;

				$t{"regex1"} =~ s/&/(&amp;)/g; $t{"regex1"} =~ s/>/(&gt;)/g; $t{"regex1"} =~ s/</(&lt;)/g; $t{"regex1"} =~ s/"/(&quot;)/g;
				$t{"regex2"} =~ s/&/(&amp;)/g; $t{"regex2"} =~ s/>/(&gt;)/g; $t{"regex2"} =~ s/</(&lt;)/g; $t{"regex2"} =~ s/"/(&quot;)/g;

				push @langmode, \%t;
			}
		}
	}

LOOPEND:
	close FILEHANDLE;





	#read file
	while (<STDIN>) { $_ =~ s/\n|\r//g; $var = $var.$_."\n"; };
	html_masquerade($var);

	for (@langmode)
	{
#word delimiters: none = 0
#word delimiters: both = 1
#word delimiters: end = 2
		if ($$_{"type"} == 1) { if ($$_{"worddel"} == 0) { if ($$_{"strip"} == 0) { &highlight_regex                        ($var, $$_{"regex1"}, $$_{"html1"}, $$_{"html2"}) }
		                                                   if ($$_{"strip"} == 1) { &highlight_regex_strip                  ($var, $$_{"regex1"}, $$_{"html1"}, $$_{"html2"}) } }
		                        if ($$_{"worddel"} == 1) { if ($$_{"strip"} == 0) { &highlight_regex_worddelimiters         ($var, $$_{"regex1"}, $$_{"html1"}, $$_{"html2"}) }
		                                                   if ($$_{"strip"} == 1) { &highlight_regex_strip_worddelimiters   ($var, $$_{"regex1"}, $$_{"html1"}, $$_{"html2"}) } }
		                        if ($$_{"worddel"} == 2) { if ($$_{"strip"} == 0) { &highlight_regex_worddelimitersend      ($var, $$_{"regex1"}, $$_{"html1"}, $$_{"html2"}) }
		                                                   if ($$_{"strip"} == 1) { &highlight_regex_strip_worddelimitersend($var, $$_{"regex1"}, $$_{"html1"}, $$_{"html2"}) } } }
		if ($$_{"type"} == 2) {                            if ($$_{"strip"} == 0) { &highlight_startstop                    ($var, $$_{"regex1"}, $$_{"regex2"}, $$_{"html1"}, $$_{"html2"}) }
		                                                   if ($$_{"strip"} == 1) { &highlight_startstop_strip              ($var, $$_{"regex1"}, $$_{"regex2"}, $$_{"html1"}, $$_{"html2"}) } };
	}

	print "<html><body><pre>\n";
	print $var;
	print "</pre></body></html>\n";





sub html_masquerade
{
	# string in $_[0] is string to be masqueraded

	$_[0] =~ s/&/&amp;/g;
	$_[0] =~ s/>/&gt;/g;
	$_[0] =~ s/</&lt;/g;
	$_[0] =~ s/"/&quot;/g;
}






sub highlight_regex
{
	# string in $_[0] is file
	# string in $_[1] is search regexp
	# string in $_[2] is code to be inserted before the match
	# string in $_[3] is code to be inserted after the match

	$_[0] =~ s/$_[1]/$_[2]$&$_[3]/gms;
}

sub highlight_regex_worddelimiters
{
	# string in $_[0] is file
	# string in $_[1] is search regexp
	# string in $_[2] is code to be inserted before the match
	# string in $_[3] is code to be inserted after the match

	my $end = $_[0];
	$_[0] = "";
	while ($end =~ m/$word_delimiters$_[1]$word_delimiters/ms)
	{
		my $begin = $`; 
		my $match = $&; 
		   $end   = $';
		   
		$match =~ m/$_[1]/ms; # get word delimiters after and before
		
		   $begin .= $`; 
		   $match = $&; 
		   $end   = $'.$end;

		$_[0] .= $begin.$_[2].$match.$_[3];
	};
	$_[0] .= $end;
}

sub highlight_regex_worddelimitersend
{
	# string in $_[0] is file
	# string in $_[1] is search regexp
	# string in $_[2] is code to be inserted before the match
	# string in $_[3] is code to be inserted after the match

	my $end = $_[0];
	$_[0] = "";
	while ($end =~ m/$_[1]$word_delimiters/ms)
	{
		my $begin = $`; 
		my $match = $&; 
		   $end   = $';
		   
		$match =~ m/$_[1]/ms; # get word delimiters after and before
		
		   $begin .= $`; 
		   $match = $&; 
		   $end   = $'.$end;

		$_[0] .= $begin.$_[2].$match.$_[3];
	};
	$_[0] .= $end;
}

sub highlight_regex_strip
{
	# string in $_[0] is file
	# string in $_[1] is search regexp
	# string in $_[2] is code to be inserted before the match
	# string in $_[3] is code to be inserted after the match

	my $end = $_[0];
	$_[0] = "";
	while ($end =~ m/$_[1]/ms)
	{
		my $begin = $`; 
		my $match = $&; 
		   $end   = $';
		$match =~ s/<[^>]*>//g;  #strip html
		$_[0] .= $begin.$_[2].$match.$_[3];
	};
	$_[0] .= $end;
}

sub highlight_regex_strip_worddelimiters
{
	# string in $_[0] is file
	# string in $_[1] is search regexp
	# string in $_[2] is code to be inserted before the match
	# string in $_[3] is code to be inserted after the match

	my $end = $_[0];
	$_[0] = "";
	while ($end =~ m/$word_delimiters$_[1]$word_delimiters/ms)
	{
		my $begin = $`; 
		my $match = $&; 
		   $end   = $';
		   
		$match =~ m/$_[1]/ms; # get word delimiters after and before
		
		   $begin .= $`; 
		   $match = $&; 
		   $end   = $'.$end;

		$match =~ s/<[^>]*>//g;  #strip html
		
		$_[0] .= $begin.$_[2].$match.$_[3];
	};
	$_[0] .= $end;
}

sub highlight_regex_strip_worddelimitersend
{
	# string in $_[0] is file
	# string in $_[1] is search regexp
	# string in $_[2] is code to be inserted before the match
	# string in $_[3] is code to be inserted after the match

	my $end = $_[0];
	$_[0] = "";
	while ($end =~ m/$_[1]$word_delimiters/ms)
	{
		my $begin = $`; 
		my $match = $&; 
		   $end   = $';
		   
		$match =~ m/$_[1]/ms; # get word delimiters after and before
		
		   $begin .= $`; 
		   $match = $&; 
		   $end   = $'.$end;

		$match =~ s/<[^>]*>//g;  #strip html
		
		$_[0] .= $begin.$_[2].$match.$_[3];
	};
	$_[0] .= $end;
}







sub highlight_startstop
{
	# string in $_[0] is file
	# string in $_[1] is start regexp
	# string in $_[2] is stop regexp
	# string in $_[3] is code to be inserted before the match
	# string in $_[4] is code to be inserted after the match


	my $end = $_[0];
	$_[0] = "";
	while($end =~ m/$_[1]/msg)
	{
		my $begin = $`; 
		my $match = $&; 
		   $end   = $';
	
		$end =~ m/$_[2]/msg;
		
		   $match .= $`.$&;  #match1, inbetween, match2
		   $end    = $';
		
		$_[0] .= $begin.$_[3].$match.$_[4];
	};

	$_[0] .= $end;
}

sub highlight_startstop_strip
{
	# string in $_[0] is file
	# string in $_[1] is start regexp
	# string in $_[2] is stop regexp
	# string in $_[3] is code to be inserted before the match
	# string in $_[4] is code to be inserted after the match


	my $end = $_[0];
	$_[0] = "";
	while($end =~ m/$_[1]/msg)
	{
		my $begin = $`; 
		my $match = $&; 
		   $end   = $';
	
		$end =~ m/$_[2]/msg;
		
		   $match .= $`.$&;  #match1, inbetween, match2
		   $end    = $';
		

		$match =~ s/<[^>]*>//g;  #strip html
		
		$_[0] .= $begin.$_[3].$match.$_[4];
	};

	$_[0] .= $end;
}

