#!/home/jc/bin/perl5 -dw
#
# txt2html [file]..
#
# This is a prototype program to read text and  convert  it  to  html.
# This program acts like a conventional Unix filter program:  It reads
# the file(s) named on the command line, or stdin if there are no file
# names on the command line; it writes to stdout.

$| = 1;				# Don't buffer output.
$mintxtlen = 20;	# Shorter lines treated as non-text.
print "<html>\n";	# Boilerplate.

@txt = <>;	# Slurp up all the text.

for $line (@txt) {
	$line =~ s"\s+$"$/";	# Trim away white stuff.
	if ($line eq $/) {		# Blank line?
		$newpar = 1;		# Treat as end of paragraph.
		if ($mailhdrs) {	# Are we processing mail headers?
			$line = "</pre><hr>\n";
			$mailhdrs = 0;
		} else {
			$line = "<p>\n";
		}
		next;
	}
	if ($line =~ /^\s*[\-= ][\-= ]+\s*$/) {
		$line = "<hr>\n";
		next;
	}
	# Check for things that look like nmbered or bulleted lists:
	if ($line =~ /^\s*\w\s*[\-.:]\s*/) {
		$line = "<br>" . $line;
	#	$line =~ s/(.*)\n$/<br>$1<br>\n/;
		next;
	}
	if ($newpar) {		# Check for formatted lines.
		if (length($line) < $mintxtlen) {	# Too short.
			$line =~ s"$/$"<br>$/";
			next;
		}
		# Check for all capitalized words to try to spot things that 
		# look like addresses or titles:
		if (!($line =~ /\s[a-z]/)) {
			$line =~ s"$/$"<br>$/";
			next;
		}
	}
	if  (($lbl,$val) = ($line =~ /^(From\s|From:|Path:|Subject:)\s*(.*)/)) {	# Mail hdr?
		if  ($line =~ "^(From|From:|Path:)\s*(.*)!(.+)!(.+)!(.+)") {
			$line = "$1 $3!$4!$5\n";	# Trim to host2!host1!user.
		}
		if ($lbl eq 'Subject:') {
			splice @txt,0,0, "<title>$val</title>\n";
		}
		$line = "<pre>\n" . $line
			if ! defined($mailhdrs);
		++$mailhdrs;
	}
	$newpar = 0;
}

for $line (@txt) {
	print $line;
}
print "</html>\n";	# Boilerplate.
