#!/usr/local/bin/perl
# -*- perl -*-
#
# dcllatex2hidx
# 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

$lang = "Japanese";

undef($refdir);
undef($curdir);
undef($aux);

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

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

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

$/='\x00';                      # Slurp in entire file
while($_ = <>) {

# 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/;
       }
}

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 %.


while (s/(\\(re)?newcommand\{[^\n]*\n)//) {
    print AUX "$1" if defined($aux);
}

s/\{\\em\s*([^}]*)\}/$1/g;
s/\{\\bf\s*([^}]*)\}/$1/g;
s/\{\\sc\s*([^}]*)\}/$1/g;
s/\{\\it\s*([^}]*)\}/$1/g;
s/\{\\tt\s*([^}]*)\}/$1/g;


if (s/(\\.?(author|date)[[{][^\n]*\n)//) {
    print AUX "$1" if defined($aux);
}

# this is dennou stuff
while (s/(\\(Dver|Dmodify|Dpath)\{[^\n]*\n)//) {
    print AUX "$1" if defined($aux);
}


# this is also for dennou stuff
s/\\begin\{tabular\}[^\n]*\n//g;
s/\\begin\{quote\}\n/\004/g;
s/\\end\{quote\}\n/\005/g;
while (s/\\.?(chapter|(sub)*section)\*?\s*\{([^}]*)\}/GALA_GALA_GALA_SEC/) {
    $ttl = $3;
    @ttl=split(/[\/()]/,$ttl);
   if(/GALA_GALA_GALA_SEC\n?([^\n\004]*\n){0,3}\\begin\{enumerate\}([^\n\004]*\n){0,3}[ \t]*\004\n?([^\n\005]*\n)/)
         { $desc=$3; $desc =~ s/^\s*//; $desc =~ s/[.,\s]*$//; }
    else { undef($desc);}
    for ($i=0;$i<=$#ttl;$i++) {
      next if $ttl[$i] =~ /^\s*$/;
      $nm  = $ttl[$i];
      $nm  =~ s/\(/\\\(/g;
      $nm  =~ s/\)/\\\)/g;
      $nm  =~ s/^\s*//;
      $ttl =~ s/$nm([\/()]|$)/<a name\=\"$nm\">$nm<\/a>$1/;
      $lbl="\[$nm\]";
      $desc=$lbl unless defined($desc);
      if (defined($aux))
        {print AUX "\\jvul_label\{$nm\}\{$thisfile\}\{$lbl\}\n"; }
      if (defined($idx))
        {print IDX "\\jvul_label\{$nm\}\{$thisfile\}\{$desc\}\n"; }
    }
    s/GALA_GALA_GALA_SEC//;
}

s/(\\end\{.?figure\})/\001$1/g;
$count = 1;

while (s/(\\begin\{.?figure\}[^\001]*\001\\end\{.?figure\})//) {
   $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/;
}

# tables

s/(\\end\{.?table\})/\001$1/g;
$count = 1;
while (s/(\\begin\{.?table\}[^\001]*\001\\end\{.?table\})//) {
   $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;
}

# Labels and refs outside

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

}
__END__

