#!/afs/athena/contrib/perl/p -s
#<plaintext>
# Usage: htmlify [-width=n -nohyphen -<cde>header=Hn] file
# "width" is the typical width of a line [default=72]
# if "nohyphen" is not specified, assume that the document contains splits
#     unhyphenated words at syllable boundaries
# "cheader", "dheader", "eheader" are the types of header (i.e. "H1") which
#     should be used for implicit headers formed by "all caps", "dash (-)
#     underlining", or "equal (=) underlining"
#
#If you are reading this via mosaic, it may be interpreted as html --
#it will probably be clearer if you read the "document source" instead....

$* = 1;                         # assume newlines in string searches
$/ = "";                        # paragraph mode
$| = 1;                         # flush immediately
$body = 0;
$pindent = 0;
$newpara = 0;
$cheader || ($cheader = "H2");
$dheader || ($dheader = "H2");
$eheader || ($eheader = "H1");
$sheader || ($sheader = "H1");
$hastitle = 0;
$width || ($width = 72);

if ($nohyphen) {
  $hstring = "^(.*[^-]-) *$";
} else {
  $hstring = "^(.*[^-])- *$";
}
while (<>) {
    s/\t/        /g;
    $oldtxt = "";
    while ($_) {
        ($txt, $sep, $rest) = split(/^(.*\n *-+ *\n|.*\n *\*+ *\n|.*\n *=+ *\n|[^a-z\n]*[A-Z][^a-z\n]*\n)/,$_,2);
        if ($sep) {
            if ($sep =~ /^( *)([^a-z\n]*[A-Z][^a-z\n]*)\n/) {
                $hastitle++ || print "<TITLE>$2</TITLE>\n";
                $h = "$1<$cheader>$2</$cheader>\n";
                &dotext($oldtxt . $txt);
                $oldtxt = "";
                print $h;
                $newpara = 0;
            } elsif ($sep =~ /^( *)(.*[^ \n]) *\n *(-+) *\n/ &&
                     length($2) == length($3)) {
                $hastitle++ || print "<TITLE>$2</TITLE>\n";
                &dotext($oldtxt . $txt);
                $oldtxt = "";
                print "$1<$dheader>$2</$dheader>\n";
                $newpara = 0;
            } elsif ($sep =~ /^( *)(.*[^ \n]) *\n *(\*+) *\n/ &&
                     length($2) == length($3)) {
                $hastitle++ || print "<TITLE>$2</TITLE>\n";
                &dotext($oldtxt . $txt);
                $oldtxt = "";
                print "$1<$sheader>$2</$sheader>\n";
                $newpara = 0;
            } elsif ($sep =~ /^( *)(.*[^ \n]) *\n *(=+) *\n/ &&
                     length($2) == length($3)) {
                $hastitle++ || print "<TITLE>$2</TITLE>\n";
                &dotext($oldtxt . $txt);
                $oldtxt = "";
                print "$1<$eheader>$2</$eheader>\n";
                $newpara = 0;
            } else {
                $oldtxt = $oldtxt . $txt . $sep;
            }
            $_ = $rest;
        } else {
            &dotext($oldtxt . $txt);
            $_ = "";
        }
    }
    $newpara = 1;
}
exit;

sub dotext {
    local($txt) = pop(@_);
    $txt =~ s/\&/&amp;/g;
    $txt =~ s/\>/&gt;/g;
    $txt =~ s/\</&lt;/g;

    split(/\n/, $txt);
    $indent = 9999;
    $maxlen = 0;
    foreach $line (@_) {
        $line =~ /^( *)(.*)/;
        (length($1) < $indent) && ($indent = length($1));
        (length($2) > $maxlen) && ($maxlen = length($2));
    }
    $line = $_[0];
    $line =~ /^( *)/;
    if (length($1) > $indent) {
        $pindent = length($1) - $indent;
    }
    $text = "";
    $hyphen = 0;
    foreach $line (@_) {
        $line =~ /^( *)(.*)/;
        $lindent = $1;
        $hyphen && ($line = $text . $2);
        $text = $2;
        if ($newpara || ($pindent != 0 && length($lindent) == $pindent)) {
            print("<P>\n");
        }
        if (length($text) < (($width - 12) - $pindent)) {
            if ($text =~ /[^-\.\"?!:) ] *$/) {
                print(STDERR "*** Suspicious paragraph end: $text\n");
            }
            $newpara = 1;
            print "$line\n";
            $hyphen = 0;
        } else {
            $newpara = 0;
            $hyphen = ($line =~ /$hstring/o);
            if ($hyphen) {
                print(STDERR "*** Unhyphenating line: $text\n");
                $text = $1;
            } else {
                print "$line\n";
            }
        }
    }
}    
#</plaintext>
