#!/usr/local/bin/perl -- # -*- Perl -*-

# 
# faq2html.pl: 
# 
#     Nov.20,'95. OSHIRO Naoki <oshiro@robotics.me.es.osaka-u.ac.jp>.
# 
#     $Log:$
#

$faq2html_ver="0.2";
$date=&ctime(time);
chop $date;

#
# HTML ヘッダの出力
#
print "<!-- -->\n";
print "<!-- Converted with faq2html ver.$faq2html_ver -->\n";
print "<!-- -->\n";
print "<!--   by OSHIRO Naoki (oshiro@robotics.me.es.osaka-u.ac.jp) -->\n";
print "<!-- -->\n";
print "<!-- Generated at $date -->\n";
print  "\n";
print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>\n";
print "\n";
print "</TITLE>\n";
print "</HEAD>\n\n";
print "<BODY>\n";

print "<PRE>\n";

#
#  目次の作成
#
$tmpfile="/tmp/faq2html$$";
open(TMP, ">$tmpfile") || die "Fatal: Cannot open tmp file '$tmpfile'.\n";
while (<>) {
    print TMP;
}
close(TMP);

open(TMP, "<$tmpfile") || die "Fatal: [2] Cannot open tmp file '$tmpfile'.\n";
while (<TMP>) {
    $_=&general_filter($_);
    if (/^(\d+)章\.?\s*/) {
	$sec=$1;
	$secnum.="$sec ";
	s/\s*$//;
	$sec{$sec}=$_;
    }
    if (/^(\d+\.\d+):/) {
	$ent=$1;
	$entnum[$sec].="$ent ";
	s/(:\s)\s*/\1/;
	s/\s*$//;
	$ent{$ent}=$_;
	while (<TMP>) {
	    last if (/^A:/);
#	    s/^\s*//;
	    s/\s*$//;
	    $ent{$ent}.="\n$_";
	}
	$ent{$ent}=~s/\s*$//;
    }
}
seek(TMP, 0, 0); # 読み戻し

#
# FAQ 本体の処理
#
while (<TMP>) {
    last if /^\d+章/;
    $_=&general_filter($_);
    $_=&tag_produce($_);
    print;
}
$_=&general_filter($_);
$_=&tag_produce($_);
$first_sec=$_;
print "<HR>\n";
foreach $sec (split(' ', $secnum)) {
    print "<A HREF=\"#sec$sec\">$sec{$sec}</A>\n";
    foreach $ent (split(' ', $entnum[$sec])) {
	$_=$ent{$ent};
	~/^\d+\.\d+:/;
	print "<A HREF=\"#ent$ent\">$&</A>$'\n";
    }
    print "<P>\n";
}
print "<HR>\n";
print $first_sec;
while (<TMP>) {
    $_=&general_filter($_);
    $_=&tag_produce($_);
    print;
}
print "<HR>\n";
unlink($tmpfile);

#
# HTML フッタの処理
#
print "</PRE>\n";
print "<HR>\n\n";

print "</BODY>\n";
print "</HTML>\n";

#-----------------------

sub tag_produce {
    local($_)=@_;

    s/([\d>]と)(\d+\.\d+)/\1<A HREF=\"#ent\2\">\2<\/A>/g;
    s/([\d>]と)(\d+\.\d+)/\1<A HREF=\"#ent\2\">\2<\/A>/g; # need twice?
    s/([\d>], )(\d+\.\d+)/\1<A HREF=\"#ent\2\">\2<\/A>/g;
    s/([\d>], )(\d+\.\d+)/\1<A HREF=\"#ent\2\">\2<\/A>/g; # need twice?
    s/[質問]+(\d+\.\d+)/<A HREF=\"#ent\1\">$&<\/A>/g; # '質?問' の正規表現は使えない？
    $_="<A NAME=\"sec$1\">$&</A>$'" if (s/^(\d+)章\.?//);
    $_="<A NAME=\"ent$1\">$&</A>$'" if (s/^(\d+\.\d+)://);

    return $_;
}

#-----------------------

#
# HTML での特殊文字を変換（例:& → &amp, < → &lt）
#
sub general_filter {
    local($_)=@_;
    s/&/&amp;/g;
    s/</&lt;/g;
    s/>/&gt;/g;
    s/"/&quot;/g;
    return $_;
}

#
# 時間の取得
#
;# ctime.pl is a simple Perl emulation for the well known ctime(3C) function.
;#
;# Waldemar Kebsch, Federal Republic of Germany, November 1988
;# kebsch.pad@nixpbe.UUCP
;#
;# modified by OSHIRO Naoki [1995/12/01]

sub ctime {
    package ctime;

    local($time) = @_;
    local($[) = 0;
    local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
    local(@DoW, @MoY);

    @DoW = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
    @MoY = ('Jan','Feb','Mar','Apr','May','Jun',
	    'Jul','Aug','Sep','Oct','Nov','Dec');

    # Determine what time zone is in effect.
    # Use GMT if TZ is defined as null, local time if TZ undefined.
    # There's no portable way to find the system default timezone.

    $TZ = defined($ENV{'TZ'}) ? ( $ENV{'TZ'} ? $ENV{'TZ'} : 'GMT' ) : '';
    ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
        ($TZ eq 'GMT') ? gmtime($time) : localtime($time);

    # Hack to deal with 'PST8PDT' format of TZ
    # Note that this can't deal with all the esoteric forms, but it
    # does recognize the most common: [:]STDoff[DST[off][,rule]]

    if($TZ=~/^([^:\d+\-,]{3,})([+-]?\d{1,2}(:\d{1,2}){0,2})([^\d+\-,]{3,})?/){
        $TZ = $isdst ? $4 : $1;
    }
    $TZ .= ' ' unless $TZ eq '';

    $year += ($year < 70) ? 2000 : 1900;
    sprintf("%s %s %2d %2d:%02d:%02d %s%4d\n",
      $DoW[$wday], $MoY[$mon], $mday, $hour, $min, $sec, $TZ, $year);
}
