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

# 
# forhtml.pl: HTML formatter
# 
#     [1996/05/28] OSHIRO Naoki.
#     [1997/06/28] OSHIRO Naoki.
#       ヘッダ部でもリンクを表示するようにした．
#     [1997/06/29] OSHIRO Naoki.
#       リンク表示で改行を入れられるようにした．
# 
#     $Log:$
#

# 
# To use:
#   please use after notation on first line of list file 
#   or argment of call forhtml.
#
#   # FORHTML: title, body_title, email, [\@heading_image,] links...
#
#      links means := url, title, url, title,...
#

package forhtml;

$date=&ctime(time);
$com_name=&main'basename($0);
$title="HTML generated by $com_name";
$heading_image="";
$body_title="なんちゃらりすと";
$email="";
@links=();

sub main'forhtml {
    local($_)=@_;

    unless (/^#\s*FORHTML:\s*/i) {
        print "Error: Please HTML making infomation put first line of file.\n";
        print "       like this '# FORHTML: title, body_title, email, [\@heading_image,] links'\n";
        return 0;
    }
    
    @links=split('\s*[,]\s*', $');
    $title=shift(@links);
    $body_title=shift(@links);
    $heading_image=shift(@links) if ($links[0]=~s/^@//);
    $email=shift(@links);
    
    return 1;
}

sub main'print_header {
    local($body_opt)=@_;
    print "<!-- -->\n";
    print "<!-- $title: $body_title -->\n";
    print "<!-- -->\n";
    print "\n";
#    print "<!-- This document converted with $com_name by OSHIRO Naoki -->\n";
    print "<HTML>\n";
    print "<HEAD>\n";
    print "<TITLE>\n"; 
    print "$title\n";
    print "</TITLE>\n";
    print "</HEAD>\n";
    print "\n";
    if (!$body_opt) {
	print "<BODY>\n";
    } else {
	print "<BODY $body_opt>\n";
    }
    print "<H1>\n";
    print "<IMG SRC=\"$heading_image\" ALT=\"\">\n" if ($heading_image); 
    print "$body_title\n";
    print "</H1>\n";
    print "最終更新: [$date]<!-- #REFDATE -->\n";
    print "<DIV ALIGN=\"right\">\n";
    print "<A HREF=\"/~oshiro/copyright.html\"><I>Copyright</I></A>\n";
    print "</DIV>\n";
    print "<HR>\n";
    print "\n";
    &print_links(@links);
}

sub main'print_footer {
    print "\n<HR>\n";
    &print_links(@links);
    if ($email ne '') {
	print "<CENTER>\n";
	print "<ADDRESS>\n";
	print "Mail-to:\n";
	print "<A HREF=\"mailto:$email\">\n";
	print "$email</A>\n";
	print "</ADDRESS>\n";
	print "</CENTER>\n";
	print "\n";
    }
    print "</BODY>\n";
    print "</HTML>\n";
}

sub print_links {
    local(@links)=@_;

    if (@links) {
	print "<CENTER>\n";
	print "||\n";
	for ($i=0; $i<$#links; $i+=2) {
	    if ($links[$i]=~/\\n/) { # [1997/06/29] OSHIRO Naoki.
		print "<BR>\n||";
		$i++;
	    }
	    if ($links[$i] ne "") {
		print "<A HREF=\"$links[$i]\">\n";
		print "$links[$i+1]</A>||\n";
	    } else {
		print "$links[$i+1]||\n";
	    }
	}
	print "</CENTER>\n";
	print "<HR>\n";
	print "\n";
    }
}

sub ctime {
    package ctime;

    local($time) = @_;
    local($date);
    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("%4d/%02d/%02d", $year, $mon+1, $mday);
    $_.=" $TZ" if ($TZ);

    return $_;
}

sub main'basename {
    local($_)=@_;
    $_=$1 if m#([^/]+/)\s*$#;
    $_=$1 if m#([^/]+)(\.s?html?)?\s*$#;
    s/\.s?html?//;
    return $_;
}

1;
