Turns
| header1 | header2 |
| tr 1, td 1 |
tr 1, td 2 |
tr 3, td 1 | 00 | | 0 | | Yes, a Lone Table Cell |
|
tr2 td2 |
into
.=------------------------------------------=.
| header1 | header2 |
|-==========================================-|
| tr 1, td 1 | tr 1, td 2 |
|=-----------------------------*------------=|
| tr 3, td 1 | tr2 td2 |
| .=------------------------=. | |
| | 00 | | |
| | | | |
| | Yes, a Lone Table Cell | | |
| '=------------------------=' | |
'=------------------------------------------='
.=------------------------=.
| 00 |
| |
| Yes, a Lone Table Cell |
'=------------------------='
caveats: doesn't handle multiple rows with TH tags (just uses the last TH tags seen).
Text::ASCIITable doesn't like incomplete rows (row has set 4 columns, but 2 were added, so the row doesn't get added)
which I consider a bug (filed as such).
FIXED in Text-ASCIITable-0.14
Doesn't handle colspan/rowspan.
#!/usr/bin/perl
use strict;
use HTML::TreeBuilder;
use Text::ASCIITable;
use List::Util qw(max);
my $html = q~
<table border=1>
<tr><th>header1</th><th>header2</th></tr>
<tr>
<Td> tr 1, td 1 </td>
<td> tr 1, td 2</td>
</tr>
<tr>
<td> tr 3, td 1 <table border=0><tr><td>00</td></tr><tr><td>0</td></tr
+><tr><td> Yes, a Lone Table Cell</td></tr></table>
</td>
<td> tr2 td2 </td>
</tr>
</table>~;
my $t = HTML::TreeBuilder->new();
$t->parse($html);
$t->eof;
print DumpTable( $_ ), $/, $/
for $t->find_by_tag_name('table') ;
sub DumpTable {
my $ht = shift;
die "$ht is not a table" unless $ht->tag eq 'table';
my $tt = Text::ASCIITable::->new;
my @co;
my @da;
my $da = [];
for my $ro ( @{ $ht->content() } ) {
if( $ro->tag eq 'tr' ) {
push @da, $da if @$da;
$da = [];
for my $ce ( @{ $ro->content() } ) {
if( $ce->tag eq 'td' ) {
if( $ce->look_down( '_tag', 'table' ) ) {
my $string = '';
for my $i ( @{ $ce->content() } ) {
if( not ref $i ) {
$string .= $i;
}
elsif( $i->tag eq 'table' ) {
$string .= "\n";
$string .= DumpTable($i);
$string .= "\n";
}
else {
$string .= $i->as_text;
}
}
push @$da, $string;
}
else {
push @$da, $ce->as_text;
}
}
elsif( $ce->tag eq 'th' ) {
push @co, $ce->as_text;
}
}
}
}
push @da, $da if @$da;
if(@co) {
$tt->setCols(@co);
}
else {
use List::Util qw(max);
my $max = 1 + max( 0, map { $#$_ } @da );
$tt->setCols( (' ') x $max );
$tt->setOptions( hide_HeadRow => 1 );
$tt->setOptions( hide_HeadLine => 1 );
}
$tt->addRow($_) for @da;
$tt->setOptions( 'drawRowLine', 1) if $ht->attr('border');
# return $tt->draw();
return $tt->draw(
[ '.=', '=.', '-', '-' ], # .=-----------=.
[ '|', '|', '|' ], # | info | info |
[ '|-', '-|', '=', '=' ], # |-===========-|
[ '|', '|', '|' ], # | info | info |
[ "'=", "='", '-', '-' ], # '=-----------='
[ '|=', '=|', '-', '*' ] # rowseperator
);
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.