#                                                         -*- Perl -*-
# Copyright (C) 1999, 2000  Motoyuki Kasahara
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#

require 5.005;

use English;
use FileHandle;

sub iso2022jp_to_eucjp {
    my ($line) = shift;
    my ($f) = sub {my $l = shift; $l =~ tr/\041-\176/\241-\376/; return $l;};
    $line =~ s/\033\$B(([\041-\176]{2})*)\033\(B/&$f($1)/eg;
    return $line;
}

sub eucjp_to_iso2022jp {
    my ($line) = shift;
    my ($f) = sub {my $l = shift; $l =~ tr/\241-\376/\041-\176/; return $l;};
    $line =~ s/(([\241-\376]{2})*)/"\033\$B".&$f($1)."\033\(B"/eg;
    return $line;
}

if (@ARGV != 1) {
    die "usage: $0 HTML-file-name\n";
}

$filename = shift;
$handle = FileHandle->new();
if (!$handle->open($filename)) {
    die "$0: failed to open the file, $ERRNO: $filename\n";
}

for (;;) {
    $_ = $handle->getline();
    if (!defined($_)) {
	last;
    }
    $_ = iso2022jp_to_eucjp($_);

    if (/^<BODY>/) {
	last;
    } elsif (/^<TITLE>/) {
	($title) = /^<TITLE>(.*)<\/TITLE>/;
	$title = eucjp_to_iso2022jp($title);
    }
}

print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n";
print "    \"http://www.w3.org/TR/html4/strict.dtd\">\n";
print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>$title</TITLE>\n";
print "</HEAD>\n";
print "<BODY>\n";
print "<H1>$title</H1>\n";
print "<UL>\n";

$curlevel = 1;

for (;;) {
    $_ = $handle->getline();
    if (!defined($_)) {
	last;
    }
    chomp;
    $_ = iso2022jp_to_eucjp($_);

    if (($level) = /^<H([1-6])>/) {
	($tag, $head) = /<A NAME=\"([^\"]+)\">(.*)<\/A>/;
	while ($level < $curlevel) {
	    print "</UL>\n";
	    $curlevel--;
	}
	while ($curlevel < $level) {
	    print "<UL>\n";
	    $curlevel++;
	}
	$head = eucjp_to_iso2022jp($head);
	print "<LI><A HREF=\"$filename#$tag\">$head</A>\n";
    }
}

for ($i = $curlevel; 1 <= $i; $i--) {
    print "</UL>\n";
}
print "</BODY>\n";
print "</HTML>\n";
