#!/usr/bin/perl
#
# This little script converts a LaTeX toc file to a HTML Table.
# Created by Christian Fries, http://www.christian-fries.de
# This file is placed in the public domain.
#
# Version 1.1
#

$Suffix = ".html";

$lines_convertes = 0;
$lines_total = 0;

while (<@ARGV>)
{
    open(IN,$_)                 || die "Unable to open input file. $Warning: $!\n";
    open(OUT,">".$_.$Suffix)    || die "Unable to open output file. $Warning: $!\n";

    print OUT "<table>\n";
    while(<IN>)
    {
        $lines_total++;
        
        if(
        s/^\\contentsline \{[^\{\}]*?\}\{\\numberline \{([^\{\}]*?)\}(.*?)\}\{([^\{\}]*?)\}\{[^\{\}]*?\}$/  \074td class=\"tocnumber\"\076 $1 \074\057td\076  \074td class=\"toctext\"\076 $2 \074\057td\076  \074td class=\"tocpage\"\076 $3 \074\057td\076 /gi ||
        s/^\\contentsline \{[^\{\}]*?\}\{(.*?)\}\{([^\{\}]*?)\}\{[^\{\}]*?\}$/  \074td class=\"tocnumber\"\076 \074\057td\076  \074td class=\"toctext\"\076 $1 \074\057td\076  \074td class=\"tocpage\"\076 $2 \074\057td\076 /gi
        )
        {
            $lines_converted++;
                   
            s/\\textit \s*\{([^\}]*)\}/$1/gi;        
            s/\\\^o/o/gi;
            s/\\"(.)/&$1uml;/gi;
            s/\\IeC \{\\ss \}/&szlig;/gi;
            s/\\glqq /&quot;/gi;
            s/\\grqq/&quot;/gi;
            s/\\ / /gi;
            s/~/&nbsp;/gi;
            s/\\texttrademark/&#153;/gi;
    
            print OUT "  <tr>\n";
            print OUT "    ".$_;
            print OUT "  </tr>\n";
        }
        else
        {
            print "Not converted: ".$_;
        }
    }
    print OUT "</table>";
    close(IN);
    close(OUT);
    
    print "Converted ".$lines_converted." of ".$lines_total." lines.\n";
}
