#!/usr/bin/perl

#
# This script is licensed under GPL (GNU General Public License)
# Use it at your own risk.
#
# (c) Christian Boltz 2003
#
# visit http://tux.boltz.de.vu to contact me or to download a new version
#


# Note: You have to add the ContentType header yourself!
#       Do this i. e. with a filter in your mail client.
#       A detailed manual can be found on my homepage.

use strict;
use warnings;

#
# configure this for your commit mail format (may contain RegExes)
#
my $diff_start		= "^Index: ";
my $newfile_start	= "^--- NEW FILE: ";
my $delfile_start	= "^--- .* DELETED ---";
my $cvs_tag			= "^ +Tag: ";
my $mailfooter		= "^-------------------------------------------------------\$|^---------------------------------------------------------------------\$";

#
# internal variables
#
my $mailpart		=	0	;
my $line			=	"_"	;

#
# pass through header
#

until ($line =~ /^$/) {
	$line = <STDIN>;
	print $line;
}

&init_body;
print "<pre>";
while (defined ($line = <STDIN>)) {
	chomp $line;
	$line =~ s/\&/\&amp;/g; # encode & (must be done first!)
	$line =~ s/\</\&lt;/g; # encode <
	$line =~ s/\>/\&gt;/g; # encode >

	if ($line =~ /$diff_start/) {
		$mailpart = 1;
		style_it ("diff_start");

	} elsif ($line =~ /$newfile_start/) {
		$mailpart = 2;
		style_it ("newfile");

	} elsif ($line =~ /$delfile_start/) {
		$mailpart = 2;
		style_it ("delfile");

	} elsif ($line =~ /$mailfooter/) {
		$mailpart = 99;
	}

	if ($mailpart == 0) { #	Intro text (commit message, ...)
		$line =~ /$cvs_tag/ && style_it ("cvs_tag");
	} elsif ($mailpart == 1) { # diffs
		$line =~ /^\+/ && style_it ("line_added");
		$line =~ /^\-/ && style_it ("line_removed");
		$line =~ /^\@/ && style_it ("linenumber");
	} elsif ($mailpart == 99) { # mailing list footer
		style_it ("mailfooter");
	}

	if ($line =~ /^$/) { $line = "&nbsp;"; }
	if ($line !~ /^\</) { $line = "<p>$line</p\n>"; }
	print "$line";
}
print "</pre>\n</body></html>\n";

sub style_it {
	my $style=shift;
	$line = "<p class='$style'>$line</p\n>";
}

sub init_body {
	print << 'EOF';
<html><head>
<style>
	p			{ margin:0px; padding:0px; }
	.line_added { color:green; }
	.line_removed { color:red; }
	.linenumber { color:#CD853F; }
	.cvs_tag	{ background-color:yellow; }
	.diff_start	{ background-color:#80FFFF; }
	.newfile	{ background-color:#80FF80; }
	.delfile	{ background-color:#FF6060; }
	.mailfooter	{ color:grey; }
</style>
</head>		
<body>

EOF
}

# vim:ts=4

