#!/usr/bin/perl -w
# Utility to format Angband character dumps into HTML
# Assumes records (the characters and the colours) are sep by \n
# and that each line of the char and colours is the same length
# (c) 2002 James Andrewartha <trs80@tartarus.uwa.edu.au>
# License: Angband license or GPL v2 or greater
# See http://thangorodrim.angband.org/opensource.html for details

# Angband Color Map (from color.prf)
%colmap = (
d => '#000000',
w => '#ffffff',
s => '#8c8c8c',
o => '#ff7700',
r => '#c90000',
g => '#008645',
b => '#0000e3',
u => '#8e4500',
D => '#505050',
W => '#d1d1d1',
v => '#c000af',
y => '#ffff00',
R => '#ff0068',
G => '#00ff00',
B => '#51dd0ff',
U => '#d78e45' );

# Get the input and output
($ARGV[0]) and $dumpfile = $ARGV[0] or $dumpfile = "dump.txt";
($ARGV[1]) and $htmlfile = $ARGV[1] or $htmlfile = "dump.html";

open (DUMPFILE,$dumpfile);
open (HTMLFILE,">$htmlfile");

# Read in the screen dump
@lines = ();
@colours = ();
while (<DUMPFILE>)  {
	if ($_ eq "\n") { last; }
	chomp;
	push @lines, ($_);
}
while (<DUMPFILE>)  {
	if ($_ eq "\n") { last; }
	chomp;
	push @colours, ($_);
}
close DUMPFILE;

# Print the HTML prelude
print HTMLFILE <<'EOHeader';
<HTML><HEAD><TITLE>Angband Character Dump</TITLE>
<style type="text/css"><!--
pre { font-family: monospace; }
--></style>
</HEAD><BODY BGCOLOR="black"><PRE>
EOHeader
# Print the HTMLised screen dump
foreach $line (@lines) {
	$colours = shift @colours;
	@chars = split "",$line;
	@charcols = split "",$colours;
	$curcol = $charcols[0];
	print HTMLFILE '<FONT COLOR="'.$colmap{$curcol}.'">';
	for ($i = 0; $i < $#chars; ++$i) {
		if ($charcols[$i] ne $curcol) {
			$curcol = $charcols[$i];
			print HTMLFILE '</FONT><FONT COLOR="'.$colmap{$curcol}.'">';
		}
		print HTMLFILE $chars[$i];
	}	
	print HTMLFILE "</FONT>\n";
}
print HTMLFILE "</PRE></BODY></HTML>";
