#!/usr/local/bin/perl
while (<>) {
if (m|^ ]|) {
while (! m|$|) { $_ .= <>; }
s/\n//g;
print "
\n";
do format_table($_);
print "
\n";
} else {
print $_;
}
}
sub format_table {
# On input, $_ contains:
# Header 1 | Header2 | ... | Header M |
# Data11 | Data12 | ... | Data1M |
# ...
# DataN1 | DataN2 | ... | DataNM |
#
# Extract the entries and compute the number of lines and columns
$numlines = 0;
$numcols = 0;
$border = 0;
$header = 0;
$x = 0;
$y = 0;
foreach $_ (split(/(|| | | | |<\/tbl>)/, $_)) {
if (/^$/) { next; }
elsif (//) { $border = 1; }
elsif (//i) {
if ($x > $numcols) { $numcols = $x; }
$x = 0;
$y++;
}
elsif (//) { $header = 1; }
elsif (!/(|| | | | |<\/tbl>)/) {
s|?[a-zA-Z]*>||g; # Remove embedded tags
s/^\s*//; # and initial blanks
s/\s*$//; # and final blanks
s/\s\s\s*/ /g; # and extra blanks
s/<//g;
s/&/&/g;
$entry{$x, $y} = $_;
$x++;
}
}
$numlines = $y;
# Compute the max width of each column
$totalwidth = 0;
for ($x = 0; $x < $numcols; $x++) {
$max = 0;
for ($y = 0; $y < $numlines; $y++) {
$len = length($entry{$x, $y});
if ($len > $max) { $max = $len; }
}
$width[$x] = $max;
$totalwidth += $max;
}
# If it does not fit in one line, turn wide fields into multi-line fields
if ($totalwidth >= 65) {
$totalwidth = 0;
$maxwidth = 65 / $numcols;
for ($x = 0; $x < $numcols; $x++) {
if ($width[$x] > $maxwidth) {
if ($x < $numcols - 1) {
$width[$x] = $maxwidth;
} else {
$width[$x] = 70 - $totalwidth;
}
}
$totalwidth += $width[$x];
}
}
# Compute the separators
if ($border) {
$horsep = '+-';
for ($x = 0; $x < $numcols; $x++) {
if ($x > 0) { $horsep .= '-+-'; }
$horsep .= '-' x $width[$x];
}
$horsep .= '-+';
$verleft = '| ';
$versep = ' | ';
$verright = ' |';
} else {
$horsep = '';
$verleft = ' ';
$versep = ' ';
$verright = ' ';
}
# Print the table
print $horsep, "\n";
for ($y = 0; $y < $numlines; $y++) {
do {
$overflow = 0;
print $verleft;
for ($x = 0; $x < $numcols; $x++) {
if ($x > 0) { print $versep; }
$_ = $entry{$x, $y};
if (length($_) > $width[$x]) {
$pos = rindex($_, ' ', $width[$x]);
if ($pos < 0) { $pos = $width[$x]; } else { $pos++; }
$entry{$x, $y} = substr($_, $pos);
$_ = substr($_, 0, $pos - 1);
$overflow = 1;
} else {
$entry{$x, $y} = '';
}
$len = length($_);
s/&/&/g;
s/</g;
s/>/>/g;
print $_, ' ' x ($width[$x] - $len);
}
print $verright, "\n";
} while($overflow);
if ($header && $y == 0) { print $horsep, "\n"; }
}
print $horsep, "\n";
}
|