#!/usr/local/bin/perl
# -*- perl -*-
#
# dcllatex2html 
# based on jvulcanize : japanese enhancement of VULCANIZE
#
# Sloppy job of mostly converting LaTeX documents to HTML
#
# Modified Version by numaguti@nies.go.jp (94/02/04)
# Modified Version by numaguti@nies.go.jp (95/08/28); dennou version
#
# [Usage]
#
#   eg% jvulcanize doc.tex | nkf > doc.html
#
#   pass html+tables after when used with tabular environment:
#
#   eg% jvulcanize doc.tex | html+tables | nkf > doc.html
#
#   For English Document try:
#
#   eg% jvulcanize lang=C doc.tex | html+tables > doc.html
#
# Coyright 1994 M-J. Dominus and the Trustees of the University of
# Pennsylvania.  All rights reserved. 
#
# M-J Dominus (mjd@saul.cis.upenn.edu)
#
# Please see man page and manifesto at 
# http://www.cis.upenn.edu/~mjd/vulcanize.html.

$lang = "Japanese";

# chop($maildomain = `domainname`); 
# $maildomain =~ s/[^\.]*.(.*)/$1/;
# $maildomain= "nies.go.jp";
chop($maildomain = `hostname`);
$address = $ENV{'MAILADDRESS'};
if ($address eq "") { $address = $ENV{'USER'} . '@' . $maildomain; }
#$myname= "www-admin";
$myname  = $ENV{'MYNAME'};
#$www_home= 'http://www.gfdl.gov/~a1n/';
$www_home= $ENV{'WWW_HOME'};
undef($backto);
undef($refdir);
undef($aux);
undef($auxin);

eval "\$$1=\$2" while $ARGV[0] =~ /^(\w+)=(.*)/ && shift;

$titlicon = "\/icons\/ball.xbm" unless defined($titlicon);
$chapicon = "\/icons\/ball.xbm" unless defined($chapicon);
$secticon = "\/icons\/ball.xbm" unless defined($secticon);
$idxicon = "\/ions\/menu.xbm"   unless defined($idxicon);
$backicon = "\/icons\/back.xbm" unless defined($backicon);
if (defined($refdir)) {
   $titlicon = "$refdir/$titlicon" unless ($titlicon =~ /^\//);
   $chapicon = "$refdir/$chapicon" unless ($chapicon =~ /^\//);
   $secticon = "$refdir/$secticon" unless ($secticon =~ /^\//);
   $idxicon  = "$refdir/$idxicon"  unless ($idxicon  =~ /^\//);
   $backicon = "$refdir/$backicon" unless ($backicon =~ /^\//);
}

$rdir="";
if (defined($refdir)) {
  if ($refdir !~ /\/$/) { $rdir="$refdir/"; }
  else                  { $rdir="$refdir"; }
}

$thisfile = $ARGV[0];
$thisfile =~ s/\.tex/.html/;
$thisfile = "$curdir/$thisfile" if defined($curdir);

$revno='$Revision: 0.5j $';
$revno =~ s/^.*:\s*//;
$revno =~ s/\$\s*$//;


$/='\x00';                      # Slurp in entire file
$_ = <>;
s/\\(re)?newcommand\{\\(\w*)\}(\[[^]]*\])?\{([^\n]*)\}\n//g if defined($auxin);

open(AUXIN,"<$auxin") if defined($auxin);
$auxl = <AUXIN>;
close(AUXIN);
$_ = $auxl . $_;

# input
while (/\\.?input\{([^}]*)\}/) {
       $finc = $1; $finc =~ s/\.tex//; 
       $finc = "$refdir/$finc" if ($refdir ne "");
       print STDERR "input file: $finc.tex\n";
       if (-e "$finc.tex" && open(FINC,"<$finc.tex")) {
         local($linc)=<FINC>;
         s/\\.?input\{([^}]*)\}/$linc/;
       }
}

open(AUX,">>$aux") if defined($aux);

# this is dennou stuff
s/\% *(\\Dchapter.*)\s*\\Dchapterhead/\004 $1/;
s/\% *\\Dchapter/Dchapter/;

# comments 
s/^\%[^\n]*//g;                 # Discard comments: what's on a line
s/\n(\%[^\n]*)+//g;             # Discard comments: what's on a line
s/([^\\])\%[^\n]*\n/$1/g;       # after a line-beginning or non\'d %.

# devide paragrpah
s/\001//g;
# s/\f//g;            
s/\n\n+/\n\f/g;            

# label in
while (s/\\jvul_label\{([^}]*)\}\{([^}]*)\}\{([^}]*)\}\n//) {
      $labels{$1} = "$3";
      $ptr        = "$2#$1";
      $ptr        = "$refdir/$ptr" if defined($refdir);
      $labelptr{$1} = $ptr;
}

# newcommand, renewcommand, def (but no optional args)
while (/(\\(re)?newcommand\{([^\n]*)\}\s*\n)/) {
    print AUX $1 if defined($aux);
    s/\\(re)?newcommand\{\\(\w*)\}(\[[^]]*\])?\{([^\n]*)\}\n//;
    $tmpa = $2;
    $tmpb = $4; 
    $tmpn = $3; 
    $tmpn =~ s/\[//; $tmpn =~ s/\]//; 
    if ($tmpn eq "") { $tmps = "\\\\$tmpa\[\^\\w\]"; }
    else             { $tmps = "\\\\$tmpa\(\\{\[\^\}\]\*\\}\)\{$tmpn\}"; }
    s/\\(re)?newcommand\{\\$tmpa\}(\[[^]]*\])?\{([^\n]*)\}\n//g;
    while (/$tmps/) {
       s/($tmps)/GALA_GALA_GALA_NEW/;
       $tmpy = $1;
       $tmpx = $tmpb;
       @tmp = split(/{|}{|}/,$tmpy);
       for ($i=1; $i <= $tmpn; $i++) { 
          $tmpx =~ s/#$i/$tmp[$i]/g;
       }
       s/GALA_GALA_GALA_NEW/$tmpx/;
   }
}
#while (/\\def\\/) {
#    s/\\def\\([@\w]*)([^{]*)?\{([^}]*)\}//;
#    $tmpa = $1;
#    $tmpb = $3; 
#    $tmps = $2; 
#    $tmpn = ($tmps =~ s/#(.)/\{\(\[\^\}\]\*\)\}/g );
#    $tmps = "\\\\$tmpa\(\\{\[\^\}\]\*\\}\)\{$tmpn\}";
#    while (/$tmps/) {
#       s/($tmps)/GALA_GALA_GALA_NEW/;
#       $tmpy = $1;
#       $tmpx = $tmpb;
#       @tmp = split(/{|}{|}/,$tmpy);
#       for ($i=1; $i <= $tmpn; $i++) { 
#          $tmpx =~ s/#$i/$tmp[$i]/g;
#       }
#       s/GALA_GALA_GALA_NEW/$tmpx/;
#   }
#}

# this is dennou stuff
while (s/\\Dver\{([^}]*)\}//) { $dver=$1; }
while (s/\\Dmodify\{([^}]*)\}\{([^}]*)\}\{([^}]*)\}//) 
            { $ddate="$1/$2/$3 (Version $dver)"; $date=$ddate; }

# Handle entities \002 -> & afterwards
s/\\\&/\002amp\;/g;
s/\\</\\\002lt\;/g;
s/\\>/\\\002gt\;/g;
s/</\002lt\;/g;
s/>/\002gt\;/g;

# Real percent signs.
s/\\\%/\%/g;
s/\\\#/\#/g;

# Tex's hard space
s/\\\\\[[^]]*\]/<br>/g;                  # \\[?mm] -> <br>
s/\\\\/<br>/g;                          # \\ -> <br>

# rule
# s/\\rule(\[[^\]]*\])?\{([^}]*)\}\{([^}]*)\}/<hr>/g;
# s/\\rule(\[[^\]]*\])?\{([^}]*)\}\{([^}]*)\}/---/g;

# mu 
s/\\multicolumn\{([^}]*)\}\{([^}]*)\}//g;

# We SHOULD translate these to &nbsp;
s/([^\\])\~/$1 /g;              # Remove twiddle only when preceded by other 
s/^\~/ /g;                      # than \ or when alone at beginning of line
s/\"/\&quot;/g;                 # " -> &quot;

#### Japanese version ###

if ($lang =~ /ja/i) {

# Accents --- delete
$figapp = "";
$tabapp = "ɽ";

# Accents --- delete
s/\\\'([AEIOUYaeiouy])\s+/\&$1/g;
s/\\\"([AEIOUaeiouy])\s+/\&$1/g;
s/\\\`([AEIOUaeiou])\s+/\&$1/g;
s/\\\~([ANOano])\s+/\&$1/g;
s/\\\^([AEIOUaeiou])\s+/\&$1/g;
s/\\\,([Cc])\s+/\&$1/g;
s/\\([aoAO][Ee])\s+/\&$1/g;

# Math Symbols
s/\\alpha//g;
s/\\beta//g;
s/\\gamma//g;
s/\\delta//g;
s/\\epsilon//g;
s/\\zeta//g;
s/\\eta//g;
s/\\theta//g;
s/\\iota//g;
s/\\kappa//g;
s/\\lambda//g;
s/\\mu//g;
s/\\nu//g;
s/\\xi//g;
s/\\pi//g;
s/\\rho//g;
s/\\sigma//g;
s/\\tau//g;
s/\\upsilon//g;
s/\\phi//g;
s/\\chi//g;
s/\\psi//g;
s/\\omega//g;
s/\\varepsilon//g;
s/\\vartheta//g;
s/\\varpi//g;
s/\\varrho//g;
s/\\varsigma//g;
s/\\varphi//g;

s/\\Alpha//g;
s/\\Beta//g;
s/\\Gamma//g;
s/\\Delta//g;
s/\\Epsilon//g;
s/\\Zeta//g;
s/\\Eta//g;
s/\\Theta//g;
s/\\Iota//g;
s/\\Kappa//g;
s/\\Lambda//g;
s/\\Mu//g;
s/\\Nu//g;
s/\\Xi//g;
s/\\Pi//g;
s/\\Rho//g;
s/\\Sigma//g;
s/\\Tau//g;
s/\\Upsilon//g;
s/\\Phi//g;
s/\\Chi//g;
s/\\Psi//g;
s/\\Omega//g;

s/\\sim//g;
s/\\pm//g;
s/\\sum//g;
s/\\int//g;
s/\\partial//g;
s/\\nabla//g;
s/\\sqrt\{([^}]*)\}/$1/g;
s/\\simeq//g;
s/\\equiv//g;
s/\\rightarrow//g;
s/\\leftarrow//g;

s/\\uparrow//g;
s/\\downarrow//g;
s/\\bullet//g;
s/\^\\circ\$C//g;
s/\^\\circ//g;
s/\\leq//g;
s/\\geq//g;
s/\\neq//g;
s/\\left\(//g;
s/\\right\)//g;
s/\\left\[//g;
s/\\right\]//g;
s/\\left\\\{//g;
s/\\right\\\{//g;

# Kanji spaces and dots
s/\\ //g; 
s/([-])\, ?/$1/g;
s/([-])\. ?/$1/g;
s/([-])\) ?/$1/g;
s/ ?\(([-])/$1/g;

s/---//g;

} else {

$figapp = "Figure";
$tabapp = "Table";

# Accents---add more here as used.
s/\\\'([AEIOUYaeiouy])\s+/\&$1acute\;/g;
s/\\\"([AEIOUaeiouy])\s+/\&$1uml\;/g;
s/\\\`([AEIOUaeiou])\s+/\&$1grave\;/g;
s/\\\~([ANOano])\s+/\&$1tilde\;/g;
s/\\\^([AEIOUaeiou])\s+/\&$1circ\;/g;
s/\\\,([Cc])\s+/\&$1cedil\;/g;
s/\\([aoAO][Ee])\s+/\&$1lig\;/g;
s/\\ss/\&szlig\;/g;
s/\\aa/\&aring\;/g;
s/\\AA/\&Aring\;/g;
s/\\([Oo])\s+/\&$1slash\;/g;

# Tex's hard space
s/\\ / /g;

}

# Miscellany that we can actually handle
s/\\ldots/.../g;
s/\\cdots/.../g;
s/--/-/g;

# Other Math Symbols
s/\$//g;

# Handle Titles
s/\\documentstyle([^}]*)\}//g;
s/\\begin\{document\}//g;
s/\\end\{document\}//g;

s/\\maketitle//g;
while (s/\\.?title\[([^]]*)\]\{([^{}]*(\{[^}]*\})*[^{}]*)\}//) 
              { $title=$2 unless defined($title); 
                $dtitle=$1 unless defined($dtitle); }
while (s/\\.?author\[([^]]*)\]\{([^{}]*(\{[^}]*\})*[^{}]*)\}//)
              { $author=$2 unless defined($author); 
                $dauthor=$1 unless defined($dauthor); }
while (s/\\.?date\[([^]]*)\]\{([^{}]*(\{[^}]*\})*[^{}]*)\}//)  
              { $date=$2 unless defined($date); 
                $ddate=$1 unless defined($ddate); }

while (s/\\.?title\{([^{}]*(\{[^}]*\})*[^{}]*)\}//) 
              { $title=$1 unless defined($title); 
                $dtitle=$title unless defined($dtitle); }
while (s/\\.?author\{([^{}]*(\{[^}]*\})*[^{}]*)\}//)
              { $author=$2 unless defined($author); 
                $dauthor=$author unless defined($dauthor); }
while (s/\\.?date\{([^{}]*(\{[^}]*\})*[^{}]*)\}//) 
              { $date=$2 unless defined($date); 
                $ddate=$date unless defined($ddate); }

while (s/\\Dpath\{([^{}]*(\{[^}]*\})*[^{}]*)\}//) 
              { $dpath=$1 unless defined($dpath); }

# use typeout for Document Title
if (s/\\.?typeout\{([^}]*)\}//)   { $dtitle = $1; }

# Handle lists
s/\\begin\{enumerate\}/\<OL\>/g;
s/\\end\{enumerate\}/\<\/OL\>/g;
s/\\begin\{itemize\}/\<UL\>/g;
s/\\end\{itemize\}/\<\/UL\>/g;
s/\\begin\{description\}/\<DL\>/g;
s/\\end\{description\}/\<\/DL\>/g;

s/\\begin\{minipage\}(\[[^\]]*\])?\{[^}]*\}//g;
s/\\end\{minipage\}//g;

# items without []'s for <li>s; otherwise as <dt>[]<dd>
s/\\item\s*\[([^\]]*)\]/\<dt\>$1\<dd\>/g;
s/\\item /\<LI\> /g;

# Handle quotes
s/\\begin\{center\}/\<blockquote\>/g;
s/\\end\{center\}/\<\/blockquote\>/g;
s/\\begin\{abstract\}/\<blockquote\>/g;
s/\\end\{abstract\}/\<\/blockquote\>/g;
s/\\begin\{quotation\}/\<blockquote\>/g;
s/\\end\{quotation\}/\<\/blockquote\>/g;
s/\\begin\{quote\}/\<blockquote\>/g;
s/\\end\{quote\}/\<\/blockquote\>/g;

# Handle preformatted text
s/\\begin\{verbatim\}/\<pre\>/g;
s/\\end\{verbatim\}/\<\/pre\>/g;
s/\\begin\{verse\}/\<pre\>/g;
s/\\end\{verse\}/\<\/pre\>/g;
s/\\begin\{.?program\}/\<pre\>/g;
s/\\end\{.?program\}/\<\/pre\>/g;

# handle formatting requests
s/\\verb\+([^+]*)\+/\<tt\>$1\<\/tt\>/g; # Doesn't work unless \verb+...+

#s/\s*\{\\em\s*([^}]*)\}/ <em>$1<\/em>/g;
#s/\s*\{\\bf\s*([^}]*)\}/ <b>$1<\/b>/g;
#s/\s*\{\\sc\s*([^}]*)\}/ \U$1\E/g;
#s/\s*\{\\it\s*([^}]*)\}/ <i>$1<\/i>/g;
#s/\s*\{\\tt\s*([^}]*)\}/ <tt>$1<\/tt>/g;

s/\{\\em\s*([^}]*)\}/<em>$1<\/em>/g;
s/\{\\bf\s*([^}]*)\}/<b>$1<\/b>/g;
s/\{\\sc\s*([^}]*)\}/\U$1\E/g;
s/\{\\it\s*([^}]*)\}/<i>$1<\/i>/g;
s/\{\\tt\s*([^}]*)\}/<tt>$1<\/tt>/g;

s/\\begin\{em\}/\<em\>/g;
s/\\end\{em\}/\<\/em\>/g;
s/\\begin\{bf\}/\<b\>/g;
s/\\end\{bf\}/\<\/b\>/g;
s/\\begin\{it\}/\<i\>/g;
s/\\end\{it\}/\<\/i\>/g;
s/\\begin\{tt\}/\<tt\>/g;
s/\\end\{tt\}/\<\tt\>/g;


# Discard italic correction `\/'
s/\\\///g;

# Discard \noindent (it's hopeless)
s/\\noindent//g;


# Section headers
if (/\\.?(chapter|(sub)*section)\*?\s*\{([^}]*)\}/)
		{ $dtitle=$3 unless defined($dtitle); }

# this is also for dennou \003,\004 -> afterwards
while (s/\\.?(chapter|(sub)*section)\*?\s*\{([^}]*)\}/GALA_GALA_GALA_SECT/) {
    if    ($1 eq "chapter")    { $l=1; }
    elsif ($1 eq "section")    { $l=2; }
    elsif ($1 eq "subsection") { $l=3; }
    else                       { $l=4; }
    $ttl = $3;
    $ttl =~ s/\s*<..?>\s*//g;
    $ttl =~ s/\s*<\/..?>\s*//g;
#    @ttl=split(/[\/()]/,$ttl);
    @ttl=split(/\//,$ttl);
    for ($i=0;$i<=$#ttl;$i++) {
      next if $ttl[$i] eq "";
      $nm  = $ttl[$i];
      $nm  =~ s/\(/\\\(/g;
      $nm  =~ s/\)/\\\)/g;
      $ttl =~ s/$nm([\/()]|$)/<a name\=\"$nm\">$nm<\/a>$1/;
      $lbl="\[$nm\]";
      if (defined($aux))
        {print AUX "\\jvul_label\{$nm\}\{$thisfile\}\{$lbl\}\n"; }
    }
    s/\004\s*GALA_GALA_GALA_SECT/<H$l>\004$ttl<\/H$l>/;
    s/GALA_GALA_GALA_SECT/<H$l>\003$ttl<\/H$l>/;
}

#s/\\.?chapter\*?\s*\{([^}]*)\}/<h1>$1<\/h1>/g;
#s/\\.?section\*?\s*\{([^}]*)\}/<h2>$1<\/h2>/g;
#s/\\.?subsection\*?\s*\{([^}]*)\}/<h3>$1<\/h3>/g;
#s/\\.?subsubsection\*?\s*\{([^}]*)\}/<h4>$1<\/h4>/g;

# Remove <P> when it follows some other tag and is thereofre unnecessary
# e.g., </h2><P> --> </h2>
# This doesn't work well---fix it.
s/\>\s*\<[Pp]\>/\>/g;

# mbox
s/\\m(ake)?box(\[[^]]*\])*\{([^}]*)\}/$3/g;
s/\\parbox(\[[^]]*\])?\{([^}]*)\}//g;

#index
s/\\index\{([^}]*)\}/\<a name\=\"ind-$1\"\>/g;

# figures

s/(\\.?caption\{[^}]*\}(\{[^}]*\})?)\s*(\\?.label\{[^}]*\})/$3<br>\n$1/g;
s/\\.?caption\{([^}]*)\}(\{[^}]*\})?/$1 $2/g;
s/\\.?epsfiles?\{file=([^,}]*)\.e?ps([^}]*)\}/<img src=\"$rdir$1\.gif\">/g;
s/\\.?epsfiles?\{file=([^,}]*)([^}]*)\}/<img src=\"$rdir$1\">/g;
s/\\.?epsf((\[[^\]]*\])*)\{([^,{}]*)\.e?ps([^}]*)\}/<img src=\"$rdir$3\.gif\">/g;
s/\\.?epsf((\[[^\]]*\])*)\{([^,{}]*)([^}]*)\}/<img src=\"$rdir$3\">/g;

s/(\\end\{.?figure\})/\001$1/g;
$count = 1;
while (/\\begin\{.?figure\}/) {
   s/(\\begin\{.?figure\}[^\001]*\001\\end\{.?figure\})/GALA_GALA_GALA_FIG/;
   $tmpa = $1;
   $tmpa =~ s/\001//;
   $labels{$1} = $count;
   if ($tmpa =~ s/\\.?label\{([^}]*)\}/<a name\=\"$1\"\>$figapp$count<\/a>/)
   { print AUX "\\jvul_label\{$1\}\{$thisfile\}\{$labels{$1}\}\n" 
     if defined($aux); }
   $count += 1;
  s/GALA_GALA_GALA_FIG/$tmpa/;
  s/\\begin\{.?figure\}\[([^\]]*)\]//;
  s/\\end\{.?figure\}//;
}

# tables

s/(\\end\{.?table\})/\001$1/g;
$count = 1;
while (/\\begin\{.?table\}/) {
   s/(\\begin\{.?table\}[^\001]*\001\\end\{.?table\})/GALA_GALA_GALA_TAB/;
   $tmpa = $1;
   $tmpa =~ s/\001//;
   $tmpa =~ s/\\.?label\{([^}]*)\}/<a name\=\"$1\"\>$tabapp$count<\/a>/;
   $labels{$1} = $count;
   if ($tmpa =~ s/\\.?label\{([^}]*)\}/<a name\=\"$1\"\>$figapp$count<\/a>/)
   { print AUX "\\jvul_label\{$1\}\{$thisfile\}\{$labels{$1}\}\n" 
     if defined($aux); }
   $count += 1;
  s/GALA_GALA_GALA_TAB/$tmpa/;
  s/\\begin\{.?table\}\[([^\]]*)\]//;
  s/\\end\{.?table\}//;
}

# Labels and refs outside

while (/\<H\d\>(\003|\004)[^\n]*\<\/H\d\>\s*\\.?label\{/) {
   s/(\<H\d\>)(\003|\004)([^\n]*)(\<\/H\d\>)\s*\\.?label\{([^}]*)\}/$1<a name\=\"$5\"\>$2<\/a>$3$4/;
      $labels{$4} = "[here]";
      if (defined($aux))
        {print AUX "\\jvul_label\{$4\}\{$thisfile\}\{$labels{$4}\}\n"; }
}

#while (/\\Dchapterhead\s*\\.?label\{/) {
#     s/\\.?label\{([^}]*)\}/<a name\=\"$1\"\><img src=\"$chapicon\"><\/a>/;
#      $labels{$1} = "[here]";
#      if (defined($aux))
#        {print AUX "\\jvul_label\{$1\}\{$thisfile\}\{$labels{$1}\}\n"; }
#}

while (/\\.?label\{/) {
     s/\\.?label\{([^}]*)\}/<a name\=\"$1\"\>.<\/a>/;
      $labels{$1} = "[here]";
      if (defined($aux))
        {print AUX "\\jvul_label\{$1\}\{$thisfile\}\{$labels{$1}\}\n"; }
}

while (/\\ref\{([^}]*)\}/) {
      if (defined($labelptr{$1}))
        { s/\\ref\{([^}]*)\}/<a href\=\"$labelptr{$1}\"\>$labels{$1}\<\/a\>/; }
      else 
        { s/\\ref\{([^}]*)\}/<a href\=\"#$1\"\>$labels{$1}\<\/a\>/; }
}


# tabular, tabbing -> HTML+

s/\s*\\hline//g;
s/\\begin\{tabular\}\{.*\|.*\}\s*\n/<Tbl border>n\<th>/g;
s/\\begin\{tabular\}\{.*\}\s*\n/<Tbl>\n<th>/g;
s/\\begin\{eqnarray\*?\}\s*\n/<Tbl>\n<th>/g;
s/\<br\>\s*\n(\s*)\\end\{tabular\}/<tr>\n$1><tr>\001<\/tbl>/g;
s/\\end\{tabular\}/<tr>\001<\/tbl>/g;
s/\\end\{eqnarray\*?\}/<tr>\001<\/tbl>/g;

while (/\<Tbl/) {
   s/(\<Tbl[^\001]*\001\<\/tbl)/GALA_GALA_GALA_TBL/;
   $tmpa = $1;
   $tmpa =~ s/\001//;
   $tmpa =~ s/\<br\>/<tr>\n<th>/g;
   $tmpa =~ s/\&/<td>/g;
   $tmpa =~ s/(\s*)\n(\s*)\<td\>/$1<td>\n$2/g;
   $tmpa =~ s/\<(\/)?(tt|i|em|b)\>//g;
   $tmpa =~ s/(\<th\>)\s*/$1/g;
   $tmpa =~ s/[\t ]*(\<td\>)[\t ]*/$1/g;
   $tmpa =~ s/\s*\n\s*/\n/g;
   $tmpa =~ s/[\t ]+/ /g;
   $tmpa =~ s/\<th\>\<tr\>//g;   
   s/GALA_GALA_GALA_TBL/$tmpa/;
   s/\<Tbl/<tbl/;
}

s/\\begin\{tabbing\}\s*\n/<Tab>\n<th>/g;
s/\<br\>\s*\n(\s*)\\end\{tabbing\}/<tr>\n$1<tr>\001<\/tbl>/g;
s/\\end\{tabbing\}/<tr>\001<\/tbl>/g;

while (/\<Tab/) {
   s/(\<Tab[^\001]*\001\<\/tbl)/GALA_GALA_GALA_TAB/;
   $tmpa = $1;
   $tmpa =~ s/\001//;
   $tmpa =~ s/\n(\<th\>)?.[^\n]*\\kill\n/$1/g;
   $tmpa =~ s/\<br\>/<tr>\n<th>/g;
   $tmpa =~ s/\\\=/<td>/g;
   $tmpa =~ s/\\\002gt\;/<td>/g;
   $tmpa =~ s/(\s*)\n(\s*)\<td\>/$1<td>\n$2/g;
   $tmpa =~ s/\<(\/)?(tt|i|em|b)\>//g;
   $tmpa =~ s/(\<th\>)\s*/$1/g;
   $tmpa =~ s/[\t ]*(\<td\>)[\t ]*/$1/g;
   $tmpa =~ s/\n\s*/\n/g;
   s/GALA_GALA_GALA_TAB/$tmpa/;
   s/\<Tab/<tbl/;
}

# only deleting other staff

s/\\\w+=[-\.\w]+\s*//g;

s/\\begin\{\w*\}(\[[^{\]]*\])?(\{[^}]*\})?//g;
s/\\end\{\w*\}//g;

s/\\vspace\*?\{[-\.\w]*\}/<p>/g;
s/\\hspace\*?\{[-\.\d]*(cm|mm|pt|zw|em|en)\s*([^}]*)\}/ $2/g;
s/\\(big|med|small)skip/<p>/g;

s/\\def\\\w*\{[^}]*\}//g;
s/\\pagestyle\{[^}]*\}//g;
s/\\pagenumbering\{[^}]*\}//g;
s/\\setcounter\{[^}]*\}\{[^}]*\}//g;

# finally include
while (s/\\.?include\{([^}]*)\}/\001GALA_GALA_INC/) {
       $finc = $1; $finc =~ s/\.tex//; 
       $tfinc = $finc; $tfinc =~ s/^.*\///;
       $finc = "$refdir/$finc" if ($refdir ne "");
#      if (-e "$finc.html" && open(FINC,"<$finc.html")) {
       if (-e "$finc.html" && open(FINC,"nkf -e $finc.html|")) {
	 print STDERR "include $finc\n";
         while ($linc=<FINC>) {
           while ($linc =~ s/(^|\n)\<(H[12])\>(.*)\<\/H[12]\>//) {
               $i=$3; $i =~ s/\<[^>]*\>//g;
 s/GALA_GALA_INC/<h3><a href=\"$finc.html\#$i\">$i<\/a><\/h3>\nGALA_GALA_INC/;
   	   }
         }
	 close(FINC);
       }       
       if( !s/\001GALA_GALA_INC/<h3><a href=\"$finc.html\">$tfinc<\/a><\/h3>/)
        { s/\001//; s/GALA_GALA_INC//; }
}

s/\\.?proginput\{([^}]*)\}/<h3><a href=\"$rdir$1\">$1<\/a><\/h3>/g;

# Yah!
s/\\\w*//g;
s/([^\\])\{/$1/g;
s/([^\\])\}/$1/g;
s/\s*\n\s*\n/\n/g;
s/\001//g;
s/\002/\&/g;

# icons
s/\004/<img src="$chapicon">/g;
s/\003/<img src="$secticon">/g;

# ad hoc dennou stuff
s/\{\s*\{/<HR>/g;
s/\}\s*\}/<HR>/g;

# URL
s/(\w*:\/\/[\.\/\w]*)/<a href="$1">$1<\/a>/g;

# Things we'll never add:
# centering, subscripts, superscripts, other math formulas. 
# fonts, parshape
# everything else.
chop($modified = "Last Modified: " . `date`); 

$title = "<h1>\005$title</h1>\n" if defined($title);
$title =~ s/\005/<img src="$titlicon">/g;

$header = "<title>$dtitle</title>\n" . 
          "<!--  Generated by JVulcanize v$revno. >\n" .
          $title;

$idxfile = $thisfile;
$idxfile =~ s/\.html/-idx.html/;
if (-e $idxfile) {
	$idxto = "\n<P>\n<h2><a href=\"$idxfile\">" .	
	          "<img src=\"$idxicon\"> Index</a></h2>\n<P>\n"; 
	$header = $header . $idxto;
}



if ($backto ne "") { 
        $backto = "<P><HR>\n<h2><a href=\"$backto\">" .
	          "<img src=\"$backicon\"> Back to Main</a></h2>\n";
}                   

if ($author ne "") { $signature = "$author : $date<P>\n"; }

$trailer = "<P><HR>\n<a href=\"$ARGV\">Latex Source</a><P>\n" .
           "<HR>$signature" .
           "<ADDRESS><a href=\"$www_home\">\n" .
           "$myname &lt;$address&gt;</a></ADDRESS>\n" .
	   "<!-- hhmts start -->\n" .
           "$modified<p>\n" .
	   "<!-- hhmts end -->\n";

# $trailer = "<P><HR>$modified<p>\n<ADDRESS>\n$author\n</ADDRESS>\n";


$footnotenumber=1;
$footnotes='';
while (/\\footnote/) {
    s/\\footnote\{([^}]*)\}/ \<a href\=\"#footnote-$footnotenumber\"\>\($footnotenumber\) \<\/a\>/;
    $footnotes .= "<a name=\"footnote-$footnotenumber\"><li\> $1</a>\n";
$footnotenumber++;
}

if ($footnotes) {
    $footnotes = "<hr>\n<h1>Footnotes</h1><ol>\n" . $footnotes . "\n</ol>\n";
}

# get rid of redundant curly braces;
# make curly braces we want, normal
# s/([^\\])[{}]/$1/g;
# s/\\([{}])/$1/g;

# prepend title and header, append address.
$_ = $header .     $_ . $footnotes . $backto . $trailer;

# Insert paragrpah marks
s/\f/\<P\>\n/g;            

# Urp.
print $_;

__END__

