# -*- Mode:Perl; -*-
# Time-stamp: <2001/06/23 (Sat) 02:08:55 m-hase@ceres.dti.ne.jp>

################################################################
### Patch for Diary.pm
################################################################
package HNS::Diary;
use vars qw($RawMode);          # added
$RawMode = 0;   ### [0:Normal 1:RAW] # added

use vars qw($RTMode @RTBuf);            
$RTMode = 0;    ### [0:Normal 1:Push 2:Convert]
@RTBuf  = ();

sub HnfPrint ($){
    my $hnf_ = shift;

    ### RAW
    if( $hnf_ =~ /^<\!--RAW START-->/ ) { $RawMode = 1;}
    if( $hnf_ =~ /^<\!--RAW END-->/ ) { $RawMode = 0;}
    if( $RawMode == 1 ){
        $hnf_ =~ s/&amp;/&/g;
        $hnf_ =~ s/&lt;/</g;
        $hnf_ =~ s/&gt;/>/g;
    }
#      $hnf_ =~ s/\\&amp;/&/g;             # \& で & を表示可能にする

    ### RT
    if( $hnf_ =~ /^<\!--RT START-->/ ) { $RTMode = 1; @RTBuf=(); return; }
    if( $hnf_ =~ /^<\!--RT END-->/ ) { $RTMode = 2; }
    if( $RTMode == 1 ) {
        push(@RTBuf, $hnf_);
        return;
    } elsif( $RTMode == 2 ){
        $hnf_ = HNS::Hnf::Command::RT::rt2html(@RTBuf);
        $RTMode = 0;
    }

    if ($HNS::System::Caching) {
        print X $hnf_;
    }
    else {
        $HNS::Diary::Hnf .= $hnf_;
    }
}


################################################################
### Append for Command.pm
################################################################
package HNS::Hnf::Command::New;;
$AllowCommands = [ $HNS::Hnf::Command::New::AllowCommands,'RT'];

package HNS::Hnf::Command::Sub;
$AllowCommands = [ $HNS::Hnf::Command::Sub::AllowCommands,'RT'];

################################################################
### RT
package HNS::Hnf::Command::RT;
use vars qw(@ISA $Template $EndTemplate $NumAttr $IsOneline $AllowCommands
            $IsBeginSection $CountName $OmittableEnd);
@ISA = qw(HNS::Hnf::Command::Cite);
$AllowCommands = [''];
$Template = "<!--RT START-->\n";
$EndTemplate = "<!--RT END-->\n";
$OmittableEnd = 1;



sub rt2html(@){

    my @lines = @_;
    
    my @value_align=("left", "center", "right");
    my $caption    = "";
    my $delimiter  = ",\t";             # デリミタ

    my $tbl_width  = 1;
    my $tbl_height = 0;
    my $tbl_header = -1;                # THEAD領域の行数
    my $tbl_body   = -1;                # TBODY領域の行数

    my %style_table;    # <TABLE> 追加属性
    my %style_thead;    # <THEAD> 追加属性
    my %style_tindex;   # <TBODY> 追加属性(見出し部)
    my %style_tbody;    # <TBODY> 追加属性(本体)

    my $theme   = 0;
    my $status  = 0;    # 0:config 1:head&body 2:body
    my $x;
    my $y;
    my $line;

    ### default
    $style_table{'border'} = 1;

    ### read
    foreach $line (@lines){
        if( $line =~ /^#/ ){
            next;
        }
        if( $status == 0 ){     # ConfigBlock
            if( $line =~ /^\s*caption\s*=\s*(.*)\s*$/i ){
                $caption = $1;
                next;
            }
            if( $line =~ /^\s*delimiter\s*=\s*(.*)\s*$/i ){
                $delimiter = $1;
                next;
            }
            if( $line =~ /^\s*(table|thead|tindex|tbody)_(\w+)\s*=\s*(\w+)\s*/i ){
                my $type  = $1;
                my $key   = $2;
                my $value = $3;
                if( $type =~ /table/ ){ $style_table{$key} = $value;}
                if( $type =~ /thead/ ){ $style_thead{$key} = $value;}
                if( $type =~ /tindex/){ $style_tindex{$key}= $value;}
                if( $type =~ /tbody/ ){ $style_tbody{$key} = $value;}
                next;
            }
            if( $line =~ /^\s*theme\s*=\s*(\d+)\s*$/i ){
                $theme = $1;
                next;
            }
        }
        
        if( $line =~ /^\s*\n/ ){
            if( $status == 1 ){
                $status = 2;                    # thead と tbody が存在する表である             
                $tbl_header =  $tbl_height;
            }
            next;
        }

        if( $status == 0 ){ $status = 1; }
        # デリミタとその前後の空白がセパレータとなる
        my @items = split(/\s*[$delimiter]\s*/, $line);
        my $w = @items;                                         # 配列の個数
        if( $w >= $tbl_width ){
            $tbl_width = $w;
        }
        if( $w == $tbl_width ){
            my $x = 0;
            my $item;
            foreach $item (@items){
                my $align;                                      # 0:left 1:center 2:right
                $item =~ s/^(\s*)//g;
                $item =~ s/(\s*)$//g;
                if( $item =~ /^[0-9]+.?[0-9]*/ ){
                    $align = 2;                 # 数字は右詰め
                } else {
                    $align = 0;                 # 他は左詰め
                }
                $tbl_item[$x][$tbl_height] = $item;
                $tbl_align[$x][$tbl_height]= $align; # align
                $tbl_col[$x][$tbl_height] = 1;          # clospan
                $tbl_row[$x][$tbl_height] = 1;          # rowspan
                $x++;
            }
            $tbl_height++;
        }

    }
    ### 
    if( $status == 1 ){
        $tbl_header = 0;
        $tbl_body   = $tbl_height;
    } elsif( $status == 2 ){
        $tbl_body = $tbl_height - $tbl_header;
    } 

    ### parse
    for( $y=$tbl_height-1 ; $y >= 0 ; $y-- ){
        for( $x=$tbl_width-1 ; $x >= 0 ; $x-- ){
            my $tmp = $tbl_item[$x][$y];
            if( $tmp =~ /^\s*==\s*$/ ){
                if( $x > 0 ){
                    $tbl_col[$x-1][$y] += $tbl_col[$x][$y];     # 左側のセルに結合(colspan 増加)
                    $tbl_col[$x][$y] = 0;
                    $tbl_row[$x][$y] = 0;                       # このセルは消滅するので0を入れておく
                }
            }
            if( $tmp =~ /^\s*\|\|\s*$/ ){
                if( $y > 0 ){
                    $tbl_row[$x][$y-1] += $tbl_row[$x][$y];     # rowspan 増加
                    $tbl_row[$x][$y] = 0;
                    $tbl_col[$x][$y] = 0;                       # このセルは消滅するので0を入れておく
                }       
            }
        }
    }

#       ### debug
#       for( $y=0 ; $y < $tbl_height ; $y++ ){
#               for( $x=0 ; $x < $tbl_width ; $x++ ){
#                       printf("(%1d,%1d,%1d)  ",$tbl_col[$x][$y] ,$tbl_row[$x][$y] ,$tbl_align[$x][$y] );
#               }
#               print "<br>\n";
#       }
#       print "tbl_height=$tbl_height<br>\n";
#       print "tbl_header=$tbl_header<br>\n";
#       print "tbl_body=$tbl_body<br>\n";
#       print "theme=$theme<br>\n";
#       print "<hr>";
#       while( ($key, $value) = each %style_table ){
#               print "TABLE[",$key,"]=",$value,"<br>\n";
#       }
#       print "<hr>";
#       while( ($key, $value) = each %style_thead ){
#               print "THEAD[",$key,"]=",$value,"<br>\n";
#       }
#       print "<hr>";
#       while( ($key, $value) = each %style_tbody ){
#               print "TBODY[",$key,"]=",$value,"<br>\n";
#       }
#       print "<hr>";

    ### generate
    my $hnf_  ="";
    $hnf_ .= "<table";
    while( ($key, $value) = each %style_table ){
        $hnf_ .= " $key='$value'";
    }
    $hnf_ .= ">\n";
    $hnf_ .= "<caption><strong>$caption</strong></caption>\n";

    ### <THEAD>
    if( $tbl_header > 0 ){
        $hnf_ .= "<thead";
        while( ($key, $value) = each %style_thead ){
            $hnf_ .= " $key='$value'";
        }
        $hnf_ .= ">\n";
        for( $y=0 ; $y < $tbl_header ; $y++ ){
            $hnf_ .= "<tr>";
            for( $x=0 ; $x < $tbl_width ; $x++ ){
                my $item = $tbl_item[$x][$y];
                my $col  = $tbl_col[$x][$y];
                my $row  = $tbl_row[$x][$y];
                my $align= 1;
                my $tag  = "th";
                if(($col > 0)&&($row > 0)){
                    $hnf_ .= sprintf("<%s",$tag);
                    $hnf_ .= sprintf(" align='%s'",$value_align[$align]);
                    $hnf_ .= sprintf(" colspan='%d'",$col) if $col > 1;
                    $hnf_ .= sprintf(" rowspan='%d'",$row) if $row > 1;
                    $hnf_ .= sprintf(">%s</%s>",$item,$tag);
                }
            }
            $hnf_ .= "</tr>\n";
        }
        $hnf_ .= "</thead>\n";
    }

    ### <TBODY>
    $hnf_ .= "<tbody>"; 

    # 見出し
    $hnf_ .= "<colgroup";
    while( ($key, $value) = each %style_tindex ){
        $hnf_ .= " $key='$value'";
    }
    $hnf_ .= ">\n";
    
    # 本体
    $style_tbody{'span'} = $tbl_width unless exists( $style_tbody{'span'});
    $hnf_ .= "<colgroup";
    while( ($key, $value) = each %style_tbody ){
        $hnf_ .= " $key='$value'";
    }
    $hnf_ .= ">\n";

    for( $y=$tbl_header ; $y < $tbl_height ; $y++ ){
        $hnf_ .= "\n";
        $hnf_ .= "<tr>";
#               $hnf_ .= "<tr";
#               $hnf_ .= " bgcolor='$style_tbody{'bgcolor'}'" if exists( $style_tbody{'bgcolor'});
#               $hnf_ .= ">";
        for( $x=0 ; $x < $tbl_width ; $x++ ){
            my $item = $tbl_item[$x][$y];
            my $align= $tbl_align[$x][$y];
            my $col  = $tbl_col[$x][$y];
            my $row  = $tbl_row[$x][$y];
            my $tag  = "td";
#                       my $bgcolor ="";
            my $x_strong = 0;
            $x_strong = $style_tindex{'span'} if exists( $style_tindex{'span'});
            if( $x <= ($x_strong - 1) ){
                $tag   = "th";
                $align = 1;
            }
            if(($col > 0)&&($row > 0)){
                $hnf_ .= sprintf("<%s",$tag);
                $hnf_ .= sprintf(" align='%s'",$value_align[$align]);
#                               $hnf_ .= sprintf(" bgcolor='%s'",$bgcolor) if $bgcolor ne "";
                $hnf_ .= sprintf(" colspan='%d'",$col) if $col > 1;
                $hnf_ .= sprintf(" rowspan='%d'",$row) if $row > 1;
                $hnf_ .= sprintf(">%s</%s>",$item,$tag);
            }
        }
        $hnf_ .= "</tr>\n";
    }
#       $hnf_ .= "</colgroup>\n";       # <colgroup>は閉じないで良いらしい
    $hnf_ .= "</tbody>\n";
    $hnf_ .= "</table>\n";

    return $hnf_;
    
}

################################################################