help2html/
#!/usr/bin/perl -w
use strict;
############################################################################################
#											   #
#	help2html.pl - Convert EW-Too style help files into a pre-formatted web page.      #
#											   #
#                          Copyright (c) 2001 Will Fischer				   #
#											   #
############################################################################################

#**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!
my $HELPDIR = 'pgplus/doc/';		# what dir is the help file in?
my $HTMLDIR = 'web/help/';		# what dir do we want the html files to go to?
my $HELP = 'help';			# name of the main talker help file
my $HTML = 'help.html';			# name for the html file where the help will be printed
my $TOC = 'toc.html';			# name for the table of contents html file
my $FRAMEPAGE = 'helpmain.html';	# name for the page that loads the previous 2 in frames
my $BGCOLOR = '#000000';		# background color of all html files
my $TEXTCOLOR = '#C0C0C0';		# default text color of all html files
my $LINKCOLOR = '#FFFFFF';		# link color of all html files
my $VLINKCOLOR = '#DDDDDD';		# visited link color of all html files
my $talker = 'MyTalker';		# talker name
#**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!

my $TEMP = 'temp.txt';			# temp file for TOC processing
my $foo = '';

open(HELP, "$HELPDIR$HELP") or die "Failed to open $HELP: $!";
open(HTML, ">$HTMLDIR$HTML") or die "Failed to open $HTML: $!";
open(TEMP, ">$TEMP") or die "Failed to open $TEMP: $!";

# This has to be done in a specific order, so that things don't get whacked out.
while (<HELP>)
{
  read_toc();		# Read all the tags the talker looks for, and print them to a list.
  convert_dynatext();	# Get the dynatext out first, in case we've got <T:blah> format.
  convert_operators();	# Replace all < and > with &lt; and &gt;, before we add any html tags
  convert_colors();	# Convert all PG color codes to html color codes
  convert_tags();	# Now convert all those tags to html: <a name="blah"> </a>
  print HTML $_;	# Print out the final result
}

close HELP;
close HTML;
close TEMP;

append_main_file();
append_toc_read();
make_toc();
make_pretty();
make_framepage();



############# SUBROUTINES ############

sub convert_dynatext
{
#**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!
# Strings of Dynatext...
  my $tname = '<T:name>';		# dynatext string for talker name
  my $taddr = '<T:addr>';		# dynatext string for talker address
  my $temail = '<T:email>';		# dynatext string for talker email
  my $tpot = '<T:pot>';			# dynatext string for slots jackpot
  my $tonline = '<T:online>';		# dynatext string for num of ppl online
  my $tstaff = '<T:staff>';		# dynatext string for num of staff online
  my $tcount = '<T:count>';		# dynatext string for num of ppl total
  my $tcountstaff = '<T:countstaff>';	# dynatext string for num of staff total
  my $mname = '<M:name>';		# dynatext string for person seeing message

# ... and their replacements.
  # $talker is defined globally, for use elsewhere
  my $address = 'mytalker.com 7777';	# talker's address and port number
  my $email = 'MyTalker@mytalker.com';	# talker's email addy
  my $pot = 4000;			# some number for jackpot
  my $online = 4;			# number of ppl online (Don't lie. :P)
  my $staff = 2;			# number of staff online
  my $count = 112;			# number of ppl total (Again, don't lie. We know better.)
  my $countstaff = 6;			# number of staff total
  my $name = 'John_Doe';		# name to use in place of $mname
#**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!**!
  my $temp = '';			# temp variable for foreach loops

# This first bit is to account for places where older dynatext is used, and a string is NOT
# to be converted. For example: &&t->name should show as &t->name. If we find such an animal,
# snip off the first & and go on to the next subroutine. Otherwise, convert the dynatext.
  foreach $temp (/&&/g)
  {
    s/&&/&/;
  }
# And now, onto the dynatext converting...
  foreach $temp (/$tname/g)
  {
    s/$tname/$talker/;
  }
  foreach $temp (/$taddr/g)
  {
    s/$taddr/$address/;
  }
  foreach $temp (/$temail/g)
  {
    s/$temail/$email/;
  }
  foreach $temp (/$tpot/g)
  {
    s/$tpot/$pot/;
  }
  foreach $temp (/$tonline/g)
  {
    s/$tonline/$online/;
  }
  foreach $temp (/$tstaff/g)
  {
    s/$tstaff/$staff/;
  }
  foreach $temp (/$tcount/g)
  {
    s/$tcount/$count/;
  }
  foreach $temp (/$tcountstaff/g)
  {
    s/$tcountstaff/$countstaff/;
  }
  foreach $temp (/$mname/g)
  {
    s/$mname/$name/;
  }
}

sub convert_operators
{
  my $temp = '';

  foreach $temp (/</g)
  {
    s/</&lt;/;		# Why does perl regexp look so NASTY?
  }
  foreach $temp (/>/g)
  {
    s/>/&gt;/;
  }
}

sub convert_colors	# More nasty regexp statements ;)
{
  my $temp = '';

  foreach $temp (/\^R/g)
  {
    s/\^R/<font color="#FF0000">/;
  }
  foreach $temp (/\^r/g)
  {
    s/\^r/<font color="#A00000">/;
  }
  foreach $temp (/\^A/g)
  {
    s/\^A/<font color="#C0C0C0">/;
  }
  foreach $temp (/\^a/g)
  {
    s/\^a/<font color="#808080">/;
  }
  foreach $temp (/\^C/g)
  {
    s/\^C/<font color="#00FFFF">/;
  }
  foreach $temp (/\^c/g)
  {
    s/\^c/<font color="#00A0A0">/;
  }
  foreach $temp (/\^Y/g)
  {
    s/\^Y/<font color="#FFFF00">/;
  }
  foreach $temp (/\^y/g)
  {
    s/\^y/<font color="#A0A000">/;
  }
  foreach $temp (/\^p/g)
  {
    s/\^p/<font color="#A000A0">/;
  }
  foreach $temp (/\^P/g)
  {
    s/\^P/<font color="#FF00FF">/;
  }
  foreach $temp (/\^G/g)
  {
    s/\^G/<font color="#00FF00">/;
  }
  foreach $temp (/\^g/g)
  {
    s/\^g/<font color="#00A000">/;
  }
  foreach $temp (/\^B/g)
  {
    s/\^B/<font color="#0000FF">/;
  }
  foreach $temp (/\^b/g)
  {
    s/\^b/<font color="#0000A0">/;
  }
  foreach $temp (/\^H/g)
  {
    s/\^H/<font color="#FFFFFF">/;
  }
# special cases
  foreach $temp (/&lt;\^N/g)	# "Turn off" code for dynatext 3.0 and higher
  {
    s/&lt;\^N/&lt;/;
  }
  foreach $temp (/\^S/g)	# italic
  {
    s/\^S/<i>/;
    if (/\^N/)		# replace the next case of ^N with </i> instead of </font>
    {
      s/\^N/<\/i>/;
    }
  }
  foreach $temp (/\^U/g)	# underline
  {
    s/\^U/<u>/;
    if (/\^N/)
    {
      s/\^N/<\/u>/;
    }
  }
  foreach $temp (/\^K/g)	# blink, but we'll use bold instead
  {
    s/\^K/<b>/;
    if (/\^N/)
    {
      s/\^N/<\/b>/;
    }
  }
  foreach $temp (/\^N/g)
  {
    s/\^N/<\/font>/;
  }
}

sub read_toc
{
  my $temp = '';
  my $gee = '';
  my @toclist = ();
  
  foreach $temp (/^:/g)
  {
    $gee = $_;
    $gee =~ s/^://g;
    $gee =~ s/,/\n/g;
    @toclist = split(/ /, $gee);
    print TEMP @toclist;
  }
}

sub append_toc_read
{
  open(TEMP, ">>$TEMP") or die "Failed to open $TEMP in append_toc_read(): $!";
  opendir(HELPDIR, "$HELPDIR") or die "Failed to open directory $HELPDIR in append_toc_read(): $!";
  my @dirlist = readdir(HELPDIR);
  closedir (HELPDIR);
  foreach (@dirlist)
  {
    if (/\.help/i)
    {
      s/\.help$//;
      print TEMP "$_\n";
    }
  }
  close TEMP;
}

sub append_main_file
{
  opendir (HELPDIR, "$HELPDIR") or die "Failed to open directory $HELPDIR in append_main_file(): $!";
  my @list = readdir(HELPDIR);
  my @pnuss = ();
  closedir (HELPDIR);
  open (HTML, ">>$HTMLDIR$HTML") or die "Failed to open $HTML in append_main_file(): $!";
  foreach (@list)
  {
    if (/\.help/i)
    {
      my $garb = $_;
      foreach ($garb)
      {
        $garb =~ s/\.help//g;
        print HTML "<hr><a name=\"$garb\"></a>\n";
      }
      open (FOO, "$HELPDIR$_") or die "Failed to open $_ in append_main_file(): $!";
      @pnuss = <FOO>;
      foreach (@pnuss)
      {
        convert_dynatext();
        convert_operators();
        convert_colors();
      }
      print HTML @pnuss;
      close FOO;
    }
  }
}

sub make_toc
{
  open (TEMP, "$TEMP") or die "Failed to open $TEMP in make_toc(): $!";
  open (TOC, ">$HTMLDIR$TOC") or die "Failed to open $TOC in make_toc(): $!";

  print TOC '<html>';
  print TOC "\n<body bgcolor=\"$BGCOLOR\" text=\"$TEXTCOLOR\" link=\"$LINKCOLOR\" vlink=\"$VLINKCOLOR\">\n";
  print TOC "\n<ul>\n";

  my @list = <TEMP>;
  my @alphalist = sort @list;
  
  foreach $foo (@alphalist)
  {
    chomp $foo;
    print TOC "<li><a target = \"help\" href = \"help.html#$foo\">$foo</a><br>\n";
  }

  print TOC "</ul>\n</html>";

  close TEMP;
  close TOC;
  unlink $TEMP;
}

sub convert_tags
{
  my $temp = '';
  my $foo = '';
  my @taglist = ();

  foreach $temp (/^:/g)
  {
    $foo = $_;
    chomp $foo;
    $foo =~ s/^://g;
    $foo =~ s/,/ /g;
    @taglist = split(/ /, $foo);
    print HTML "<hr>";
    foreach $temp (@taglist)
    {
      $_ = "<a name = \"$temp\"></a>";
      print HTML $_;
    }
  }
}

sub make_pretty
{
  open(HTML, "$HTMLDIR$HTML") or die "Failed to open $HTML in make_pretty(): $!";
  open(FOO, ">foo.txt") or die "Failed to open foo.txt in make_pretty(): $!";

  print FOO "<html>\n";
  print FOO "\n<body bgcolor=\"$BGCOLOR\" text=\"$TEXTCOLOR\" link=\"$LINKCOLOR\" vlink=\"$VLINKCOLOR\">\n";
  print FOO "<pre>\n";
  print FOO <HTML>;
  print FOO "\n</pre></html>";

  close FOO;
  close HTML;

  rename 'foo.txt', "$HTMLDIR$HTML";
}

sub make_framepage
{
  open(FP, ">$HTMLDIR$FRAMEPAGE") or die "Failed to open $FRAMEPAGE in make_framepage(): $!";

  print FP "<html>\n<head>\n<title>$talker Online Help</title>\n<\head>\n\n";
  print FP "<frameset cols = \"20\%, 80\%\">\n";
  print FP "<frame name=\"toc\" src=\"$TOC\" scrolling=\"auto\" noresize longdesc=\"Table of Contents\">\n";
  print FP "<frame name=\"help\" src=\"$HTML\" scrolling=\"auto\" noresize longdesc=\"Help Files\">\n";
  print FP "</frameset>\n\n</html>";

  close FP;
}