#!/usr/bin/perl
######################################################################
#                                                                    #
# Code2HTML                                                          #
# ---------                                                          #
#                                                                    #
# written 1999 by Palfrader Peter palfrader@writeme.com              #
#                                                                    #
# Version 0.1                                                        #
#                                                                    #
# License for this program: Redestribution/modification of this      #
# program is forbidden. If a friend of yours wants this program,     #
# he/she should contact me, I'll give him/her a copy. You may not    #
# redistribute this beta version.                                    #
#                                                                    #
# The final version will be distributed under the GPL                #
#                                                                    #
######################################################################


    # word delimiters :     +  ?  .  *  ^  {  }  [  ]  |   \  +() ,/`'!\@#\%\&-=\":;<>~\n\t])
	$word_delimiters = "([\\+\\?\\.\\*\\^\\{\\}\\[\\]\\|\\\\\\+() ,/`'!\@#\%\&-=\":;<>~\n\t])";
	#                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	#                   these are regex special characters. they need to be escaped with a \ (backslash) which
	#                   itself needs to be escaped in the string.







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

#replace tabs with 4 spaces
	$var =~ s/\t/    /g;

	html_masquerade($var);
	
	


#braces
	&highlight_regex(							$var,
												"[{}]",
												"<strong>",
												"</strong>");

#keyword							 
	&highlight_regex_worddelimiters(			$var,
												"(new|delete|this|return|goto|if|else|case|default|switch|break|continue|while|do|for|catch|throw|sizeof|true|false|namespace|using|dynamic_cast|static_cast|reinterpret_cast)",
												"<strong>",
												"</strong>");

#storage keyword							 
	&highlight_regex_worddelimiters(			$var,
												"(class|typename|typeid|template|friend|virtual|inline|explicit|operator|overload|public|private|protected|const|extern|auto|register|static|mutable|unsigned|signed|volatile|char|double|float|int|long|short|bool|wchar_t|void|typedef|struct|union|enum)",
												"<strong>",
												"</strong>");

#numeric constant
	&highlight_regex_worddelimiters(			$var,
												"((0(x|X)[0-9a-fA-F]*)|(([0-9]+\\.?[0-9]*)|(\\.[0-9]+))((e|E)(\\+|-)?[0-9]+)?)(L|l|UL|ul|u|U|F|f)?",
												"<font color=\"red\">",
												"</font>");

#character constant
	&highlight_regex(							$var,
												"'.'",
												"<font color=\"maroon\">",
												"</font>");
							 
#string
	&highlight_startstop_strip(					$var,
												"\&quot;",
												"(\&quot;|\$)",
												"<font color=\"green\">",
												"</font>");


#preprocessor line
	&highlight_startstop_strip(					$var,
												"^[ 	]*#",
												"\$",
												"<font color=\"blue\">",
												"</font>");

#cplus comment
	&highlight_startstop_strip(					$var,
												"//",
												"\$",
												"<font color=\"gray\"><i>",
												"</i></font>");

#comment
	&highlight_startstop_strip(				$var, 
												"[^/]/\\*",
												"\\*/",
												"<font color=\"gray\"><i>", 
												"</i></font>");


	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_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_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;
}
