# -*- Mode:Perl; -*-
# Time-stamp: <2001/06/22 (Fri) 19:14:28 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 $theme	   = 0;
    my $tbl_width  = 1;
    my $tbl_height = 0;
    my $tbl_header = -1;
    my $tbl_body   = -1;

    my $table_border = "1"; 
    my $table_width  = ""; 
    my $table_height = ""; 
    my $table_frame  = ""; 

    my $thead_bgcolor = "";

    my $tbody_span    = 0;
    my $tbody_width   = "";
    my $tbody_align   = "";
    my $tbody_bgcolor = "";

#  	my $style_table={};
#  	my $style_thead={};
#  	my $style_tbody={};

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

    foreach $line (@lines) {

	if( $line =~ /^#/ ) {
	    next;
	}
	if( $status == 0 ) {
	    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_(\w+)\s*=\s*(\w+)\s*/i ) {
		my $name  = $1;
		my $value = $2; 
		if( $name =~ /border/ ) { $table_border = $value; }
		if( $name =~ /width/ ) { $table_width = $value; }
		if( $name =~ /height/ ) { $table_height = $value; }
		if( $name =~ /frame/ ) { $table_frame = $value; }
		next;
	    }
	    if( $line =~ /^\s*thead_(\w+)\s*=\s*(\w+)\s*/i ) {
		my $name  = $1;
		my $value = $2; 
		if( $name =~ /bgcolor/ ) { $thead_bgcolor = $value; }
		next;
	    }
	    if( $line =~ /^\s*tbody_(\w+)\s*=\s*(\w+)\s*/i ) {
		my $name  = $1;
		my $value = $2; 
		if( $name =~ /span/ ) { $tbody_span = $value; }
		if( $name =~ /width/ ) { $tbody_width = $value; }
		if( $name =~ /align/ ) { $tbody_align = $value; }
		if( $name =~ /bgcolor/ ) { $tbody_bgcolor = $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";

    ### generate
    my $hnf_  ="";
#  	$hnf_ .= sprintf("<table border='1'>\n");
    $hnf_ .= "<table";
    $hnf_ .= " border='$table_border'" if $table_border ne "";
    $hnf_ .= " width='$table_width'"   if $table_width  ne "";
    $hnf_ .= " height='$table_height'" if $table_height ne "";
    $hnf_ .= " frame='$table_frame'"   if $table_frame  ne "";
    $hnf_ .= ">\n";
    $hnf_ .= sprintf("<caption><strong>%s</strong></caption>\n",$caption);

    # THEAD
    if( $tbl_header > 0 ) {
	$hnf_ .= "<thead";
	$hnf_ .= " bgcolor='$thead_bgcolor'" if $thead_bgcolor ne "";
	$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>\n";

    $hnf_ .= "<colgroup";
    $hnf_ .= " span='$tbody_span'"       if $tbody_span    ne "";
    $hnf_ .= " width='$tbody_width'"     if $tbody_width   ne "";
    $hnf_ .= " align='$tbody_align'"     if $tbody_align   ne "";
    $hnf_ .= " bgcolor='$tbody_bgcolor'" if $tbody_bgcolor ne "";
    $hnf_ .= ">\n";

    for( $y=$tbl_header ; $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  = "td";
	    my $bgcolor ="";
	    if( $x <= ($tbody_span - 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";
    $hnf_ .= "</tbody>\n";
    $hnf_ .= "</table>\n";

    return $hnf_;
    
}
################################################################