#!/usr/bin/perl
#
# prephtml - tidy up usman.html (generated by l2h from usman.tex).
#            Add page heading, table of contents etc.
#

$fname = $ARGV[0];
$quiet = 0;
$singlePage = 1;
$ext = "html";
$opp_version = "2.3";

#
# general template for our output HTML pages; real contents will go to __BODY__
#
$template = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
   <TITLE>OMNeT++ - Manual</TITLE>
   <META NAME="Author" CONTENT="Andras Varga">
</HEAD>

<style type="text/css">

H1 {
color: #006f00;
}

H2 {
color: #006f00;
}

H3 {
color: #006f00;
}

.subheading {
color: black;
font-weight: bold;
}

</style>

<BODY TEXT="#000000" BGCOLOR="#FFFFE8" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000">

<P><FONT COLOR="#FF0000"><B><FONT SIZE=+4>OMNeT++ </FONT></B>
<I><FONT SIZE=+1>Discrete Event Simulation System</FONT></I></FONT></P>

<!--
<br>
<P><FONT SIZE=-1>Note: HTML rendering of this manual is not 100% -- use PDF when in doubt</FONT></P>
-->
__BODY__

</BODY>
</HTML>
';

#
# read file into variable
#
print "reading $fname\n" unless $quiet;
open( htmlf, $fname ) || die "cannot open $fname";
read( htmlf, $html, 1000000 ) || die "cannot read $fname";

#
# kill unwanted formatting
#
print "removing unnecessary formatting and parts\n" unless $quiet;
$html =~ s/ SIZE=2//g;
$html =~ s/ FACE="[^"]*"//g;
$html =~ s/ color=[a-z]*?//g;

#$html =~ s/(\<H[0-9]\>.*?\<\/H[0-9]\>)/<font color="#006f00">\1<\/font>/g;

# add IDs to headers
$ctr=100;
$html =~ s/(\<H[0-9]\>)/$1."<A NAME=\"sec".$ctr++."\"\/>"/ge;

#
# Do some more LaTeX formatting, not done by L2H
#
$html =~ s/\$\^([^ ]*)\$/\<sup\>$1\<\/sup\>/g;
$html =~ s/\\chapter\*(.*)/\<hr\>\<b\>$1\<\/b\>/g;
$html =~ s/\\'([AEOUaeuo])/&$1acute;/g;
$html =~ s/\\"([OUuo])/&$1uml;/g;
$html =~ s/\\-//g;


#
# kill html header, title page, bad table of contents etc.
#
#$html =~ s/^(.*?)\<H1\>/<H1>/s;
#$html =~ s/\<\/BODY\>.*$//s;

##
## add horiz bar before each H1 heading
##
# $html =~ s/\<H1\>/<HR><H1>/g;

#
# convert filenames in <IMG SRC="IMG00010.GIF"> tags to lowercase
#
#$html =~ s/\"IMG([0-9]*)\.GIF\"/\"img$1.gif\"/g;

#
# collect heading info from document
#
print "collecting headings\n" unless $quiet;
$scrap = $html;
$i = 0;
while ($scrap =~ s/\<H([0-9])\>(.*?)\<\/H[0-9]\>//s)
{
      $level[$i] = $1;
      $2 =~ m/\<A NAME\=\"(.*?)\"\/\>(.*)/s;
      $label[$i] = $1;
      $title[$i] = $2;
      $i++;
}
$nheadings = $i;


#
# calculate heading numbers
#
print "adding heading numbers\n" unless $quiet;
for ($l=1; $l<10; $l++) {$hnum[$l]=0;}
for ($i=0; $i<$nheadings; $i++)
{
      # advance numbering
      $hnum[$level[$i]]++;
      for ($j=$level[$i]+1; $j<10; $j++) {
         $hnum[$j] = 0;
      }

      # assemble current heading number, e.g. "1.4.2."
      $number[$i] = '';
      for ($j=1; $j<=$level[$i]; $j++) {
         $number[$i] .= $hnum[$j].'.';
      }

      # store chapter number
      $chapnum[$i] = $hnum[1];
}

#
# insert numbers into manual body
#
for ($i=0; $i<$nheadings; $i++)
{
      # search replace phrases like this: <A NAME="_Toc473901724">Expressions</A>
      $html =~ s/(\<A NAME=\"$label[$i]\"\/>)/\1$number[$i] /sg;
}

#
# create 'chapters' table
#
print "creating Topics and TOC tables\n" unless $quiet;
if ($singlePage) {
      $chapters = "<H1>Chapters</H1>\n";
} else {
      $navbar="<p><b><hr><A HREF=\"toc.$ext\">[Table of Contents]</a><hr></b></p>";
      $chapters = $navbar . "<H1>OMNeT++ User Manual -- Chapters</H1>\n";
}
for ($i=0; $i<$nheadings; $i++)
{
      if ($level[$i]==1)
      {
           $link = $singlePage ? "#toc_$number[$i]" : "chap$chapnum[$i].$ext";
           $entry = "<B><A HREF=\"$link\">$number[$i] $title[$i]</A></B><BR>";
           $chapters .= "$entry\n";
      }
}

#
# create table of contents
#
if ($singlePage) {
      $toc = "<H1>Table of Contents</H1>\n";
} else {
      $navbar="<p><b><hr><A HREF=\"usman.$ext\">[Chapters]</a><hr></b></p>";
      $toc = $navbar . "<H1>Table of Contents</H1>\n";
}
for ($i=0; $i<$nheadings; $i++)
{
      $link = $singlePage ? "#$label[$i]" : "chap$chapnum[$i].$ext#$label[$i]";
      $entry = "<A NAME=\"toc_$number[$i]\"></A><A HREF=\"$link\">$number[$i] $title[$i]</A>";
      for ($j=0; $j<$level[$i]; $j++) {$entry = "&nbsp;&nbsp;$entry";}
      if ($level[$i]==1)    {$entry = "<BR><FONT SIZE=+1><B>$entry</B></FONT>";}
      elsif ($level[$i]==2) {$entry = "<B>$entry</B>";}
      $toc .= "$entry<BR>\n";
}

if ($singlePage)
{
      print "writing page\n" unless $quiet;
      $new_html = $template;
      $body = "<H1><B>User Manual</B></H1><H3>OMNeT++ version $opp_version</H3>$chapters<HR>$toc$html";
      $new_html =~ s/__BODY__/$body/;
      open(FILE,">usman.$ext");
      print FILE $new_html;
      close FILE;
}
else
{
      #
      # split up to chapters along H1 headings
      #
      print "splitting up to chapters\n" unless $quiet;
      $nchapters = 0;
      $scrap = $html;
      while ($scrap =~ s/(\<H1\>.*?)\<H1\>/\<H1\>/s)
      {
            $chapter[++$nchapters] = $1;
      }
      $chapter[++$nchapters] = $scrap;

      #
      # create navigation bars for each chapter
      #
      for ($i=1; $i<=$nchapters; $i++)
      {
            $navbar[$i] = "<p><hr><b>";
            if ($i>1) {$t=$i-1; $navbar[$i] .= "<A HREF=\"chap$t.$ext\">[Prev]</A>";}
            if ($i<$nchapters) {$t=$i+1; $navbar[$i] .= "&nbsp;<A HREF=\"chap$t.$ext\">[Next]</A>";}
            $navbar[$i] .= "&nbsp;<A HREF=\"toc.$ext#toc_$i.\">[TOC]</A>";
            $navbar[$i] .= "&nbsp;<A HREF=\"usman.$ext\">[Chapters]</A>";
            $navbar[$i] .= "</b><hr></p>";
      }

      #
      # write each chapter into a separate file
      #
      print "writing chapter files\n" unless $quiet;
      for ($i=1; $i<=$nchapters; $i++)
      {
            $new_html = $template;
            $new_html =~ s/__BODY__/$navbar[$i]$chapter[$i]$navbar[$i]/;
            open(FILE,">chap$i.$ext");
            print FILE $new_html;
            close FILE;
      }

      #
      # write chapters file (usman.html)
      #
      print "writing chapters file\n" unless $quiet;
      $new_html = $template;
      $body = "<H1><B>User Manual</B></H1><H3>OMNeT++ version $opp_version</H3>$chapters";
      $new_html =~ s/__BODY__/$body/;
      open(FILE,">usman.$ext");
      print FILE $new_html;
      close FILE;

      #
      # write toc file (toc.html)
      #
      print "writing toc file\n" unless $quiet;
      $new_html = $template;
      $new_html =~ s/__BODY__/$toc/;
      open(FILE,">toc.$ext");
      print FILE $new_html;
      close FILE;
}

print "done\n" unless $quiet;




syntax highlighted by Code2HTML, v. 0.9.1