# -*- Mode:Perl; -*-
# Time-stamp: <2001/06/21 (Thu) 00:02:30 m-hase@ceres.dti.ne.jp>

################################################################
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:RT 2:/RT]
@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;
    }

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

################################################################
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;
    my $tbl_body   = -1;

    my $x;
    my $y;
    my $line;
    foreach $line (@lines) {

        if( $line =~ /^#/ ) {
            next;
        }
        if( $line =~ /^\s*caption\s*=\s*(.*)\s*$/i ) {
            $caption = $1;
            next;
        }
        if( $line =~ /^\s*delimiter\s*=\s*(.*)\s*$/i ) {
            $delimiter = $1;
            next;
        }
        if( $line =~ /^\n/ ) {
            if(( $tbl_height > 0 )&&( $tbl_header == -1 )) {
                $tbl_header =  $tbl_height;
            }
            next;
        }
        my @items = split(/[$delimiter]/, $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( $tbl_header == -1 ) {
        $tbl_body = $tbl_height;
    } else {
        $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";
#       }

    ### generate
    my $hnf_  ="";
    $hnf_ .= sprintf("<table border='1'>\n");
    $hnf_ .= sprintf("<caption>%s</caption>\n",$caption);
    $hnf_ .= sprintf("<tbody>\n");

    for( $y=0 ; $y < $tbl_height ; $y++ ) {
        $hnf_ .= "\n";
        $hnf_ .= "<tr>";
        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  = $y < $tbl_header ? "th" : "td";
            my $tag;
            if( $y < $tbl_header ) {
                $tag = "th";
                $align = 1;             # 1:center
            } else {
                $tag = "td";
            }                           
            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>";
    }

    $hnf_ .= "</tbody>\n";
    $hnf_ .= "</table>\n";

    return $hnf_;
    
}