#!/usr/bin/perl -w
use strict;
use FindBin;
use lib "$FindBin::RealBin/../lib";
use Pod::Html;
use Pod::Find;
use LaTeXML;
use LaTeXML::Post;

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
my $LOC = $FindBin::RealBin."..";
my $DEST= "/local/www/site/htdocs/DigitalMathLib/LaTeXML";
my $PDDEST = "$DEST/perldoc";

#======================================================================
# Process the LaTeX documentation
print "Generating LaTeXML documentation in $DEST\n";
LaTeXML->new()->convertAndWriteFile('LaTeXML');
LaTeXML::Post->new()->process('LaTeXML',destination=>"$DEST/LaTeXML.html",
		      CSS=>'latexml.css');

#======================================================================
# Process the perldocs
# This indicates which packages are actually contained in modules (in addition to the expected one).
our %GLOMPED = (Box       =>[qw(MathBox Comment List MathList Whatsit)],
		Definition=>[qw(Expandable Primitive Parameter Constructor)],
		Font      =>[qw(MathFont)],
		Token     =>[qw(Tokens Number Dimension MuDimension Glue MuGlue KeyVals)]
	       );
our %REDIRECT =(
	       "XML/LibXML/Document.html"=>"http://search.cpan.org/search?query=XML%3A%3ALibXML&mode=module");
foreach my $homepkg (keys %GLOMPED){
  foreach my $subpkg (@{$GLOMPED{$homepkg}}){
    $REDIRECT{"LaTeXML/$subpkg.html"} = "LaTeXML/$homepkg.html"; }}
our @MISSED = (qw(latexml latexmlpost LaTeXML));

print "Generating perldocs in $PDDEST\n";
process_dir("$LOC/lib");
process_dir("$LOC/bin");

unlink('pod2htmd.tmp');
unlink('pod2htmi.tmp');

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sub process_dir {
  my($dir)=@_;
  my %pods = Pod::Find::pod_find($dir);
  while(my($podfile,$object) = each %pods){
    my $file = $podfile;
    $file =~ s|^\Q$dir/\E||;
    my @subdir = split('/',$file);
    my $name = pop(@subdir);
    my $type='Command';
    if   ($name =~ s/\.pm$//)   { $type = "Module"; }
    elsif($name =~ s/\.ltxml$//){ $type = "Package Implementation"; }
    my $toroot = join('/',map("..",@subdir)) || '.';
    my $htmlfile= join('/',@subdir,$name).".html";

    print "$object => $htmlfile\n";

    foreach my $i (-1..$#subdir){
      my $dir = join('/',$PDDEST,@subdir[0..$i]);
      mkdir($dir) unless -d $dir; }

    pod2html(
	     "--css=../$toroot/latexml.css",
	     "--podroot=$dir",
	     "--title=$object documentation",
	     "--infile=$podfile",
	     "--outfile=$PDDEST/$htmlfile",
	     "--htmldir=$PDDEST",
	     "--norecurse",
	    ); 
    # Hmm, annoying pattern (or my usage thereof).
    open(HTML,"$PDDEST/$htmlfile");
    local $/=undef;
    my $doc = <HTML>;
    close(HTML);
    # MUNGY Rewriting!!!
    $doc =~ s/\>the\s+([\w\:]*)\s+manpage\</\>$1\</g;
    $doc =~ s|<(/?)h(\d)>| {"<$1h".($2+1).">"}|ge;
    $doc =~ s|<!-- INDEX BEGIN -->|<div class="nav"><b>$object documentation</b>|;
    $doc =~ s|<!-- INDEX END -->|</div><div class="main"><h1><tt>$type: $object</tt></h1>|;
    $doc =~ s|</body>|</div></body>|;
    $doc =~ s|<hr\s*/>||g;
    $doc =~ s|<p>\s*</p>||g;
    foreach my $child (keys %REDIRECT){
      $doc =~ s/$child/$REDIRECT{$child}/g; }
    foreach my $missed (@MISSED){
      $doc =~ s|<em>$missed</em>|<a href='$toroot/$missed.html'>$missed</a>|g; }
    open(HTML,">$PDDEST/$htmlfile");
    print HTML $doc;
    close(HTML);
  }}
#======================================================================
