--- twikibeta/lib/TWiki.pm 2002-12-31 02:46:02.000000000 -0500
+++ w2lwiki/lib/TWiki.pm 2003-01-16 16:37:21.000000000 -0500
@@ -2199,48 +2199,132 @@
return $result;
}
-# =========================
+# ========================
+sub initTableData {
+ my ( $tableData ) = @_;
+ %$tableData = ("rows"=>[], "cols"=>[], "rowspan"=>[]);
+}
+
+# ========================
sub emitTR {
- my ( $thePre, $theRow, $insideTABLE ) = @_;
+ my ( $row ) = @_;
+
+ my $result = "
";
+ foreach my $col (@$row) {
+ my $tag = $$col{"tag"};
+ my $attr = $$col{"attr"};
+ my $text = $$col{"text"};
+ if ($tag) {
+ $result .= "<$tag$attr> $text $tag>";
+ }
+ }
+ $result .= "
\n";
+
+ return $result;
+}
+
+# ========================
+sub doTableRowspan {
+ my ( $rows, $span, $col ) = @_;
+
+ my $numRows = scalar(@$rows);
+ $$rows[$numRows - $span][$col]{"attr"} .= " rowspan=\"$span\"";
+}
+
+# ========================
+sub flushTable {
+ my ( $tableData ) = @_;
+
+ my $rows = $$tableData{"rows"};
+ my $rowspan = $$tableData{"rowspan"};
+
+ for(my $i = 0; $i < @$rowspan; $i++) {
+ if ($$rowspan[$i] > 0) {
+ my $span = $$rowspan[$i]+1;
+ doTableRowspan($rows, $span, $i);
+ }
+ }
+
+ my $result = "";
+ foreach my $row (@$rows) {
+ $result .= emitTR($row);
+ }
+ $result .= "\n";
+ initTableData($tableData);
+ return $result;
+}
+
+# ========================
+sub processTR {
+ my ( $thePre, $theRow, $insideTABLE, $tableData ) = @_;
my $text = "";
- my $attr = "";
- my $l1 = 0;
- my $l2 = 0;
- if( $insideTABLE ) {
- $text = "$thePre";
- } else {
- $text = "$thePre ";
+ unless( $insideTABLE ) { # start a new table if necessary
+ $text .= " ";
}
$theRow =~ s/\t/ /g; # change tabs to space
$theRow =~ s/\s*$//; # remove trailing spaces
$theRow =~ s/(\|\|+)/$TranslationToken . length($1) . "\|"/ge; # calc COLSPAN
+
+ my $colnum = 0;
+ my $rowData = [];
+ my $rows = ${$tableData}{"rows"};
+
foreach( split( /\|/, $theRow ) ) {
- $attr = "";
+ my $l1 = 0;
+ my $l2 = 0;
+ my $attr = "";
+ my $colData = {};
+ my $colInc = 1;
#AS 25-5-01 Fix to avoid matching also single columns
if ( s/$TranslationToken([0-9]+)// ) { # No o flag for mod-perl compatibility
$attr = " colspan=\"$1\"" ;
+ $colInc = $1;
}
- s/^\s+$/ /;
- /^(\s*).*?(\s*)$/;
- $l1 = length( $1 || "" );
- $l2 = length( $2 || "" );
- if( $l1 >= 2 ) {
- if( $l2 <= 1 ) {
- $attr .= ' align="right"';
+ if (/^\s*\^\s*$/) {
+ $$tableData{"rowspan"}[$colnum]++;
+ } else {
+ for (my $c = $colnum; $c < ($colnum+$colInc); $c++) {
+ if ($$tableData{"rowspan"}[$c]) {
+ my $span = $$tableData{"rowspan"}[$c] + 1;
+ $$tableData{"rowspan"}[$c] = 0;
+ doTableRowspan($rows, $span, $c);
+ }
+ }
+ s/^\s+$/ /;
+ /^(\s*).*?(\s*)$/;
+ $l1 = length( $1 || "" );
+ $l2 = length( $2 || "" );
+ if( $l1 >= 2 ) {
+ if( $l2 <= 1 ) {
+ $attr .= ' align="right"';
+ } else {
+ $attr .= ' align="center"';
+ }
+ }
+ if( /^\s*(\*.*\*)\s*$/ ) {
+ $$colData{"text"} = $1;
+ $$colData{"tag"} = "th";
+ $$colData{"attr"} = $attr . " bgcolor=\"#99CCCC\"";
} else {
- $attr .= ' align="center"';
+ $$colData{"text"} = $_;
+ $$colData{"tag"} = "td";
+ $$colData{"attr"} = $attr;
}
+ $$rowData[$colnum] = $colData;
}
- if( /^\s*(\*.*\*)\s*$/ ) {
- $text .= " $1 | ";
- } else {
- $text .= " $_ | ";
- }
+ $colnum+=$colInc;
+ }
+ push @$rows, $rowData;
+ my $maxSpan = 0;
+ foreach my $span (@{$$tableData{"rowspan"}}) {
+ $maxSpan = $span if $span > $maxSpan;
+ }
+ while ($maxSpan < (@$rows-1)) {
+ $text .= emitTR(shift @$rows);
}
- $text .= "";
return $text;
}
# =========================
@@ -2487,9 +2571,10 @@
# =========================
sub getRenderedVersion {
my( $text, $theWeb, $meta ) = @_;
- my( $head, $result, $extraLines, $insidePRE, $insideTABLE, $noAutoLink );
+ my( $head, $result, $extraLines, $insidePRE, $insideTABLE, %tableData,
+ $noAutoLink, $noParagraphTag );
return "" unless $text; # nothing to do
# FIXME: Get $theTopic from parameter to handle [[#anchor]] correctly
@@ -2508,8 +2593,9 @@
$noAutoLink = 0; # PTh 02 Feb 2001: Added Codev.DisableWikiWordLinks
$isList = 0;
@listTypes = ();
@listElements = ();
+ initTableData(\%tableData);
# Initial cleanup
$text =~ s/\r//g;
$text =~ s/(\n?)$/\n\n/s; # clutch to enforce correct rendering at end of doc
@@ -2606,17 +2692,21 @@
# Table of format: | cell | cell |
# PTh 25 Jan 2001: Forgiving syntax, allow trailing white space
if( $_ =~ /^(\s*)\|.*\|\s*$/ ) {
- s/^(\s*)\|(.*)/&emitTR($1,$2,$insideTABLE)/e;
+ s/^(\s*)\|(.*)/processTR($1,$2,$insideTABLE,\%tableData)/e;
$insideTABLE = 1;
+ $noParagraphTag = 1;
} elsif( $insideTABLE ) {
- $result .= "
\n";
+ $_ = flushTable(\%tableData) . $_;
$insideTABLE = 0;
}
# Lists and paragraphs
- s/^\s*$//o && ( $isList = 0 );
+ unless ($noParagraphTag) {
+ s/^\s*$//o && ( $isList = 0 );
+ }
+ $noParagraphTag = 0;
m/^(\S+?)/o && ( $isList = 0 );
s/^(\t+)(\S+?):\s/ $2<\/dt> /o && ( $result .= &emitList( "dl", "dd", length $1 ) );
s/^(\t+)\* / /o && ( $result .= &emitList( "ul", "li", length $1 ) );
s/^(\t+)\d+\.? ?/ /o && ( $result .= &emitList( "ol", "li", length $1 ) );