#!/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";

#
# 前文の処理：行頭に Q1. などが出て来るまでの処理
#
while (<>) {
    $_=&general_filter($_); # HTML 特殊文字の変換
    chop;
#    last if (/^(Q\d+)\./); # 行頭に Q1. などがあったら抜ける
    last if (/^#####/); # 行頭に '#####' があったら抜ける
    print "$_\n";
}
print "<HR>\n";
print "$_\n";

while (<>) {
    $_=&general_filter($_); # HTML 特殊文字の変換
    chop;
#    last if (/^(Q\d+)\./); # 行頭に Q1. などがあったら抜ける
    last if (/^\s*$/); # 行頭にもなかったら抜ける
    print "$_\n";
}

#
# FAQ 目次部の処理
#
#$q_num=$1 if /^(Q\d+)\./;
if (/^(\s*(\d+(\.\d+)+\))\s*)/) {
    $q_num=$2;
    $faq{$_}++;
    print "$`$1<A HREF=\"#$q_num\">$'</A>\n"; # FAQ 本体への参照生成
}

while (<>) {
    $_=&general_filter($_); 
    chop;
#   last if (!/^(Q\d+)\./); # 行頭に Q1. などがなくなったら抜ける
    last if (/^#####/); # 行頭に '#####' があったら抜ける
    unless (/^(\s*(\d+(\.\d+)+\))\s*)/) {
	print "$_\n";
	next;
    }
    $q_num=$2;	
    last if ($faq{$_}!=0); 
    $faq{$_}++;
    print "$`$1<A HREF=\"#$q_num\">$'</A><BR>\n"; # FAQ 本体への参照生成
}

#
# 目次と本文との間にある文章の処理
#
#print "<PRE>\n";
#print "<HR>";
#while (<>) {
#    $_=&general_filter($_);
#    chop;
#    last if (/^(Q\d+)\./); # 行頭に Q1. などがあったら抜ける
#    print "$_\n";
#}

#
# FAQ 本体の処理
#
#$q_num=$1 if (/^(Q\d+)\./);
print "<HR>\n";
if (/^\*\*+\s*(\d+(\.\d+)+\))/) {
    $q_num=$1;
	print "<A NAME=\"$q_num\">"; # ラベル生成
    print "$_";
    print "</A>\n";
} else {
    print "$_\n";
}

while (<>) {
    $_=&general_filter($_);
    chop;
#    if (!/^(Q\d+)/) {
#	s/((Q\d+)\.)/<A HREF="#\1">\1<\/A>/g; # FAQ 参照生成
#	print "$_\n";
#	next;
#    }
    if (!/^\*\*+\s*(\d+(\.\d+)+\))/) {
	s/(\d+(\.\d+)+\))/<A HREF="#\1">\1<\/A>/g; # FAQ 参照生成
	print "$_\n";
	next;
    }
	       
    $q_num=$1;
    print "<A NAME=\"$q_num\">"; # ラベル生成
    print "$_";
    print "</A>\n";
}

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

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

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