#!/usr/local/bin/perl5


$depth = 0;
$section = 1;
$note = 1;
$abstract = 0;
$current = fh000;
$tag = "content000";

sub o2html {
local($current) = @_;
%content = ();

print $fh <<RogueRogue;
<html>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=x-euc-jp">
<!--Æþ¸ý--->
RogueRogue

line: while (<$current>) {
    chop;	# strip record separator
    if(!/^</) {
	s/&/\&amp;/g;
	s/</\&lt;/g;
	s/>/\&gt;/g;
	s%href="([^"]+")\s*([^\s]*)%<a href="$1">$2</a>%g;
    }
    if( (!/^\s/) && (!/^$/) && $pre) {
	print $fh "</pre>\n";
	$pre = 0;
    }
    if (/^-*input: (.*)/) {
	$fdbase = $current; $fdbase ++;
	open($fdbase,"<$1");
	push(@depend,$1);
#	print STDOUT "$1 ";
	do o2html($fdbase);
	next line;
    }
    if (/^-title:/) {
	$title = substr($_, 8, length($_) - 7);
	&title($title);
	next line;
    }
    if (/^--*date:/) {
	for ($tmp = 1; substr($_, $tmp, 1) eq '-'; $tmp++) {
	    ;
	}
	$tmp += 5;
	$day = substr($_, $tmp, length($_) - $tmp + 1);
	next line;
    }
    if (/^--*number:/) {
	for ($tmp = 1; substr($_, $tmp, 1) eq '-'; $tmp++) {
	    ;
	}
	$tmp += 7;
	$number = substr($_, $tmp, length($_) - $tmp + 1);
	next line;
    }
    if (/^--*note:/) {
	$note = 2;
	printf $fh "<quote>\n";
	next line;
    }
    if (/^--*author:/) {
	for ($tmp = 1; substr($_, $tmp, 1) eq '-'; $tmp++) {
	    ;
	}
	$tmp += 7;
	$author = substr($_, $tmp, length($_) - $tmp + 1);
	printf $fh "<author>$author</author>\n";
	
	next line;
    }
    if (/^--*abstract:/) {
	$abstract = 1;
	printf $fh "<quote>\n";
	next line;
    }
    if (/^-/) {
	if ($abstract == 1) {
	    printf $fh "</quote>\n";
	    $abstract = 0;
	}
	if ($note) {
	    if ($note == 2) {
		printf $fh "</quote>\n";
	    $note = 0;
	    }
	}
	for ($depth = 1; substr($_, $depth, 1) eq '-'; $depth++) {
	    ;
	}
	$section_title = substr($_, $depth, length($_) - $depth + 1);
	if(! defined($title)) {
	    &title($section_title);
	}
	print $fh "<hr>\n";
	print $fh "<h$depth><a name=\"",$tag, "\">",$section_title,
	      "</a></h",$depth,">\n";
	$section++;
	$content{$tag} = " " x $depth . $section_title;
	$tag++;
	next line;
    } elsif (/^\s/) {
	if(! $pre) {
	    print $fh "<pre>\n";
	    $pre = 1;
	}
	$paragraph = 0;
	print $fh $_,"\n";
    } elsif (/^$/) {
	if(! $pre && ! $paragraph) {
	    print $fh $_,"<p>\n";
	    $paragraph = 1;
	}
    } elsif (!/^-/) {
	print $fh $_,"\n";
	$paragraph = 0;
    }
  }
    if (defined $section_title) {
	print $fh "<h2><a name=\"content\">Content</h2>\n";
	print $fh "<ol>\n";
	for (sort keys %content ) {
	    print $fh "<li><a href=\"#$_\">",$content{$_},"</a>\n";
	}
	print $fh "</ol>\n";
    }

    printf $fh "</body></html>\n";
}

foreach $arg (@ARGV) {
  $fdbase = $current; $fdbase ++;
  open($fdbase,"<$arg") || die("$arg cannot open");
  $out = $arg;
  $out =~ s/\.ind$//;
  $out =~ s/\.index$//;
  $out .=  ".html";
  $fh = "out00"; 
  if($arg eq $out) {
      $out .= ".html";
  }
  open($fh,">$out"); 
  do o2html($fdbase);
}

sub title {
	$title = $_[0];
	print $fh "<head>\n";
	print $fh "<title>$title</title>\n";
	print $fh "</head>\n";
	print $fh "<body>\n";
	print $fh "<h1>$title</h1>\n";
	print $fh "<a href=#content>content</a><br>\n";
	print $fh "<nr>\n";
	
}

# end
