Display an outline as a nested table: Difference between revisions

Line 916:
style = "background: #ddffdd;" >(The sum of the widths of its children)</td>
</tr>
</table>
 
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
 
use strict;
use warnings;
 
my @rows;
my $row = -1;
my $width = 0;
my $color = 0;
our $bg = 'e0ffe0';
 
parseoutline( do { local $/; <DATA> =~ s/\t/ /gr } );
 
print "<table border=1 cellspacing=0>\n";
for ( @rows )
{
my $start = 0;
print " <tr>\n";
for ( @$_ ) # columns
{
my ($data, $col, $span, $bg) = @$_;
print " <td></td>\n" x ( $col - $start ),
" <td colspan=$span align=center bgcolor=#$bg> $data </td>\n";
$start = $col + $span;
}
print " <td></td>\n" x ( $width - $start ), " </tr>\n";
}
print "</table>\n";
 
sub parseoutline
{
++$row;
while( $_[0] =~ /^( *)(.*)\n((?:\1 .*\n)*)/gm )
{
my ($head, $body, $col) = ($2, $3, $width);
$row == 1 and local $bg = qw( ffffe0 ffe0e0 )[ $color ^= 1];
if( length $body ) { parseoutline( $body ) } else { ++$width }
push @{ $rows[$row] }, [ $head, $col, $width - $col, $bg ];
}
--$row;
}
 
__DATA__
Display an outline as a nested table.
Parse the outline to a tree,
measuring the indent of each line,
translating the indentation to a nested structure,
and padding the tree to even depth.
count the leaves descending from each node,
defining the width of a leaf as 1,
and the width of a parent node as a sum.
(The sum of the widths of its children)
and write out a table with 'colspan' values
either as a wiki table,
or as HTML.</lang>
{{out}}
<table border=1 cellspacing=0>
<tr>
<td colspan=7 align=center bgcolor=#e0ffe0> Display an outline as a nested table. </td>
</tr>
<tr>
<td colspan=3 align=center bgcolor=#ffe0e0> Parse the outline to a tree, </td>
<td colspan=2 align=center bgcolor=#ffffe0> count the leaves descending from each node, </td>
<td colspan=2 align=center bgcolor=#ffe0e0> and write out a table with 'colspan' values </td>
</tr>
<tr>
<td colspan=1 align=center bgcolor=#ffe0e0> measuring the indent of each line, </td>
<td colspan=1 align=center bgcolor=#ffe0e0> translating the indentation to a nested structure, </td>
<td colspan=1 align=center bgcolor=#ffe0e0> and padding the tree to even depth. </td>
<td colspan=1 align=center bgcolor=#ffffe0> defining the width of a leaf as 1, </td>
<td colspan=1 align=center bgcolor=#ffffe0> and the width of a parent node as a sum. </td>
<td colspan=1 align=center bgcolor=#ffe0e0> either as a wiki table, </td>
<td colspan=1 align=center bgcolor=#ffe0e0> or as HTML. </td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td colspan=1 align=center bgcolor=#ffffe0> (The sum of the widths of its children) </td>
<td></td>
<td></td>
</tr>
</table>
 
Anonymous user