#!/usr/bin/perl -w
#
# htmlify version 0.1.1 - script to convert text files to HTML
#
# Copyright (C) 2002 Daniel Burton
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# By Daniel Burton
# daniel@individualist-anarchist.net
# http://www.Individualist-Anarchist.Net/software/


my $title = '';

while (@ARGV and $ARGV[0] =~ /^-/) {
    $_ = shift @ARGV;
    if (/^-t$/) {
	$title = shift @ARGV;
    } else {
	print "Usage: htmlify [--help] [-t title] [[file] ... ]\n";
	exit(1);
    }
}

my $lines;

while ($_ = <>) {
    chomp($_);
    # Remove leading whitespace
    s/^\s+//g;
    $lines .= "$_\n";
}

undef $/;
$_ = $lines;

# Convert < and >
s/</&lt\;/g;
s/>/&gt\;/g;

# Make non-breaking spaces after the ends of sentences.
s/(?<=[.!?])[ \t][ \t]*/&nbsp\; /g;
s/(?<=[.!?]")[ \t][ \t]*/&nbsp\; /g;

# Add paragraph divisions
s#\n\n+#</P>\n\n\<P>#g;

# Convert lines like "---" to <HR>
s#<P>\s*-+\s*</P>#<HR>#g;

# Convert "--" to m dash
s/--/&mdash;/g;

# Add finishing </P>
!m#</P>$# and $_ .= "</P>";

# Eliminate spurious <P></P>
s#<P></P>\n?##g;

print <<END;
<HTML>
<HEAD>
<TITLE>$title</TITLE>
<STYLE type="text/css">
<!--
h1 { text-align: center }
-->
</STYLE>
</HEAD>

<BODY>

END

print "<H1>$title</H1>\n\n" if $title;
print "<P>$_\n</BODY>\n</HTML>";
