#!@@PERL@@ -s
#
#	This script is part of rtfdtohtml.
#	Copyright (c) 1996  Elmar Ludwig - Universitaet Osnabrueck
#
#	Experimental table support (this is now the default),
#	simple indentation is emulated using nested lists.
#
#	Special rule:	Code enclosed in "\t{sometext}" and "\t{}"
#			will become pre-formatted text.

print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n";

%TAGS = ('/TT', '</TT>', '/I', '</I>', '/B', '</B>', '/U', '</U>',
	 '/STRIKE', '</STRIKE>', '/BIG', '</BIG>', '/SMALL', '</SMALL>',
	 '/SUB', '</SUB>', '/SUP', '</SUP>', '/EM', '</EM>',
	 '/STRONG', '</STRONG>', '/DFN', '</DFN>', '/CODE', '</CODE>',
	 '/SAMP', '</SAMP>', '/KBD', '</KBD>', '/VAR', '</VAR>',
	 '/CITE', '</CITE>', '/H1', '</H1>', '/H2', '</H2>', '/H3',
	 '</H3>', '/H4', '</H4>', '/H5', '</H5>', '/H6', '</H6>');

sub printstr
{
    $str = $_[0];

    unless ($html2 || $head)
    {
	$str =~ s/<[Hh]([1-4])>/'<FONT size="+'.(5-$1).'">'/ge;
	$str =~ s/<[Hh]([56])>/'<FONT size="'.(4-$1).'">'/ge;
	$str =~ s/<\/[Hh][1-6]>/<\/FONT>/g;
    }
    print $str;
}

sub finalize
{
    if ($html2 || !$usetable)
    {
	&printstr($text);
	while ($nest)
	{
	    print '</DL>';
	    --$nest;
	}
    }
    else
    {
	&printstr($table . "</TABLE>\n");
	$nest = 0;
    }

    $usetable = $text = $table = '';
}

sub blue
{
    $_ = $_[0];
    return $_[1] unless s/&lt;(([^&]|&([^g]|g([^t]|t[^;])))*)&gt;//;

    $url = $1;
    $url =~ s/<[^>]*>//g;
    if ($url !~ m%^[A-Za-z]+://% && $url !~ m%^//% && $url !~ /^mailto:/ &&
	$url !~ /^news:/ && ! -e $url)
    {
	print STDERR "Warning: invalid hypertext link to `$url'\n";
    }
    return "<A href=\"$url\">$_</A>";
}

$dir = $ENV{'FILENAME'};
if ($dir !~ /\.rtfd$/)
{
    $dir = $dir =~ /^(.*)\// ? $1 : '.';
}
chdir $dir || die "chdir failed for $dir: $!";

$nest = 0;

while (<>)
{
    s/<body>/<body $ENV{'RTFDBODYATTR'}>/i;	# default is "bgcolor=White"

    if (/<\/body>/i)
    {
	$_ = $`;
	$last = $& . $';
    }

    # remove some weird HTML-tags...
    while (s/<(\w+)>([ \t]*)<\/\1>/$2/g) {}
    
    if ($html2 || $head)
    {
	/<[Hh][1-6]>.*<[Hh][1-6]>.*<br>$/ && s/<\/?[Hh][1-6]>//g;
    }

    s%<FONT color=\"#0000FF\">(([^<]|<([^/]|/([^F]|F([^O]|O([^N]|N([^T]|T[^>]))))))*)</FONT>%&blue($1,$&)%eg if defined $blue;

    $pre = $_;
    $pre =~ s/<\/?FONT[^>]*>//g;
    $pre =~ s/<\/?[Hh][1-6]>//g;

    if ($pre =~ /^((<HR>|<A [^>]*>|<\/A>)*) *\t{[^;{}]+}<br>$/)
    {
	s/<br>$//i;
	if ($code)
	{
	    s/{[^;{}]+}//;
	    print "\n";
	}
	else
	{
	    &finalize if $table;
	    s/{[^;{}]+}/<PRE>/;
	    $code = 'YES';
	}
	&printstr($_);
    }
    elsif ($pre =~ /^((<HR>|<A [^>]*>|<\/A>)*) *\t{}(<br>)?$/ && $code)
    {
	s/<br>$//i;
	s/{}/<\/PRE>/;
	$code = '';
	&printstr($_);
    }
    elsif ($code)
    {
	s/<br>$//i;
	&printstr($_);
    }
    elsif (index($_, "\t") > -1)
    {
	$table = "<TABLE>\n" unless $table;

	if ($tables)
	{
	    $bold = '';

	#   /^(<[^>]+>)*\t*[^\t]+(\t*)(<[^>]+>)*/;
	#   $left = $&;
	#   $bold = /^(<[^>]+>|\t)*<[Bb]>/ && $left =~ /<\/[Bb]>(<[^>]+>|\t)*$/;
	}
	else
	{
	    $bold = /^(<[^>]+>|\t)*<[Bb]>/;	# any line starting bold
	}

	s/(<img [^>]+>(<[^>]+>)*)\t/$1 \t/gi;

	/^(<[^>]+>)*\t*[^\t]+(\t*)[^\t]*(\t*)/;
	$usetable = 'YES' if !$usetable && ($2 && !$bold || $3);

	# Use <DT> for this line? (Yes, if first word is bold.)
	$bold ? /^((<[^>]+>)*)(\t*)([^\t]+)(\t*)/ :
		/^((<[^>]+>)*)(\t*)([^\000]+)(@*)/;

	$level = length($3) + ($5 ? 1 : 0);
	$delta = $level - $nest;

	while ($delta)
	{
	    if ($delta > 0)
	    {
		$text .= '<DL>';
		--$delta;
		$text .= '<DD>' if $delta || !$5;
	    }
	    else
	    {
		$text .= '</DL>';
		++$delta;
	    }
	}

	if ($5)
	{
	    $text .= "<DT>$1$4";
	    $' =~ /^(<\/[^>]+>)*/;
	    $text .= "$&<DD>\n";
	    $line = $';
	}
	else
	{
	    $text .= "\n" if $level != $nest;
	    $line = $1 . $4;
	}

	$text .= $line;
	$nest = $level;

	$table .= '<TR valign=top><TD>';
	s/\t/&nbsp;&nbsp;&nbsp;<TD>/g;
	s/<br>$//i;

	$line = $tags = '';
	$pos = 0;

	while (/<([\/A-Za-z0-9]+)>/g)
	{
	    $left = $`;
	    $tag = "\U$1\E";
	    $atag = '/' . $tag;

	    if (defined $TAGS{$atag})
	    {
		$tags = $TAGS{$atag} . $tags; next;
	    }
	    if (defined $TAGS{$tag})
	    {
		$tags = substr($tags, length $TAGS{$tag}); next;
	    }

	    if ($tag eq 'TD' && $tags)
	    {
		$tags1 = join('', reverse split(/(<\/[A-Z0-9]+>)/, $tags));
		$tags1 =~ s%/%%g;
		$line .= substr($left, $pos) . $tags . '<TD>' . $tags1;
		$pos = length($left) + 4;
	    }
	}

	$line .= substr($_, $pos);
	if ($pos)
	{
	    while ($line =~ s%<(\w+)></\1>%%gi) {}
	}
	$table .= $line;
    }
    else 
    {
	&finalize if $table;
	&printstr($_);
    }

    if ($last)
    {
	print "</PRE>\n" if $code;
	&finalize if $table;
	print $last;
	last;
    }
}
