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

# 
# link2html: 
# 
#     Apr. 4,'96. OSHIRO Naoki.
# 
#     $Log: link2html,v $
#     Revision 0.2  1996/05/14 09:10:47+09  oshiro
#     *** empty log message ***
#
#
#     [1996/04/26]
#       ・<PRE> から <UL> に変更しインデントに対応した．
#       ・出力結果にヘッダを付加した．
#     [1996/05/14]
#       ・行頭空白＋'#' をコメントとして扱う．
#     [1996/08/14]
#       ・'|' 記号による無改行連結．
#     [1996/08/16]
#       ・複数階層のインデント上げに対応．
#     [1996/10/26]
#       ・ラベル処理での行頭空白＋ '#' 無視を追加修正
#     [1996/10/28]
#       ・ラベル処理での日付無視を追加修正
#       ・/^\s*-+\s*$/ を <HR> 表示
#     [1997/06/23]
#       ・連結リンク '||' の処理で非 URL があった場合に
#         $item を更新していなかったのを修正
#       ・URL 認識で mailto: を追加
#     [1997/06/25]
#       ・6/23 の $item 更新をさらに修正
#       ・リンク部はそのまま HTML で出力し，大枠は tips 形式で
#         出力するモード (-t) を追加
#     [1997/06/26]
#       ・$next_item の初期化忘れを修正
#     [1997/06/29]
#       ・ラベルチェックの空白数に誤りがあったのを修正．
#     [1997/09/17]
#       ・認識 URL に https を追加．
#     [1997/11/07]
#       ・任意インデントでの --- で <HR> を出力するようにした．
#     [1997/11/28]
#       ・memo2tips を参考に独自の画像タグ img: を処理するようにした．
#     [1998/03/27]
#       ・連結表示時に $next_item をきちんとクリアするようにした．
#

#
# ToDo:
#   [1998/02/14]
#   ・段落最後にラベルなしの URL があるとそれは処理されん．なんで？
#   [1997/06/29]
#   ・@次のようになっていると「学振」が "bsghost.html" へラベル
#     と見なされてしまうよ．．．
#
#     ex)
#     攻殻機動隊
#     [1996/03/14]
#     ||http://www.hollywood.com/movies/ghost/text/credits.ht
#     ||http://www.hollywood.com/movies/ghost/bsghost.html
#     学振
#
#    -->行頭空白のチェックがうまくいってないのか？
#      -->空白が同じ場合もラベルとするのがうまくいっていない部分があった．
#         これを修正し解決．
#

#$debug=1;

require "getopts.pl";
require "/home/grp1/oshiro/bin/forhtml.pl";

&Getopts("tc:"); # t: tips format, c: back color
$bg_color="#ffefdb";
$bg_color=$opt_c if $opt_c;

$com_name=&basename($0);
$author="Oshiro Naoki";
$email="n-oshiro\@tec.u-ryukyu.ac.jp";
$revision='$Revision: 0.2 $';
($ver)=$revision=~/:\s*(.+)\s*\$/;

$_=<>;
while (/^#/ || /^\s*$/) {last if /^#\s*FORHTML:\s*/i; $_=<>;}
if ($opt_t) {
    print;
} else {
    print "<!-- This document produced by $com_name -->\n";
    print "<!-- -->\n";
    print "<!--     $com_name Ver.$ver was create by $author -->\n";
    print "<!--     E-mail: $email -->\n";
    &forhtml($_);
}
&print_header("BGCOLOR=\"$bg_color\"") unless ($opt_t);

$spc_pre[0]="";
$item="<LI>";
$next_item="<LI>";
while (<>) {
    chop;
    s/\t/        /g;
    &imgtag;

    #
    # 表題処理
    #
    next if (m#^\s*\[\d{4}/\d{2}/\d{2}\]#); # 日付は無視
    next if (/^\s*#/);                     # 行頭 '#' はコメントで無視
    $item="||" if (s/^(\s*)\|+/$1/);   # 行頭が '|' なら連結表示
    unless (/^(\s*)((ftp|telnet|https?|file|href|mailto|news):.+)(\s*)$/) {
        # URL 'http:' でなければ
print "!url:$_\n" if $debug;
	/^(\s*)(.+)\s*$/;
	($spc, $label)=($1, $2);
        if ($spc=~/^\s+/) { # 行頭に空白があれば
	    $spc=&indent_check($spc, *spc) unless ($item eq "||");
	    if ($label=~/^\s*-+\s*$/) {
		print "$spc<HR>\n";
	    } else {
		print "$spc$item$label\n";
	    }
	    $item=$next_item; # [1997/06/23] OSHIRO Naoki. added 1 line.
	} else {
            print "$spc[0]" . "</UL>" x $indent . "\n" if ($indent > 0);
	    $item="<LI>"; # [1997/06/25] OSHIRO Naoki. added 1 line
	    if (/^-+\s*/) {
		if ($opt_t) {
		    print "$_\n";
		} else {
		    print "<HR>\n";
		}
	    } else  {
		if ($opt_t) {
		    print "$_\n";
		} else {
		    print "<H3>$_</H3>\n";
		}
	    }
            @spc='';
	    $indent=0;
	}
	next;
    }
print " url:$_\n" if $debug;

    #
    # URL (http:) 処理
    #
    ($spc, $url)=($1, $2); # URL(HTTP) 取得
    $spc=&indent_check($spc, *spc) unless ($item eq "||");
    last if (eof);
print "[Get URL]:$url\n" if $debug;

    # 指定ファイルがカレントディレクトリにあるならば file: を除く
    $url=~s#^file:([^/])#$1#; 

    #
    # ラベル処理
    #
    do {
	# 行頭 "#" である
        # 日付である
        # ならばラベルでない
	$_=<>; # 次の行を取得
	last if (eof);
	s/\t/        /g;
	&imgtag;
    } while (/^\s*#/ || /^\s*\[\d{4}\/\d{2}\/\d{2}\]/); # テスト中
    $keep=$_;
    $next_item="<LI>";
    $next_item="||" if (s/^(\s*)\|+/$1/); # 行頭が '|' なら連結表示
    if (/^(\s*)http:/ 
	|| !/^$spc\s/ # add "\s" for correction. [1997/06/29]
	|| ($next_item eq "||")
	) { 
print "!label:$_" if $debug;
        # 'http:' である
	# 行頭の空白が $spc と同じ，または，より小さい
	# 行頭 "||" である
        # ならばラベルでない
	print "$spc$item<A HREF=\"$url\">" 
	    . &basename($url)
	    . "</A>\n"; # $url のベースをラベルとして表示
	$spc=&indent_check($spc, *spc) unless ($item eq "||");
	$item=$next_item;
        $next_item="<LI>"; # [1998/03/27]
#	$_=$keep;
	redo; # 取り込んだ行を処理しなおす
    }
print " label:$_" if $debug;
    s/^\s*(.+)\s*/\1/; # ラベル取得
    print "$spc$item<A HREF=\"$url\">$_</A>\n";
    $spc=&indent_check($spc, *spc) unless ($item eq "||");
    $item=$next_item;
}
print "</UL>" x $indent . "\n" if ($indent>0); # 最後にラベルを閉じる
@spc='';
$indent=0;

&print_footer unless ($opt_t);

sub indent_check {
    local($spc, *spc_pre)=@_;
    if ($debug) {
	print "spc:${spc}x\n";
	foreach $_ (@spc_pre) {
	    print "pre:${_}x\n";
	}
    }
    for (;;) {
	return $spc_pre[0] if ($spc eq $spc_pre[0]); # 同じ深さなら何もしない．
	if ($spc gt $spc_pre[0]) {       # 行頭の空白を以前のものと比較して大きければ
	    print "$spc_pre[0]<UL>\n"; # <UL> を表示して
	    unshift(@spc_pre, $spc);
	    $indent++;            # インデントレベルを上げる
	    return $spc[0];
	} elsif ($indent>0) {     # そうでなく，さらにインデントレベルが正なら
	    print "$spc_pre[0]</UL>\n";  # </UL> で閉じて
	    shift(@spc_pre);
	    $indent--;            #インデントレベルを下げる
	}
    }
}

sub imgtag {
    local($spc, $file, $label);
    # 独自の img: タグを追加 [1997/11/28] OSHIRO Naoki.
    if (/(\s*)img:(.+\.(jpg|gif))\s(.*)\s*$/) {
	chop;
	($spc, $file, $label)=($1, $2, $4);
	if ($label=~/^\s*$/) {
	    $label=$file;
	    $_="";
	} else {
	    $_=$label;
	}
	$_.="$spc<IMG SRC=\"$file\" BORDER=\"0\" ALT=\"$label\">";
    } else {
	s#img:(.+\.(p[pgb]m))\s#<A HREF="$1">img:$1</A>#g;
    }
}

# major-mode: perl
