HTML-TableExtract-2.11/0000755000175000017500000000000011625004044013261 5ustar sisksiskHTML-TableExtract-2.11/Makefile.PL0000644000175000017500000000075010400114364015232 0ustar sisksiskuse ExtUtils::MakeMaker; my %prereq_pm = ( 'HTML::Parser' => 0, ); # The idea is to skip tests and dependencies on H::ET if it is not # installed at all. If it is presently installed, however, enforce the # version dependency. my $et_version = '1.16'; eval "use HTML::ElementTable"; unless ($@) { $prereq_pm{'HTML::ElementTable'} = $et_version; } WriteMakefile( NAME => 'HTML-TableExtract', VERSION_FROM => 'lib/HTML/TableExtract.pm', PREREQ_PM => \%prereq_pm, ); HTML-TableExtract-2.11/README0000644000175000017500000000374411625003570014154 0ustar sisksiskHTML-TableExtract ----------------- HTML::TableExtract is a module that simplifies the extraction of information contained in tables within HTML documents. Tables of note may be specified using Headers, Depth, Count, Attributes, or some combination of the three. See the module documentation for details. INSTALLATION You install HTML-TableExtract, as you would install any perl module library, by running these commands: perl Makefile.PL make make test make install DOCUMENTATION See HTML/TableExtract.pm for the code. See Changes for recent changes. POD style documentation is included in the module. This is normally converted to a manual page and installed as part of the "make install" process. You should also be able to use the 'perldoc' utility to extract and read documentation from the module directly. There is also an example page available: http://www.mojotoad.com/sisk/projects/HTML-TableExtract/tables.html SUPPORT There is a mailing list available. To register or examine archived messages, go here: http://lists.sourceforge.net/mailman/listinfo/table-extract-general Questions and comments may also be directed to Matt Sisk AVAILABILITY The package is available from CPAN: http://www.cpan.org/authors/id/M/MS/MSISK/ The latest version is also available at: http://www.mojotoad.com/sisk/projects/HTML-TableExtract/ ACKNOWLEDGEMENTS Thanks to the following people for their generous bug catching, fault analysis, and suggestions: Celeste Suliin Burris, Jeff Casey, David Finberg, Michael Fowler, Robert Goff, Klaus Gottschalk, Daniel Griscom, Jeremy Howard, Martin Joost, Jeff Lewwid, Nicholas R. Markham, Julian Mehnle, Michael S. Muegel, Patrick Naubert, Jani Ollikainen, Wilson Snyder, Volker Stuerzl, Steve Wong, and Matt Zip. COPYRIGHT Copyright (c) 1999-2011 Matthew P. Sisk. All rights reserved. All wrongs revenged. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. HTML-TableExtract-2.11/META.yml0000644000175000017500000000102311625004044014526 0ustar sisksisk--- #YAML:1.0 name: HTML-TableExtract version: 2.11 abstract: ~ author: [] license: unknown distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: ExtUtils::MakeMaker: 0 requires: HTML::ElementTable: 1.16 HTML::Parser: 0 no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.55_02 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 HTML-TableExtract-2.11/Changes0000644000175000017500000001405211625003501014553 0ustar sisksiskRevision history for HTML::TableExtract 2.11 Tue Aug 23 16:01:04 EDT 2011 - added parsing context, override for eof() and parse() for memory clear on new docs or post-eof() - fixed some long standing test warnings 2.10 Sat Jul 15 20:50:41 EDT 2006 - minor bug fixed in HTML repair routines (thanks to Dave Gray) 2.09 Thu Jun 8 15:46:17 EDT 2006 - Tweaked rasterizer to handle some situations where the HTML is broken but tables can still be inferred. - Fixed TREE() definition for situations where import() is not invoked. (thanks to DDICK on cpan.org) 2.08 Wed May 3 17:17:33 EDT 2006 - Implemented new rasterizer for grid mapping. Thanks to Roland Schar for a tortuous example of span issues. - This also fixes a bug the old skew method had when it encountered ridiculously large spans (out of memory). Thanks to Andreas Gustafsson. - Regular extraction and TREE mode are using the same rasterizer now. - Fixed HTML stripping for a header matching bug on single word text in keep_html mode (thanks to Michael S. Muegel for pointing the bug out) 2.07 Sun Feb 19 13:40:44 EST 2006 - Fixed subtable slicing bug - Fixed hrow() attachment bug - Added tests 2.06 Tue Oct 18 13:13:52 EDT 2005 - Tightened up element interactions in TREE() mode when examining rows, columns, cells, etc. Was running into trouble with dereferencing scalars vs objects. - Documented space() H::TE::T method, added tests - Added POD tests - Documentation updates and fixes 2.05 Tue Oct 4 16:00:02 EDT 2005 - Fixed a TREE() definition bug and class method assignments - Fixed a 'row above header' bug, added tests 2.04 Wed Aug 3 14:42:23 EDT 2005 - Fixed some conditional optional dependency tests in order to avoid falure assertions on some test boxes. 2.03 Wed Jul 20 12:45:56 EDT 2005 - Fixed greedy attribute bug (non qualifying tables were being selected under certain circumstances) - Moved more completely to File::Spec operations in testload.pm in order to make windows boxes happy. 2.02 Thu Jun 23 12:42:44 EDT 2005 - squelched TREE() creation warnings for subclasses - fixed a rows() bug involving keep_headers 2.01 Tue Jun 21 22:05:53 EDT 2005 - fixed some test changes 2.00 Fri Jun 17 17:28:10 EDT 2005 - Can now return parsed tables as HTML::TableElement objects within an HTML::Element tree structure (via HTML::TreeBuilder) for such purposes as in-line editing of table content within documents. Invoked via 'use HTML::TableExtract qw(tree);'. - Added columns(), row(), column(), and cell() methods. - Added some handy reporting methods: tables_report() and tables_dump(). These are almost always handy while first analyzing a new HTML document for table content. - Debugging and error output can now be assigned to arbitrary file handles. ! Old 'table_state' methods are now merely 'table' methods, though the old table_state style is still supported. ! Chains have been dropped. Though interesting (think xpath), they needlessly complicated matters as they were nearly universally unused. 1.09 Fri Feb 25 17:49:00 EST 2005 - Tables can now be selected by table tag attributes - lineage() method now returns row and column information, as well as depth and count, for each ancestor (potential backwards incompatability, entries are now 4 element arrays now rather than 2) - header matching and column retention enhancements - header retention - old-style procedures deprecated in prepration for them to become methods - various bug fixes 1.08 Thu Apr 4 11:26:27 CST 2002 - Added some more crufty HTML tolerance -- not PC (puristicly correct) but HTML correctness is probably of no interest to those merely trying to extract information *out* of HTML. - Fixed a mapback problem with the legacy methods 1.07 Wed Aug 22 06:14:24 CDT 2001 - Added keep_html option for HTML retention - bug fix for depth/count targets 1.06 Thu Nov 2 15:29:49 CST 2000 - Added
translation to newlines (enabled by default) - cleaned up some warnings 1.05 Sun Aug 6 06:38:14 CDT 2000 - minor bug fix involving empty cells 1.04 Sat Jul 15 02:18:04 CDT 2000 - fixed gridmap bug involving skew calcs on unwanted columns - added example page reference in README 1.03 Tue Jul 7 03:43:30 CDT 2000 - gridmap option, columns are really columns regardless of cell span skew - Added chains for relative targeting * Terminus-matching by default * Elasticity option * Waypoint retention option * Lineage tracking (match record along chain) - Significant tests added to 'make test' - Documentation rewrite 0.05 Tue Mar 21 08:11:54 CST 2000 - Fixed -w init warnings for dangling columns in header mode - added 'decode' option to turn off text decoding when desired - internally stores real slices right now rather than sparse tables that later get massaged. 0.03 Thu Mar 9 13:10:03 CST 2000 - Fixed bug regarding incomplete defaults - Tables, rows, and cells that are either empty or contain no text are now properly noted - Header patterns now match across stripped tags - In some cases, mangled HTML tables are properly scanned by inferring missing tags. - Depth/Count votes are now properly honored. - Cleaned up some -w noise. 0.02 Thu Feb 10 13:43:04 CST 2000 - Fixed some problems tracking counts at revisited depths. - Minor doc fix, added mailing list 0.01 Wed Feb 2 18:24:07 CST 2000 - Initial version. HTML-TableExtract-2.11/MANIFEST0000644000175000017500000000066710456307003014425 0ustar sisksiskREADME Changes MANIFEST Makefile.PL lib/HTML/TableExtract.pm t/00_basic.t t/01_pod.t t/02_pod_coverage.t t/10_bulk.t t/11_count.t t/12_depth.t t/13_attrib.t t/14_headers.t t/15_depth_count.t t/16_subtable.t t/20_skew.t t/21_skew_map.t t/22_header_row.t t/30_tree.t t/testload.pm t/basic.html t/basic2.html t/gnarly.html t/skew.html t/subtable.html t/ugly.html META.yml Module meta-data (added by MakeMaker) HTML-TableExtract-2.11/t/0000755000175000017500000000000011625004044013524 5ustar sisksiskHTML-TableExtract-2.11/t/basic2.html0000644000175000017500000002166010301445350015561 0ustar sisksisk TableExtract Test HTML

Here lies Table 1:

not headernot headernot headernot headernot headernot headernot headernot headernot headernot header
not headernot headernot headernot headernot headernot headernot headernot headernot headernot header
not headernot headernot headernot headernot headernot headernot headernot headernot headernot header
Header ZeroHeader OneHeader TwoHeader ThreeHeader FourHeader FiveHeader SixHeader SevenHeader EightHeader Nine
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7) (5,8) (5,9)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9)
(7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7) (7,8) (7,9)
(8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9)
(9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9)

Here lies Table 2:

Header ZeroHeader OneHeader TwoHeader ThreeHeader FourHeader FiveHeader SixHeader SevenHeader EightHeader Nine
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7) (5,8) (5,9)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9)
(7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7) (7,8) (7,9)
(8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9)
(9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9)

Here lies Table 3 with 4 and 5 inside:

Header ZeroHeader OneHeader TwoHeader ThreeHeader FourHeader FiveHeader SixHeader SevenHeader EightHeader Nine
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5)
Header ZeroHeader OneHeader TwoHeader ThreeHeader FourHeader FiveHeader SixHeader SevenHeader EightHeader Nine
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7) (5,8) (5,9)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9)
(7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7) (7,8) (7,9)
(8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9)
(9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9)
(5,6) (5,7) (5,8) (5,9)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9)
(7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7)
Header ZeroHeader OneHeader TwoHeader ThreeHeader FourHeader FiveHeader SixHeader SevenHeader EightHeader Nine
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7) (5,8) (5,9)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9)
(7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7) (7,8) (7,9)
(8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9)
(9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9)
(7,8) (7,9)
(8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9)
(9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9)
HTML-TableExtract-2.11/t/12_depth.t0000755000175000017500000000072210321276244015330 0ustar sisksisk#!/usr/bin/perl use strict; use lib './lib'; use Test::More tests => 222; use FindBin; use lib $FindBin::RealBin; use testload; my $file = "$Dat_Dir/basic.html"; use HTML::TableExtract; # By count my $label = 'by depth'; my $te = HTML::TableExtract->new( depth => 1 ); ok($te->parse_file($file), "$label (parse_file)"); my @tablestates = $te->tables; cmp_ok(@tablestates, '==', 2, "$label (extract count)"); good_data($_, "$label (data)") foreach @tablestates; HTML-TableExtract-2.11/t/ugly.html0000644000175000017500000001511110215630545015376 0ustar sisksisk Chain Test

You like ugly? I'll show you UGLY:

0,0: (0,0)0,0: (0,1)
0,0: (1,0)
1,0: (0,0)1,0: (0,1)
1,0: (1,0)
2,0: (0,0)2,0: (0,1)
2,0: (1,0)
3,0: (0,0)3,0: (0,1)
3,0: (1,0)3,0: (1,1)
3,0: (2,0)3,0: (2,1)
2,0: (1,1)
2,0: (2,0)2,0: (2,1)
3,1: (0,0)3,1: (0,1)
3,1: (1,0)3,1: (1,1)
3,1: (2,0)3,1: (2,1)
1,0: (1,1)
1,0: (2,0)1,0: (2,1)
2,1: (0,0)2,1: (0,1)
2,1: (1,0)2,1: (1,1)
3,2: (0,0)3,2: (0,1)
3,2: (1,0)3,2: (1,1)
3,2: (2,0)3,2: (2,1)
2,1: (2,0)
3,3: (0,0)3,3: (0,1)
3,3: (1,0)3,3: (1,1)
3,3: (2,0)3,3: (2,1)
2,1: (2,1)
0,0: (1,1)
0,0: (2,0)0,0: (2,1)
1,1: (0,0)1,1: (0,1)
1,1: (1,0)1,1: (1,1)
2,2: (0,0)2,2: (0,1)
2,2: (1,0)
3,4: (0,0)3,4: (0,1)
3,4: (1,0)3,4: (1,1)
3,4: (2,0)3,4: (2,1)
2,2: (1,1)
2,2: (2,0)2,2: (2,1)
3,5: (0,0)3,5: (0,1)
3,5: (1,0)3,5: (1,1)
3,5: (2,0)3,5: (2,1)
1,1: (2,0)
2,3: (0,0)2,3: (0,1)
2,3: (1,0)2,3: (1,1)
3,6: (0,0)3,6: (0,1)
3,6: (1,0)3,6: (1,1)
3,6: (2,0)3,6: (2,1)
2,3: (2,0)
3,7: (0,0)3,7: (0,1)
3,7: (1,0)3,7: (1,1)
3,7: (2,0)3,7: (2,1)
2,3: (2,1)
1,1: (2,1)
HTML-TableExtract-2.11/t/16_subtable.t0000755000175000017500000000334411623037117016034 0ustar sisksisk#!/usr/bin/perl use strict; use lib './lib'; use Test::More tests => 692; use FindBin; use lib $FindBin::RealBin; use testload; my $file = "$Dat_Dir/basic.html"; use HTML::TableExtract; # By count my $label = 'by subtable scoop'; my $te = HTML::TableExtract->new( depth => 0, count => 2, subtables => 1, ); ok($te->parse_file($file), "$label (parse_file)"); my @tablestates = $te->tables; cmp_ok(@tablestates, '==', 3, "$label (extract count)"); good_data($_, "$label (data)") foreach @tablestates; # Check subtable slice immunity $file = "$Dat_Dir/subtable.html"; $label = 'by subtable, slice immune'; $te = HTML::TableExtract->new( headers => [('SubtableHead Eight', 'SubtableHead Two')], subtables => 1, ); ok($te->parse_file($file), "$label (parse_file)"); @tablestates = $te->tables; cmp_ok(@tablestates, '==', 4, "$label (extract count)"); my $mule = splice(@tablestates, 0, 1); my @mrows = $mule->rows; cmp_ok(@mrows, '==', 1, "$label (mule row check)"); cmp_ok(@{$mrows[0]}, '==', 4, "$label (mule col check)"); good_slice_data($tablestates[-1], "$label (data)", 0, 3); good_data($_, "$label (data)") foreach @tablestates[0,1]; # Check subtable slice precedence $label = 'by subtable, slice precedence'; $te = HTML::TableExtract->new( headers => [('Head.*Eight', 'Head.*Two')], subtables => 1, ); ok($te->parse_file($file), "$label (parse_file)"); @tablestates = $te->tables; cmp_ok(@tablestates, '==', 4, "$label (extract count)"); $mule = splice(@tablestates, 2, 1); @mrows = $mule->rows; cmp_ok(@mrows, '==', 1, "$label (mule row check)"); cmp_ok(@{$mrows[0]}, '==', 4, "$label (mule col check)"); good_slice_data($tablestates[-1], "$label (data)", 0, 3); good_data($_, "$label (data)", 2, 8) foreach @tablestates[0,1]; HTML-TableExtract-2.11/t/22_header_row.t0000755000175000017500000000707710376140224016354 0ustar sisksisk#!/usr/bin/perl use strict; use lib './lib'; use Test::More tests => 19; use FindBin; use lib $FindBin::RealBin; use testload; my $file = "$Dat_Dir/basic.html"; my $file2 = "$Dat_Dir/basic2.html"; use HTML::TableExtract; # Check header row retention cases my($label, $te, @rows, $table); $label = 'header row (basic, default)'; $te = HTML::TableExtract->new(); ok($te->parse_file($file), "$label (parse)"); $table = $te->first_table_found; @rows = $table->rows; cmp_ok(@rows, '==', scalar @{$table->{grid}}, "$label (row count)"); $label = 'header row (basic, no keep)'; $te = HTML::TableExtract->new( keep_headers => 0 ); ok($te->parse_file($file), "$label (parse)"); $table = $te->first_table_found; @rows = $table->rows; cmp_ok(@rows, '==', scalar @{$table->{grid}}, "$label (row count)"); $label = 'header row (basic, keep)'; $te = HTML::TableExtract->new( keep_headers => 1 ); ok($te->parse_file($file), "$label (parse)"); $table = $te->first_table_found; @rows = $table->rows; cmp_ok(@rows, '==', scalar @{$table->{grid}}, "$label (row count)"); my(@hrow, $hindex); my @headers = qw(Eight Six Four Two Zero); my @hlabels = map("Header $_", @headers); $label = 'header row (header, default)'; $te = HTML::TableExtract->new( headers => [@headers] ); ok($te->parse_file($file), "$label (parse)"); $table = $te->first_table_found; @rows = $table->rows; cmp_ok(@rows, '==', scalar @{$table->{grid}} - 1, "$label (row count)"); $hindex = $table->hrow_index; @hrow = $table->hrow; cmp_ok(join(' ', @hrow), 'eq', join(' ', @hlabels), "$label (hrow)"); $label = 'header row (header, nokeep)'; $te = HTML::TableExtract->new( headers => [@headers], keep_headers => 0, ); ok($te->parse_file($file), "$label (parse)"); $table = $te->first_table_found; @rows = $table->rows; cmp_ok(@rows, '==', scalar @{$table->{grid}} - 1, "$label (row count)"); $hindex = $table->hrow_index; @hrow = $table->hrow; cmp_ok(join(' ', @hrow), 'eq', join(' ', @hlabels), "$label (hrow)"); $label = 'header row (header, keep)'; $te = HTML::TableExtract->new( headers => [@headers], keep_headers => 1, ); ok($te->parse_file($file), "$label (parse)"); $table = $te->first_table_found; @rows = $table->rows; cmp_ok(@rows, '==', scalar @{$table->{grid}}, "$label (row count)"); $hindex = $table->hrow_index; @hrow = $table->hrow; cmp_ok(join(' ', @hrow), 'eq', join(' ', @hlabels), "$label (hrow)"); ### # Traditionally we clip extraneous rows above our header rows. $label = 'pre-header row clip (header, nokeep)'; $te = HTML::TableExtract->new( headers => [@headers], keep_headers => 0, ); ok($te->parse_file($file2), "$label (parse)"); $table = $te->first_table_found; my $ghi = get_grid_header_index($table->{grid}); @rows = $table->rows; cmp_ok(@rows, '==', scalar @{$table->{grid}} - $ghi - 1, "$label (row count)"); $label = 'pre-header row clip (header, keep)'; $te = HTML::TableExtract->new( headers => [@headers], keep_headers => 1, ); ok($te->parse_file($file2), "$label (parse)"); $table = $te->first_table_found; $ghi = get_grid_header_index($table->{grid}); @rows = $table->rows; cmp_ok(@rows, '==', scalar @{$table->{grid}} - $ghi, "$label (row count)"); sub get_grid_header_index { my $grid = shift; my $ghi = 0; foreach (0 .. $#{$table->{grid}}) { my $item = $table->{grid}[$_][0]; $item = $$item if ref $item; next if $item =~ /not\s+header/i; $ghi = $_; last; } $ghi; } HTML-TableExtract-2.11/t/01_pod.t0000755000175000017500000000021410426212527015000 0ustar sisksiskuse Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 or greater required for testing POD" if $@; all_pod_files_ok(); HTML-TableExtract-2.11/t/testload.pm0000644000175000017500000001451110426003735015707 0ustar sisksiskpackage testload; use strict; use Test::More; use File::Spec; use vars qw( @ISA @EXPORT $Dat_Dir @LINEAGE_DATA @HEADERS @SKEW_DATA @GNARLY_DATA @TRANSLATION_DATA ); require Exporter; @ISA = qw(Exporter); @EXPORT = qw( $Dat_Dir @LINEAGE_DATA @HEADERS @SKEW_DATA i @TRANSLATION_DATA @GNARLY_DATA good_data good_slice_data good_skew_data good_gnarly_data good_sticky_data ); my $base_dir; BEGIN { my $pkg = __PACKAGE__; $pkg =~ s%::%/%g; $pkg .= '.pm'; my @parts = File::Spec->splitpath(File::Spec->canonpath($INC{$pkg})); $parts[-1] = ''; $base_dir = File::Spec->catpath(@parts); } $Dat_Dir = $base_dir; # For dataset 'chain' @LINEAGE_DATA = ( [ '0,0,1,0', '1,0,1,0', '2,0,1,0', '3,0' ], [ '0,0,1,0', '1,0,1,0', '2,0,2,1', '3,1' ], [ '0,0,1,0', '1,0,1,0', '2,0' ], [ '0,0,1,0', '1,0,2,1', '2,1,1,1', '3,2' ], [ '0,0,1,0', '1,0,2,1', '2,1,2,0', '3,3' ], [ '0,0,1,0', '1,0,2,1', '2,1' ], [ '0,0,1,0', '1,0' ], [ '0,0,2,1', '1,1,1,1', '2,2,1,0', '3,4' ], [ '0,0,2,1', '1,1,1,1', '2,2,2,1', '3,5' ], [ '0,0,2,1', '1,1,1,1', '2,2' ], [ '0,0,2,1', '1,1,2,0', '2,3,1,1', '3,6' ], [ '0,0,2,1', '1,1,2,0', '2,3,2,0', '3,7' ], [ '0,0,2,1', '1,1,2,0', '2,3' ], [ '0,0,2,1', '1,1' ], [ '0,0' ] ); # For data set 'basic' @HEADERS = ( 'Header Zero', 'Header One', 'Header Two', 'Header Three', 'Header Four', 'Header Five', 'Header Six', 'Header Seven', 'Header Eight', 'Header Nine', ); # For data set 'skew' @SKEW_DATA = ( [ 'head0','head1','head2','head3' ], [ 'THIS IS A WHOLE ROW-CELL OF JUNK','','','' ], [ 'JUNK','Tasty tidbit (1,1)','JUNK','Tasty tidbit (1,3)' ], [ '',"BIG\nJUNK",'','Tasty tidbit (2,3)' ], [ 'Tasty tidbit (3,0)','','','Tasty tidbit (3,3)' ], [ 'Tasty tidbit (4,0)','','','Tasty tidbit (4,3)' ], [ 'JUNK BUTTON','','Tasty tidbit (5,2)','Tasty tidbit (5,3)' ], ); @TRANSLATION_DATA = ( [ '0,0', '0,1', '0,2', '0,3' ], [ '1,0', '1,0', '1,0', '1,0' ], [ '2,0', '2,1', '2,2', '2,3' ], [ '2,0', '3,1', '3,1', '3,3' ], [ '4,0', '3,1', '3,1', '4,3' ], [ '5,0', '3,1', '3,1', '5,3' ], [ '6,0', '6,0', '6,2', '6,3' ] ); @GNARLY_DATA = ( [ '(0,0) [1,4]', '', '', '', '(0,1) [2,4]', '', '', '' ], [ '(1,0) [2,1]', '(1,1) [1,1]', '(1,2) [1,2]', '', '', '', '', '' ], [ '', '(2,0) [2,4]', '', '', '', '(2,1) [2,2]', '', '(2,2) [1,1]' ], [ '(3,0) [1,1]', '', '', '', '', '', '', '(3,1) [1,1]' ], [ '(4,0) [3,2]', '', '(4,1) [1,1]', '(4,2) [3,1]', '(4,3) [4,4]', '', '', '' ], [ '', '', '(5,0) [1,1]', '', '', '', '', '' ], [ '', '', '(6,0) [1,1]', '', '', '', '', '' ], [ '(7,0) [1,4]', '', '', '', '', '', '', '' ] ); sub good_data { my($ts, $label, @slice) = @_; ref $ts or die "Oops: Table state ref required\n"; my $t = $ts->{grid}; my $skew; my $txt = ref $t->[0][0] eq 'SCALAR' ? ${$t->[0][0]} : $t->[0][0]->as_text; $skew = $txt =~ /^Header/ ? 1 : 0; my $row = 0 + $skew; if (@slice) { my @rows = $ts->rows; cmp_ok(scalar @slice, '==', scalar @{$rows[0]}, "$label (col cnt)"); } # Must have rows ok(scalar @{$t}, "$label (rows)"); # See if we got the numbers. foreach my $r ($row .. $#$t) { # Must have columns ok(scalar @{$t->[$r]}, "$label (columns)"); my @indices = @slice ? @slice : 0 .. $#{$t->[$r]}; foreach my $c (@indices) { my $rc = $skew ? $r : $r + 1; next if $ts->{headers} && !$ts->{hits}{$c}; my $txt = ref $t->[$r][$c] eq 'SCALAR' ? ${$t->[$r][$c]} : $t->[$r][$c]->as_text; like($txt, qr/^ \($rc,$c\)/, "$label ($r,$c)"); } } # Header order check if ($skew) { foreach my $c (0 .. $#{$t->[0]}) { my $hs = $HEADERS[$c]; my $txt = ref $t->[0][$c] eq 'SCALAR' ? ${$t->[0][$c]} : $t->[0][$c]->as_text; like($txt, qr/^$hs$/, "$label (header order)"); } } 1; } sub good_slice_data { my($ts, $label, @slice) = @_; my $t = $ts->{grid}; my @rows = $ts->rows; my $txt = ref $t->[0][0] eq 'SCALAR' ? ${$t->[0][0]} : $t->[0][0]->as_text; my $skew = 1; foreach my $r (0 .. $#rows) { my $row = $rows[$r]; my $trow = $t->[$r+$skew]; ok(@$row == @slice, "$label (slice width)"); my @s = $ts->column_map; foreach my $c (0 .. $#$row) { my $sc = $s[$c]; my $cell = $trow->[$sc]; my $txt = ref $cell eq 'SCALAR' ? $$cell : $cell->as_text; ok($row->[$c] eq $txt, "$label ($r,$c)"); } } } sub good_skew_data { push(@_, 0) if @_ == 2; _good_span_data(@_, \@SKEW_DATA); } sub good_gnarly_data { push(@_, 0) if @_ == 2; _good_span_data(@_, \@GNARLY_DATA); } sub _good_span_data { my($ts, $label, $reverse, $REF_DATA) = @_; ref $ts or die "Oops: Table state ref required\n"; my $t = $ts->{grid}; foreach my $r (1 .. $#$t) { my $row = $t->[$r]; my @cols = 0 .. $#$row; @cols = reverse @cols if $reverse; foreach my $c (@cols) { my $txt = ref $row->[$c] eq 'SCALAR' ? ${$row->[$c]} : $row->[$c]->as_text; $txt = '' unless defined $txt; cmp_ok($txt, 'eq', $REF_DATA->[$r][$c], $label); } } 1; } sub good_sticky_data { # testing grid aliasing my($ts, $label, $reverse) = @_; ref $ts or die "Oops: Table state ref required\n"; my $t = $ts->_gridalias; foreach my $r (0 .. $#$t) { my $row = $t->[$r]; my @cols = 0 .. $#$row; @cols = reverse @cols if $reverse; foreach my $c (@cols) { my $txt = ref $row->[$c] eq 'SCALAR' ? ${$row->[$c]} : $row->[$c]->as_text; my($tr,$tc) = $ts->source_coords($r,$c); cmp_ok("$tr,$tc", 'eq', $TRANSLATION_DATA[$r][$c], "$label (coords)"); my $trow = $t->[$tr]; my $ttxt = ref $trow->[$tc] eq 'SCALAR' ? ${$trow->[$tc]} : $trow->[$tc]->as_text; cmp_ok($ttxt, 'eq', $txt, "$label (content)"); cmp_ok($ttxt, 'eq', $SKEW_DATA[$tr][$tc], "$label (abs)"); } } 1; } 1; HTML-TableExtract-2.11/t/00_basic.t0000755000175000017500000000010210323027773015275 0ustar sisksiskuse Test::More tests => 1; BEGIN { use_ok('HTML::TableExtract') } HTML-TableExtract-2.11/t/basic.html0000644000175000017500000002051510267475127015515 0ustar sisksisk TableExtract Test HTML

Here lies Table 1:

Header ZeroHeader OneHeader TwoHeader ThreeHeader FourHeader FiveHeader SixHeader SevenHeader EightHeader Nine
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7) (5,8) (5,9)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9)
(7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7) (7,8) (7,9)
(8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9)
(9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9)

Here lies Table 2:

Header ZeroHeader OneHeader TwoHeader ThreeHeader FourHeader FiveHeader SixHeader SevenHeader EightHeader Nine
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7) (5,8) (5,9)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9)
(7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7) (7,8) (7,9)
(8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9)
(9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9)

Here lies Table 3 with 4 and 5 inside:

Header ZeroHeader OneHeader TwoHeader ThreeHeader FourHeader FiveHeader SixHeader SevenHeader EightHeader Nine
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5)
Header ZeroHeader OneHeader TwoHeader ThreeHeader FourHeader FiveHeader SixHeader SevenHeader EightHeader Nine
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7) (5,8) (5,9)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9)
(7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7) (7,8) (7,9)
(8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9)
(9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9)
(5,6) (5,7) (5,8) (5,9)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9)
(7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7)
Header ZeroHeader OneHeader TwoHeader ThreeHeader FourHeader FiveHeader SixHeader SevenHeader EightHeader Nine
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7) (5,8) (5,9)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9)
(7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7) (7,8) (7,9)
(8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9)
(9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9)
(7,8) (7,9)
(8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9)
(9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9)
HTML-TableExtract-2.11/t/skew.html0000644000175000017500000000131410425472540015371 0ustar sisksiskskew test
head0 head1 head2 head3
THIS IS A WHOLE ROW-CELL OF JUNK
JUNK Tasty tidbit (1,1) JUNK Tasty tidbit (1,3)
BIG
JUNK
Tasty tidbit (2,3)
Tasty tidbit (3,0) Tasty tidbit (3,3)
Tasty tidbit (4,0) Tasty tidbit (4,3)
JUNK BUTTON Tasty tidbit (5,2) Tasty tidbit (5,3)
HTML-TableExtract-2.11/t/13_attrib.t0000755000175000017500000000211110267475663015523 0ustar sisksisk#!/usr/bin/perl use strict; use lib './lib'; use Test::More tests => 556; use FindBin; use lib $FindBin::RealBin; use testload; my $file = "$Dat_Dir/basic.html"; use HTML::TableExtract; my($label, $te, @tables); # By border $label = 'by attribute (regular)'; $te = HTML::TableExtract->new( attribs => { border => 1 } ); ok($te->parse_file($file), "$label (parse_file)"); @tables = $te->tables; cmp_ok(@tables, '==', 3, "$label (extract count)"); good_data($_, "$label (data)") foreach @tables; # By cellpadding $label = 'by attribute (subset)'; $te = HTML::TableExtract->new( attribs => { cellpadding => 1 } ); ok($te->parse_file($file), "$label (parse_file)"); @tables = $te->tables; cmp_ok(@tables, '==', 1, "$label (extract count)"); good_data($_, "$label (data)") foreach @tables; # By cellpadding existence $label = 'by attribute (undef)'; $te = HTML::TableExtract->new( attribs => { cellpadding => undef } ); ok($te->parse_file($file), "$label (parse_file)"); @tables = $te->tables; cmp_ok(@tables, '==', 1, "$label (extract count)"); good_data($_, "$label (data)") foreach @tables; HTML-TableExtract-2.11/t/30_tree.t0000755000175000017500000000362011623025542015162 0ustar sisksisk#!/usr/bin/perl my $test_count; BEGIN { $test_count = 126 } use strict; use lib './lib'; use Test::More tests => $test_count; use FindBin; use lib $FindBin::RealBin; use testload; my $et_version = '1.17'; my($tb_present, $et_present); eval "use HTML::TreeBuilder"; $tb_present = !$@; eval "use HTML::ElementTable $et_version"; $et_present = !$@; SKIP: { skip "HTML::TreeBuilder not installed", $test_count unless $tb_present; skip "HTML::ElementTable $et_version not installed", $test_count unless $et_present; use_ok("HTML::TableExtract", qw(tree)); my $file = "$Dat_Dir/gnarly.html"; my $label = 'element table'; my $te = HTML::TableExtract->new(); isa_ok($te, 'HTML::TreeBuilder', "$label - HTML::TableExtract"); ok($te->parse_file($file), "$label (parse_file)"); my @tablestates = $te->tables; cmp_ok(@tablestates, '==', 1, "$label (extract count)"); good_gnarly_data($_, "$label (data)") foreach @tablestates; my $tree = $te->tree; ok($tree, 'treetop'); isa_ok($tree, 'HTML::Element'); foreach my $ts ($te->tables) { my $tree = $ts->tree; ok($tree, 'tabletop'); isa_ok($tree, 'HTML::ElementTable'); } local *FH; open(FH, '<', $file) or die "Oops opening $file : $!\n"; my $hstr = join('', ); close(FH); $hstr =~ s/\n//gm; $te->_attribute_purge; my $estr = $te->elementify->as_HTML; $estr =~ s/\n//gm; $estr =~ s/\"//gm; cmp_ok($estr, 'eq', $hstr, 'mass html comp'); # TREE() gets called during header extractions, make sure it does $label .= ' (header)'; $te = HTML::TableExtract->new( headers => [qr|\(0,1\) \[2,4\]|], ); ok($te->parse_file($file), "$label (parse_file)"); $tree = $te->tree; ok($tree, 'treetop'); isa_ok($tree, 'HTML::Element'); my $table = $te->first_table_found; good_gnarly_data($table, "$label (data)"); $tree = $table->tree; ok($tree, 'tabletop'); isa_ok($tree, 'HTML::ElementTable'); } HTML-TableExtract-2.11/t/11_count.t0000755000175000017500000000072210321276240015347 0ustar sisksisk#!/usr/bin/perl use strict; use lib './lib'; use Test::More tests => 222; use FindBin; use lib $FindBin::RealBin; use testload; my $file = "$Dat_Dir/basic.html"; use HTML::TableExtract; # By count my $label = 'by count'; my $te = HTML::TableExtract->new( count => 1 ); ok($te->parse_file($file), "$label (parse_file)"); my @tablestates = $te->tables; cmp_ok(@tablestates, '==', 2, "$label (extract count)"); good_data($_, "$label (data)") foreach @tablestates; HTML-TableExtract-2.11/t/02_pod_coverage.t0000755000175000017500000000030711623037342016657 0ustar sisksiskuse Test::More; eval "use Test::Pod::Coverage 1.00"; plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@; all_pod_coverage_ok({also_private => [qw/TREE parse eof/]}); HTML-TableExtract-2.11/t/10_bulk.t0000755000175000017500000000153610442077166015171 0ustar sisksisk#!/usr/bin/perl use strict; use lib './lib'; use Test::More tests => 52; use FindBin; use lib $FindBin::RealBin; use testload; my $file = "$Dat_Dir/ugly.html"; BEGIN { require_ok('HTML::TableExtract') } # by bulk, lineage integrity my $label = 'by bulk with lineage check'; my $te = HTML::TableExtract->new(); ok($te->parse_file($file), "$label (parse_file)"); my @tablestates = $te->tables; cmp_ok(@tablestates, '==', @LINEAGE_DATA, "$label (extract count)"); foreach my $tsc (0 .. $#tablestates) { my $ts = $tablestates[$tsc]; foreach (0 .. $#{$ts->{lineage}}) { cmp_ok(join(',', @{$ts->{lineage}[$_]}), 'eq', $LINEAGE_DATA[$tsc][$_], "$label (data)"); } my $mod = 1; $mod = 0 unless $ts->{headers} && !$ts->{keep_headers}; my @rows = $ts->rows; cmp_ok(@rows, '==', @{$ts->{grid}}-$mod, "rows() returns correct number of rows"); } HTML-TableExtract-2.11/t/20_skew.t0000755000175000017500000000310611623021700015162 0ustar sisksisk#!/usr/bin/perl use strict; use lib './lib'; use Test::More tests => 121; use FindBin; use lib $FindBin::RealBin; use testload; my $file = "$Dat_Dir/skew.html"; use HTML::TableExtract; # By count my $label = 'by header with span correction'; my $te = HTML::TableExtract->new( headers => [ qw(head0 head1 head2 head3) ], ); ok($te->parse_file($file), "$label (parse_file)"); my @tablestates = $te->tables; cmp_ok(@tablestates, '==', 1, "$label (extract count)"); good_skew_data($_, "$label (skew data)") foreach @tablestates; good_sticky_data($_, "$label (sticky data)") foreach @tablestates; # test aliasing directly -- this is tightly coupled with a cell in the # skew.html test file. my $str = "BIG\nJUNK"; $label = 'alias (no headers)'; alias_test($te->first_table_found, 2, 1, $str, $label); $label = 'alias (keep headers)'; $te = HTML::TableExtract->new( headers => [ qw(head0 head1 head2 head3) ], keep_headers => 1, ); ok($te->parse_file($file), "alias parse (keep headers)"); alias_test($te->first_table_found, 3, 1, $str, $label); sub alias_test { my($ts, $r, $c, $str, $label) = @_; my $item1 = $ts->row($r)->[$c]; my @rows = $ts->rows; my $item2 = $rows[$r][$c]; my $cell = $ts->cell($r,$c); my $cellno = $ts->cell($r+2,$c+1); my $space = $ts->space($r+2,$c+1); cmp_ok($str, 'eq', $item1, "$label (via row)"); cmp_ok($str, 'eq', $item2, "$label (via rows)"); cmp_ok($str, 'eq', $cell, "$label (via cell)"); cmp_ok($str, 'eq', $space, "$label (via space)"); cmp_ok(defined undef, '==', defined $cellno, "$label (undef via cell)"); } HTML-TableExtract-2.11/t/gnarly.html0000644000175000017500000000151310425476047015723 0ustar sisksiskgnarly table
(0,0) [1,4](0,1) [2,4]
(1,0) [2,1](1,1) [1,1](1,2) [1,2]
(2,0) [2,4](2,1) [2,2](2,2) [1,1]
(3,0) [1,1](3,1) [1,1]
(4,0) [3,2](4,1) [1,1](4,2) [3,1](4,3) [4,4]
(5,0) [1,1]
(6,0) [1,1]
(7,0) [1,4]
HTML-TableExtract-2.11/t/21_skew_map.t0000755000175000017500000000101510321276270016025 0ustar sisksisk#!/usr/bin/perl use strict; use lib './lib'; use Test::More tests => 26; use FindBin; use lib $FindBin::RealBin; use testload; my $file = "$Dat_Dir/skew.html"; use HTML::TableExtract; # By count my $label = 'by header with column mapping'; my $te = HTML::TableExtract->new( headers => [ qw(head3 head2 head1 head0) ], ); ok($te->parse_file($file), "$label (parse_file)"); my @tablestates = $te->tables; cmp_ok(@tablestates, '==', 1, "$label (extract count)"); good_skew_data($_, "$label (data)") foreach @tablestates; HTML-TableExtract-2.11/t/subtable.html0000644000175000017500000001223510342721364016224 0ustar sisksisk TableExtract Test HTML

Here lies Table 3 with 4, 5, and 6 inside:

SubtableHead ZeroSubtableHead OneSubtableHead TwoSubtableHead ThreeSubtableHead FourSubtableHead FiveSubtableHead SixSubtableHead SevenSubtableHead EightSubtableHead Nine
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5)
HubbaBubbaSandmanPiper
(2,6) (2,7) (2,8) (2,9)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5)
Header ZeroHeader OneHeader TwoHeader ThreeHeader FourHeader FiveHeader SixHeader SevenHeader EightHeader Nine
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7) (5,8) (5,9)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9)
(7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7) (7,8) (7,9)
(8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9)
(9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9)
(5,6) (5,7) (5,8) (5,9)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9)
(7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7)
Header ZeroHeader OneHeader TwoHeader ThreeHeader FourHeader FiveHeader SixHeader SevenHeader EightHeader Nine
(1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9)
(2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2,9)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3,9)
(4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4,9)
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7) (5,8) (5,9)
(6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6,9)
(7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7) (7,8) (7,9)
(8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9)
(9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9)
(7,8) (7,9)
(8,0) (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8,9)
(9,0) (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9,9)
HTML-TableExtract-2.11/t/15_depth_count.t0000755000175000017500000000076510321276257016556 0ustar sisksisk#!/usr/bin/perl use strict; use lib './lib'; use Test::More tests => 112; use FindBin; use lib $FindBin::RealBin; use testload; my $file = "$Dat_Dir/basic.html"; use HTML::TableExtract; # By count my $label = 'by depth and count'; my $te = HTML::TableExtract->new( depth => 0, count => 2, ); ok($te->parse_file($file), "$label (parse_file)"); my @tablestates = $te->tables; cmp_ok(@tablestates, '==', 1, "$label (extract count)"); good_data($_, "$label (data)") foreach @tablestates; HTML-TableExtract-2.11/t/14_headers.t0000755000175000017500000000140610376136540015645 0ustar sisksisk#!/usr/bin/perl use strict; use lib './lib'; use Test::More tests => 464; use FindBin; use lib $FindBin::RealBin; use testload; my $file = "$Dat_Dir/basic.html"; use HTML::TableExtract; # By headers my $label = 'by headers'; my $te = HTML::TableExtract->new( headers => [qw(Eight Six Four Two Zero)], ); ok($te->parse_file($file), "$label (parse_file)"); my @tablestates = $te->tables; cmp_ok(@tablestates, '==', 5, "$label (extract count)"); good_data($_, "$label (data)") foreach @tablestates; $te = HTML::TableExtract->new( headers => [qw(Eight Two)], ); ok($te->parse_file($file), "$label (parse_file)"); @tablestates = $te->tables; cmp_ok(@tablestates, '==', 5, "$label (extract count)"); good_slice_data($_, "$label (data)", 0, 3) foreach @tablestates; HTML-TableExtract-2.11/lib/0000755000175000017500000000000011625004044014027 5ustar sisksiskHTML-TableExtract-2.11/lib/HTML/0000755000175000017500000000000011625004044014573 5ustar sisksiskHTML-TableExtract-2.11/lib/HTML/TableExtract.pm0000644000175000017500000015351211625003542017524 0ustar sisksiskpackage HTML::TableExtract; # This package extracts tables from HTML. Tables of interest may be # specified using header information, depth, order in a depth, table tag # attributes, or some combination of the four. See the POD for more # information. # # Author: Matthew P. Sisk. See the POD for copyright information. use strict; use Carp; use vars qw($VERSION @ISA); $VERSION = '2.11'; use HTML::Parser; @ISA = qw(HTML::Parser); use HTML::Entities; # trickery for subclassing from HTML::TreeBuilder rather than the # default HTML::Parser. (use HTML::TableExtract qw(tree);) Also installs # a mode constant TREE(). BEGIN { *TREE = sub { 0 } } sub import { my $class = shift; no warnings; *TREE = @_ ? sub { 1 } : sub { 0 }; return unless @_; my $mode = shift; croak "Unknown mode '$mode'\n" unless $mode eq 'tree'; eval "use HTML::TreeBuilder"; croak "Problem loading HTML::TreeBuilder : $@\n" if $@; eval "use HTML::ElementTable 1.17"; croak "problem loading HTML::ElementTable : $@\n" if $@; @ISA = qw(HTML::TreeBuilder); $class; } # Backwards compatibility for deprecated methods *table_state = *table; *table_states = *tables; *first_table_state_found = *first_table_found; ### my %Defaults = ( headers => undef, depth => undef, count => undef, attribs => undef, subtables => undef, gridmap => 1, decode => 1, automap => 1, slice_columns => 1, keep_headers => 0, br_translate => 1, error_handle => \*STDOUT, debug => 0, keep_html => 0, strip_html_on_match => 1, ); my $Dpat = join('|', sort keys %Defaults); ### Constructor sub new { my $that = shift; my $class = ref($that) || $that; my(%pass, %parms, $k, $v); while (($k,$v) = splice(@_, 0, 2)) { if ($k eq 'headers') { ref $v eq 'ARRAY' or croak "Param '$k' must be passed in ref to array\n"; $parms{$k} = $v; } elsif ($k =~ /^$Dpat$/) { $parms{$k} = $v; } else { $pass{$k} = $v; } } my $self = $class->SUPER::new(%pass); bless $self, $class; foreach (keys %parms, keys %Defaults) { $self->{$_} = exists $parms{$_} && defined $parms{$_} ? $parms{$_} : $Defaults{$_}; } if ($self->{headers}) { $self->_emsg("TE here, headers: ", join(',', @{$self->{headers}}), "\n") if $self->{debug}; $self->{gridmap} = 1; } # Initialize counts and containers $self->_reset_state; $self; } ### HTML::Parser overrides sub start { my $self = shift; my @res; @res = $self->SUPER::start(@_) if TREE(); # Create a new table state if entering a table. if ($_[0] eq 'table') { my $ts = $self->_enter_table(@_); $ts->tree($res[0]) if @res; } # Rows and cells are next. if ($self->{_in_a_table}) { my $ts = $self->current_table; my $skiptag = 0; if ($_[0] eq 'tr') { $ts->_enter_row; ++$skiptag; } elsif ($_[0] eq 'td' || $_[0] eq 'th') { $ts->_enter_cell(@_); my %attrs = ref $_[1] ? %{$_[1]} : {}; my $rspan = $attrs{rowspan} || 1; my $cspan = $attrs{colspan} || 1; $ts->_rasterizer->($ts->row_count, $rspan, $cspan); $ts->_anchor_item(@res); ++$skiptag; } if ($self->{keep_html} && !$skiptag) { $self->text($_[3]); } } # Replace
with newlines if requested if ($_[0] eq 'br' && $self->{br_translate} && !$self->{keep_html}) { $self->text("\n"); } @res; } # end start sub end { my $self = shift; my @res = $self->SUPER::end(@_) if TREE(); if ($self->{_in_a_table}) { my $ts = $self->current_table; if ($_[0] eq 'td' || $_[0] eq 'th') { $ts->_exit_cell; } elsif ($_[0] eq 'tr') { $ts->_exit_row; } elsif ($_[0] eq 'table') { $self->_exit_table; } unless (TREE()) { $self->text($_[1]) if $self->{keep_html} && $ts->{in_cell}; } } @res; } sub text { my $self = shift; my @res = $self->SUPER::text(@_) if TREE(); if ($self->{_in_a_table} && !TREE()) { my $ts = $self->current_table; return unless $ts->{in_cell}; if ($self->{decode} && !$self->{keep_html}) { $ts->_add_text(decode_entities($_[0])); } else { $ts->_add_text($_[0]); } } @res; } sub parse { my $self = shift; $self->_reset_state unless $self->{_parsing}; $self->{_parsing} ||= 1; $self->SUPER::parse(@_); } sub eof { my $self = shift; $self->{_parsing} = 0; $self->SUPER::eof(@_); } ### End HTML::Parser overrides ### Report Methods sub depths { # Return all depths where valid tables were located. my $self = shift; return () unless ref $self->{_tables}; sort { $a <=> $b } keys %{$self->{_tables}}; } sub counts { # Given a depth, return the counts of all valid tables found therein. my($self, $depth) = @_; defined $depth or croak "Depth required\n"; return () unless exists $self->{_tables}{$depth}; sort { $a <=> $b } keys %{$self->{_tables}{$depth}}; } sub table { # Return the table state for a particular depth and count my($self, $depth, $count) = @_; defined $depth or croak "Depth required\n"; defined $count or croak "Count required\n"; if (! $self->{_tables}{$depth} || ! $self->{_tables}{$depth}{$count}) { return undef; } $self->{_tables}{$depth}{$count}; } sub first_table_found { my $self = shift; ref $self->{_ts_sequential}[0] ? $self->{_ts_sequential}[0] : undef; } sub rows { shift->first_table_found->rows(@_) } sub tables { # Return all valid table records found, in the order that they # were seen. my $self = shift; while ($self->{_in_a_table}) { my $ts = $self->current_table; $self->_emsg("Mangled HTML in table ($ts->{depth},$ts->{count}), inferring closing table tag.\n") if $self->{debug}; $self->_exit_table; } @{$self->{_ts_sequential}}; } # in tree mode, we already are an HTML::TreeBuilder, which is an # HTML::Element structure after parsing...but we provide this for # consistency with the table object method for accessing the tree # structures. sub tree { shift } sub tables_report { # Print out a summary of extracted tables, including depth/count my $self = shift; my $str; foreach my $ts ($self->tables) { $str .= $ts->report(@_); } $str; } sub tables_dump { my $self = shift; $self->_emsg($self->tables_report(@_)); } # for testing/debugging sub _attribute_purge { my $self = shift; foreach (keys %Defaults) { delete $self->{$_}; } } ### Runtime sub _enter_table { my($self, @args) = @_; ++$self->{_cdepth}; ++$self->{_in_a_table}; my $depth = $self->{_cdepth}; # Table tag attributes, if present my $attribs = $args[1] || {}; # Table states can come and go on the stack...here we retrieve the # table state for the table surrounding the current table tag (parent # table state). If the current table tag belongs to a top level table, # then this will be undef. my $pts = $self->current_table; # Counts are tracked for each depth. my $counts = $self->{_counts}; $counts->[$depth] = -1 unless defined $counts->[$depth]; ++$counts->[$depth]; my $count = $counts->[$depth]; $self->_emsg("TABLE: cdepth $depth, ccount $count, it: $self->{_in_a_table}\n") if $self->{debug} >= 2; # Umbrella status means that this current table and all of its # descendant tables will be harvested. my $umbrella = 0; if (! defined $self->{depth} && ! defined $self->{count} && ! $self->{attribs} && ! $self->{headers}) { ++$umbrella; } # Basic parameters for the soon-to-be-created table state. my %tsparms = ( depth => $depth, count => $count, attribs => $attribs, umbrella => $umbrella, automap => $self->{automap}, slice_columns => $self->{slice_columns}, keep_headers => $self->{keep_headers}, counts => $counts, error_handle => $self->{error_handle}, debug => $self->{debug}, keep_html => $self->{keep_html}, strip_html_on_match => $self->{strip_html_on_match}, parent_table => $pts, ); # Target constraints. There is no point in passing any of these along # if we are under an umbrella. Notice that with table states, "depth" # and "count" are absolute coordinates recording where this table was # created, whereas "tdepth" and "tcount" are the target constraints. # Headers have "absolute" meaning, therefore are passed by the # same name. if (!$umbrella) { $tsparms{tdepth} = $self->{depth}; $tsparms{tcount} = $self->{count}; $tsparms{tattribs} = $self->{attribs}; $tsparms{headers} = $self->{headers}; } # Abracadabra my $ts = HTML::TableExtract::Table->new(%tsparms); # Push the newly created and configured table state onto the stack. # This will now be the current_table(). push(@{$self->{_tablestack}}, $ts); $ts; } sub _exit_table { my $self = shift; my $ts = $self->current_table; # Last ditch fix for HTML mangle if ($ts->{in_cell}) { $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), forcing exit of cell ($ts->{rc},$ts->{cc}) due to table exit\n") if $self->{debug}; $ts->_exit_cell; } if ($ts->{in_row}) { $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), forcing exit of row $ts->{rc} due to table exit\n") if $self->{debug}; $ts->_exit_row; } # transform from tree to grid using our rasterized template $ts->_grid_map(); $self->_capture_table($ts) if $ts->_check_triggers; # Restore last table state pop(@{$self->{_tablestack}}); --$self->{_in_a_table}; my $lts = $self->current_table; if (ref $lts) { $self->{_cdepth} = $lts->{depth}; } else { # Back to the top level $self->{_cdepth} = -1; } $self->_emsg("LEAVE: cdepth: $self->{_cdepth}, ccount: $ts->{count}, it: $self->{_in_a_table}\n") if $self->{debug} >= 2; } sub _capture_table { my($self, $ts, $type) = @_; croak "Table state ref required\n" unless ref $ts; if ($self->{debug} >= 2) { my $msg = "Captured table (" . $ts->depth . ',' . $ts->count . ")"; $msg .= " ($type)" if $type; $msg .= "\n"; $self->_emsg($msg); } $ts->tree(HTML::ElementTable->new_from_tree($ts->tree)) if TREE(); if ($self->{subtables}) { foreach my $child (@{$ts->{children}}) { next if $child->{captured}; $self->_capture_table($child, 'subtable'); $child->{slice_columns} = 0; $child->{keep_headers} = 1; $child->{headers} = ''; } } $ts->{captured} = 1; $self->{_tables}{$ts->{depth}}{$ts->{count}} = $ts; push(@{$self->{_ts_sequential}}, $ts); } sub current_table { my $self = shift; $self->{_tablestack}[$#{$self->{_tablestack}}]; } sub _reset_state { my $self = shift; $self->{_cdepth} = -1; $self->{_tablestack} = []; $self->{_tables} = {}; $self->{_ts_sequential} = []; $self->{_counts} = []; $self->{_in_a_table} = 0; $self->{_parsing} = 0; } sub _emsg { my $self = shift; my $fh = $self->{error_handle}; return unless defined $_[0]; print $fh @_; } ########## { package HTML::TableExtract::Table; use strict; use Carp; *TREE = *HTML::TableExtract::TREE; sub new { my $that = shift; my $class = ref($that) || $that; # Note: # - 'depth' and 'count' are where this table were found. # - 'tdepth' and 'tcount' are target constraints on which to trigger. # - 'headers' represent a target constraint, location independent. # - 'attribs' represent target table tag constraints my $self = { umbrella => 0, in_row => 0, in_cell => 0, rc => -1, cc => -1, grid => [], translation => [], hrow => [], order => [], children => [], captured => 0, debug => 0, }; $self->{_rastamon} = HTML::TableExtract::Rasterize->make_rasterizer(); bless $self, $class; my %parms = @_; # Depth and Count -- this is the absolute address of the table. croak "Absolute depth required\n" unless defined $parms{depth}; croak "Count required\n" unless defined $parms{count}; croak "Counts required\n" unless defined $parms{counts}; foreach (keys %parms) { $self->{$_} = $parms{$_}; } # Register lineage my $pts = $self->{parent_table}; $self->lineage($pts || undef); push(@{$pts->{children}}, $self) if ($pts); delete $self->{parent_table}; $self; } sub _anchor_item { # anchor the reference to a cell in our grid -- in TREE mode this is # a reference to a data element, otherwise it's a reference to an # empty scalar in which we will collect our text. my($self, @res) = @_; my $row = $self->{grid}[-1]; my $item; if (@res && ref $res[0]) { $item = $res[0]; } else { my $scalar_ref; $item = \$scalar_ref; } push(@$row, $item); } sub _gridalias { my $self = shift; $self->{gridalias} ||= $self->_make_gridalias; } sub _grid_map { # using our rasterized template, flesh out our captured items which # are still in 'tree' format my $self = shift; my $template = $self->_rasterizer->(); my $grid = $self->{grid}; # drop empty rows if ($self->{debug}) { foreach (0 .. $#$grid) { next if @{$grid->[$_]}; $self->_emsg("Dropping empty row $_\n"); } } @$grid = grep(@$_, @$grid); foreach my $r (0 .. $#$template) { my $row = $grid->[$r]; my $trow = $template->[$r]; $self->_emsg("Flesh row $r ($#$row) to $#$trow\n") if $self->{debug} > 1; foreach my $c (0 .. $#$trow) { print STDERR $trow->[$c] ? '1' : '0' if $self->{debug} > 1; if ($trow->[$c]) { if (! defined $row->[$c]) { $row->[$c] = \undef; } next; } else { my $scalar; splice(@$row, $c, 0, \$scalar); } } print STDERR "\n" if $self->{debug} > 1; croak "row $r splice mismatch: $#$row vs $#$trow\n" unless $#$row == $#$trow; } $grid; } sub _make_gridalias { # our aliased grid will have references in masked cells to the same # cell that is covering it via spanning. my $self = shift; my $grid = $self->{grid}; my $template = $self->_rasterizer->(); my(@gridalias, @translation); $gridalias[$_] = [@{$grid->[$_]}] foreach 0 .. $#$grid; foreach my $r (0 .. $#gridalias) { my $row = $gridalias[$r]; foreach my $c (0 .. $#$row) { my $tcell = $template->[$r][$c] || next; my($rspan, $cspan) = @$tcell; foreach my $rs (0 .. $rspan-1) { foreach my $cs (0 .. $cspan-1) { $gridalias[$r + $rs][$c + $cs] = $grid->[$r][$c]; $translation[$r + $rs][$c + $cs] = "$r,$c"; } } } } $self->{translation} = \@translation; $self->{gridalias} = \@gridalias; } ### Constraint tests sub _check_dtrigger { # depth my $self = shift; return 1 unless defined $self->{tdepth}; $self->{tdepth} == $self->{depth} ? 1 : 0; } sub _check_ctrigger { # count my $self = shift; return 1 unless defined $self->{tcount}; return 1 if (exists $self->{counts}[$self->{depth}] && $self->{tcount} == $self->{counts}[$self->{depth}]); return 0; } sub _check_atrigger { # attributes my $self = shift; return 1 unless scalar keys %{$self->{tattribs}}; return 0 unless scalar keys %{$self->{attribs}}; my $a_hit = 1; foreach my $attrib (keys %{$self->{tattribs}}) { if (! defined $self->{attribs}{$attrib}) { $a_hit = 0; last; } if (! defined $self->{tattribs}{$attrib}) { # undefined, but existing, target attribs are wildcards next; } if ($self->{tattribs}{$attrib} ne $self->{attribs}{$attrib}) { $a_hit = 0; last; } } $self->_emsg("Matched attributes\n") if $self->{debug} > 3 && $a_hit; $a_hit; } sub _check_htrigger { # headers my $self = shift; return 1 if $self->{umbrella}; return 1 unless $self->{headers}; ROW: foreach my $r (0 .. $#{$self->{grid}}) { $self->_reset_hits; my $hpat = $self->_header_pattern; my @hits; foreach my $c (0 .. $#{$self->{grid}[$r]}) { my $ref = $self->{grid}[$r][$c]; my $target = ''; my $ref_type = ref $ref; if ($ref_type) { if ($ref_type eq 'SCALAR') { my $item = $$ref; if ($self->{keep_html} && $self->{strip_html_on_match}) { my $stripper = HTML::TableExtract::StripHTML->new; $target = $stripper->strip($item); } else { $target = $item; } } else { if (($self->{keep_html} || TREE()) && $self->{strip_html_on_match}) { $target = $ref->as_text; } else { $target = $ref->as_HTML; } } } $target = defined $target ? $target : ''; $self->_emsg("attempt match on $target ($hpat): ") if $self->{debug} >= 5; if ($target =~ $hpat) { my $hit = $1; $self->_emsg("($hit)\n") if $self->{debug} >= 5; # Get rid of the header segment that matched so we can tell # when we're through with all header patterns. my $real_hit; foreach (sort _header_string_sort keys %{$self->{hits_left}}) { if ($hit =~ /$_/im) { delete $self->{hits_left}{$_}; $real_hit = $_; $hpat = $self->_header_pattern; last; } } if (defined $real_hit) { if ($self->{debug} >= 4) { my $str = $ref_type eq 'SCALAR' ? $$ref : $ref->as_HTML; $self->_emsg("HIT on '$hit' ($real_hit) in $str ($r,$c)\n"); } push(@hits, $hit); # $self->{hits}{$c} = $real_hit; push(@{$self->{order}}, $c); if (!%{$self->{hits_left}}) { # Successful header row match ++$self->{head_found}; $self->{hrow_index} = $r; $self->{hrow} = $self->{grid}[$r]; last ROW; } } } elsif ($self->{debug} >= 5) { $self->_emsg("0\n"); } } if ($self->{debug} && @hits) { my $str = "Incomplete header match "; $str .= "(left: " . join(', ', sort keys %{$self->{hits_left}}) . ") "; $str .= "in row $r, resetting scan"; $str .= "\n"; $self->_emsg($str); } } $self->{head_found}; } sub _check_triggers { my $self = shift; return 1 if $self->{umbrella}; $self->_check_dtrigger && $self->_check_ctrigger && $self->_check_atrigger && $self->_check_htrigger; } ### Maintain table context sub _enter_row { my $self = shift; if ($self->{in_row}) { $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), forcing exit of row $self->{rc} due to new row\n") if $self->{debug}; $self->_exit_row; } ++$self->{rc}; ++$self->{in_row}; push(@{$self->{grid}}, []) } sub _exit_row { my $self = shift; if ($self->{in_row}) { if ($self->{in_cell}) { $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), forcing exit of cell ($self->{rc}, $self->{cc}) due to new row\n") if $self->{debug}; $self->_exit_cell; } $self->{in_row} = 0; $self->{cc} = -1; } else { $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), extraneous ignored after row $self->{rc}\n") if $self->{debug}; } } sub _enter_cell { my $self = shift; if ($self->{in_cell}) { $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), forcing exit of cell ($self->{rc},$self->{cc}) due to new cell\n") if $self->{debug}; $self->_exit_cell; } if (!$self->{in_row}) { # Go ahead and try to recover from mangled HTML, because we care. $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), inferring as row $self->{rc}\n") if $self->{debug}; $self->_enter_row; } ++$self->{cc}; ++$self->{in_cell}; my %attrs = ref $_[1] ? %{$_[1]} : {}; my $rspan = $attrs{rowspan} || 1; my $cspan = $attrs{colspan} || 1; } sub _exit_cell { my $self = shift; if ($self->{in_cell}) { $self->{in_cell} = 0; } else { $self->_emsg("Mangled HTML in table ($self->{depth},$self->{count}), extraneous ignored in row $self->{rc}\n") if $self->{debug}; } } # Header stuff sub _header_pattern { my($self, @headers) = @_; my $str = join('|', map("($_)", sort _header_string_sort keys %{$self->{hits_left}} )); my $hpat = qr/($str)/im; $self->_emsg("HPAT: /$hpat/\n") if $self->{debug} >= 2; $self->{hpat} = $hpat; } sub _header_string_sort { # this ensures that supersets appear before subsets in our header # search pattern, eg, '10' appears before '1' and 'hubbabubba' # appears before 'hubba'. if ($a =~ /^$b/) { return -1; } elsif ($b =~ /^$a/) { return 1; } else { return $b cmp $a; } } # Report methods sub depth { shift->{depth} } sub count { shift->{count} } sub coords { my $self = shift; ($self->depth, $self->count); } sub row_count { shift->{rc} } sub col_count { shift->{cc} } sub tree { my $self = shift; @_ ? $self->{_tree_ref} = shift : $self->{_tree_ref}; } sub lineage { my $self = shift; $self->{lineage} ||= []; if (@_) { my $pts = shift; my(@lineage, $pcoords); if ($pts) { foreach my $pcoord ($pts->lineage) { push(@lineage, [@$pcoord]); } $pcoords = [$pts->depth, $pts->count, $pts->{rc}, $pts->{cc}]; push(@lineage, $pcoords); } $self->{lineage} = \@lineage; } @{$self->{lineage}}; } sub rows { shift->_rows(0) } sub space_rows { my $self = shift; $self->_rows(1); } sub _rows { my $self = shift; my $alias = shift; my @ri = $self->row_indices; my @rows; my $grid = $alias ? $self->_gridalias : $self->{grid}; foreach ($self->row_indices) { push(@rows, scalar $self->_slice_and_normalize_row($grid->[$_])); } wantarray ? @rows : \@rows; } sub columns { my $self = shift; my @cols; my @rows = $self->rows; foreach my $row (@rows) { foreach my $c (0 .. $#$row) { $cols[$c] ||= []; push(@{$cols[$c]}, $row->[$c]); } } @cols; } sub row_indices { my $self = shift; my $start_index = 0; if ($self->{headers}) { $start_index = $self->hrow_index; $start_index += 1 unless $self->{keep_headers}; } $start_index .. $#{$self->{grid}}; } sub col_indices { my $self = shift; my $row = $self->{grid}[0]; 0 .. $#$row; } sub row { my $self = shift; my $r = shift; $r <= $#{$self->{grid}} or croak "row $r out of range ($#{$self->{grid}})\n"; my @ri = $self->row_indices; my @row = $self->_slice_and_normalize_row( $self->{grid}[($self->row_indices)[$r]] ); wantarray ? @row : \@row; } sub _slice_and_normalize_row { my $self = shift; my $rowref = shift; my @row; if ($self->{automap} && $self->_map_makes_a_difference) { @row = @{$rowref}[$self->column_map]; } else { @row = @$rowref; } @row = map($self->_cell_to_content($_), @row); wantarray ? @row : \@row; } sub column { my $self = shift; my $c = shift; my @column; foreach my $row ($self->rows) { push(@column, $self->cell($row, $c)); } wantarray ? @column : \@column; } sub cell { my $self = shift; my($r, $c) = @_; my $row = $self->row($r); $c <= $#$row or croak "Column $c out of range ($#$row)\n"; $self->_cell_to_content($row->[$c]); } sub _cell_to_content { my $self = shift; @_ or croak "cell item required\n"; my $cell = shift; return $cell unless ref $cell; return $cell if TREE(); return $$cell; } sub space { my $self = shift; my($r, $c) = @_; my $gridalias = $self->_gridalias; $r <= $#$gridalias or croak "row $r out of range ($#$gridalias)\n"; my $row = $gridalias->[$r]; $c <= $#$row or croak "Column $c out of range ($#$row)\n"; $self->_cell_to_content($row->[$c]); } sub source_coords { my $self = shift; my($r, $c) = @_; $r <= $#{$self->{translation}} or croak "row $r out of range ($#{$self->{translation}})\n"; my $row = $self->{translation}[$r]; $c <= $#$row or croak "Column $c out of range ($#$row)\n"; split(/,/, $self->{translation}[$r][$c]); } sub hrow_index { my $self = shift; $self->{hrow_index}; } sub hrow { my $self = shift; if ($self->{automap} && $self->_map_makes_a_difference) { return map(ref $_ ? $$_ : $_, @{$self->{hrow}}[$self->column_map]); } else { return map(ref $_ ? $$_ : $_, @{$self->{hrow}}); } } sub column_map { # Return the column numbers of this table in the same order as the # provided headers. my $self = shift; if ($self->{headers}) { # First we order the original column counts by taking a hash slice # based on the original header order. The resulting original # column numbers are mapped to the actual content indicies since # we could have a sparse slice. my %order; foreach (keys %{$self->{hits}}) { $order{$self->{hits}{$_}} = $_; } return @order{@{$self->{headers}}}; } else { return 0 .. $#{$self->{grid}[0]}; } } sub _map_makes_a_difference { my $self = shift; return 0 unless $self->{slice_columns}; my $diff = 0; my @order = $self->column_map; my @sorder = sort { $a <=> $b } @order; ++$diff if $#order != $#sorder; ++$diff if $#sorder != $#{$self->{grid}[0]}; foreach (0 .. $#order) { if ($order[$_] != $sorder[$_]) { ++$diff; last; } } $diff; } sub _add_text { my($self, $txt) = @_; my $r = $self->{rc}; my $c = $self->{cc}; my $row = $self->{grid}[$r]; ${$row->[$c]} .= $txt; $txt; } sub _reset_hits { my $self = shift; return unless $self->{headers}; $self->{hits} = {}; $self->{order} = []; foreach (@{$self->{headers}}) { ++$self->{hits_left}{$_}; } 1; } sub _rasterizer { shift->{_rastamon} } sub report { # Print out a summary of this table, including depth/count my($self, $include_content, $col_sep) = @_; $col_sep ||= ':'; my $str; $str .= "TABLE(" . $self->depth . ", " . $self->count . ')'; if ($include_content) { $str .= ":\n"; foreach my $row ($self->rows) { $str .= join($col_sep, @$row) . "\n"; } } else { $str .= "\n"; } $str; } sub dump { my $self = shift; $self->_emsg($self->report(@_)); } sub _emsg { my $self = shift; my $fh = $self->{error_handle}; print $fh @_; } } ########## { package HTML::TableExtract::Rasterize; # Provide a closure that will rasterize (turn into a grid) a table # from a tree structure based on repeated data element calls with # rowspan and colspan information. Not as straight forward as it # seems...see test cases for an example bugaboo. my $DEBUG = 0; sub make_rasterizer { my $pkg = shift; my(@grid, @row_spinner, @col_spinner); my $empty_row_offset = 0; sub { return \@grid unless @_; my($row_num, $rspan, $cspan) = @_; $rspan = 1 unless $rspan > 1; $cspan = 1 unless $cspan > 1; my($rspin_propogate, $row_added); my $trigger = $#grid + $empty_row_offset; if ($row_num > $trigger) { # adjust for having been handed a row that skips a prior row, # otherwise the next cell will land in a wrong row. Hopefully # this doesn't happen too often but I've seen it in the wild! if ($row_num - $trigger > 1) { $empty_row_offset += $row_num - $trigger - 1; } # add new row $row_added = 1; my @new_row; # first add new row spinner if ($row_spinner[-1] && $col_spinner[-1]) { push(@row_spinner, $row_spinner[-1]); $rspin_propogate = 1; } else { push(@row_spinner, $cspan - 1); } # spin columns foreach (@col_spinner) { if ($_) { push(@new_row, 0); --$_; } else { push(@new_row, undef); } } @new_row = (undef) unless @new_row; push(@grid, \@new_row); } my $current_row = $grid[-1]; # locate next available cell in row my $col; foreach my $ci (0 .. $#$current_row) { if (! defined $current_row->[$ci]) { $col = $ci; last; } } if (! defined $col) { ADDCOL: while (! defined $col) { # if no cells were available, add a column foreach my $ri (0 .. $#grid) { my $row = $grid[$ri]; my $cspan_count = $row_spinner[$ri]; if (!$cspan_count) { push(@$row, undef); } else { push(@$row, 0); --$row_spinner[$ri]; } } push(@col_spinner, $col_spinner[-1]); foreach my $ci (0 .. $#$current_row) { if (! defined $current_row->[$ci]) { $col = $ci; last ADDCOL; } } } $col_spinner[-1] = $rspan - 1 if $col == $#$current_row; $row_spinner[$#grid] = $cspan - 1; } # we now have correct coordinates for this element $current_row->[$col] = [$rspan, $cspan]; $col_spinner[$col] = $rspan - 1; # if this is an embedded placment (not a trailing element), use up # the cspan if ($col < $#$current_row) { my $offset = 1; my $row_span = $col_spinner[$col]; if ($col + $row_spinner[-1] < $#$current_row && $row_added && !$rspin_propogate) { # cell is spun out -- clear spinner unless it inherited cspan # from a cell above $row_spinner[-1] = 0; } while ($offset < $cspan) { my $cursor = $col + $offset; $current_row->[$cursor] = 0; $col_spinner[$cursor] = $row_span; ++$offset; if ($col + $offset > $#$current_row) { $row_spinner[-1] = $cspan - $offset; last; } } } if ($DEBUG) { foreach my $r (0 .. $#grid) { my $row = $grid[$r]; foreach my $c (0 .. $#$row) { if (defined $row->[$c]) { print STDERR $row->[$c] ? 1 : 0; } else { print STDERR '?'; } } print STDERR " $row_spinner[$r]\n"; } print STDERR "\n"; foreach (@col_spinner) { print STDERR defined $_ ? $_ : '?'; } print STDERR "\n\n-----\n\n"; } return \@grid; } } } ########## { package HTML::TableExtract::StripHTML; use vars qw(@ISA); use HTML::Parser; @ISA = qw(HTML::Parser); sub tag { my($self, $tag, $num) = @_; $self->{_htes_inside}{$tag} += $num; } sub text { my $self = shift; return if $self->{_htes_inside}{script} || $self->{_htes_inside}{style}; $self->{_htes_tidbit} .= $_[0]; } sub new { my $class = shift; my $self = HTML::Parser->new( api_version => 3, handlers => [start => [\&tag, "self, tagname, '+1'"], end => [\&tag, "self, tagname, '-1'"], text => [\&text, "self, dtext"], ], marked_sections => 1, ); bless $self, $class; } sub strip { my $self = shift; $self->parse(shift); $self->eof; $self->{_htes_tidbit}; } } 1; __END__ =head1 NAME HTML::TableExtract - Perl module for extracting the content contained in tables within an HTML document, either as text or encoded element trees. =head1 SYNOPSIS # Matched tables are returned as table objects; tables can be matched # using column headers, depth, count within a depth, table tag # attributes, or some combination of the four. # Example: Using column header information. # Assume an HTML document with tables that have "Date", "Price", and # "Cost" somewhere in a row. The columns beneath those headings are # what you want to extract. They will be returned in the same order as # you specified the headers since 'automap' is enabled by default. use HTML::TableExtract; $te = HTML::TableExtract->new( headers => [qw(Date Price Cost)] ); $te->parse($html_string); # Examine all matching tables foreach $ts ($te->tables) { print "Table (", join(',', $ts->coords), "):\n"; foreach $row ($ts->rows) { print join(',', @$row), "\n"; } } # Shorthand...top level rows() method assumes the first table found in # the document if no arguments are supplied. foreach $row ($te->rows) { print join(',', @$row), "\n"; } # Example: Using depth and count information. # Every table in the document has a unique depth and count tuple, so # when both are specified it is a unique table. Depth and count both # begin with 0, so in this case we are looking for a table (depth 2) # within a table (depth 1) within a table (depth 0, which is the top # level HTML document). In addition, it must be the third (count 2) # such instance of a table at that depth. $te = HTML::TableExtract->new( depth => 2, count => 2 ); $te->parse_file($html_file); foreach $ts ($te->tables) { print "Table found at ", join(',', $ts->coords), ":\n"; foreach $row ($ts->rows) { print " ", join(',', @$row), "\n"; } } # Example: Using table tag attributes. # If multiple attributes are specified, all must be present and equal # for match to occur. $te = HTML::TableExtract->new( attribs => { border => 1 } ); $te->parse($html_string); foreach $ts ($te->tables) { print "Table with border=1 found at ", join(',', $ts->coords), ":\n"; foreach $row ($ts->rows) { print " ", join(',', @$row), "\n"; } } # Example: Extracting as an HTML::Element tree structure # Rather than extracting raw text, the html can be converted into a # tree of element objects. The HTML document is composed of # HTML::Element objects and the tables are HTML::ElementTable # structures. Using this, the contents of tables within a document can # be edited in-place. use HTML::TableExtract qw(tree); $te = HTML::TableExtract->new( headers => qw(Fee Fie Foe Fum) ); $te->parse_file($html_file); $table = $te->first_table_found; $table_tree = $table->tree; $table_tree->cell(4,4)->replace_content('Golden Goose'); $table_html = $table_tree->as_HTML; $table_text = $table_tree->as_text; $document_tree = $te->tree; $document_html = $document_tree->as_HTML; =head1 DESCRIPTION HTML::TableExtract is a subclass of HTML::Parser that serves to extract the information from tables of interest contained within an HTML document. The information from each extracted table is stored in table objects. Tables can be extracted as text, HTML, or HTML::ElementTable structures (for in-place editing or manipulation). There are currently four constraints available to specify which tables you would like to extract from a document: I, I, I, and I. I, the most flexible and adaptive of the techniques, involves specifying text in an array that you expect to appear above the data in the tables of interest. Once all headers have been located in a row of that table, all further cells beneath the columns that matched your headers are extracted. All other columns are ignored: think of it as vertical slices through a table. In addition, TableExtract automatically rearranges each row in the same order as the headers you provided. If you would like to disable this, set I to 0 during object creation, and instead rely on the column_map() method to find out the order in which the headers were found. Furthermore, TableExtract will automatically compensate for cell span issues so that columns are really the same columns as you would visually see in a browser. This behavior can be disabled by setting the I parameter to 0. HTML is stripped from the entire textual content of a cell before header matches are attempted -- unless the I parameter was enabled. I and I are more specific ways to specify tables in relation to one another. I represents how deeply a table resides in other tables. The depth of a top-level table in the document is 0. A table within a top-level table has a depth of 1, and so on. Each depth can be thought of as a layer; tables sharing the same depth are on the same layer. Within each of these layers, I represents the order in which a table was seen at that depth, starting with 0. Providing both a I and a I will uniquely specify a table within a document. I match based on the attributes of the html EtableE tag, for example, boder widths or background color. Each of the I, I, I, and I specifications are cumulative in their effect on the overall extraction. For instance, if you specify only a I, then you get all tables at that depth (note that these could very well reside in separate higher- level tables throughout the document since depth extends across tables). If you specify only a I, then the tables at that I from all depths are returned (i.e., the Ith occurrence of a table at each depth). If you only specify I, then you get all tables in the document containing those column headers. If you have specified multiple constraints of I, I, I, and I, then each constraint has veto power over whether a particular table is extracted. If no I, I, I, or I are specified, then all tables match. When extracting only text from tables, the text is decoded with HTML::Entities by default; this can be disabled by setting the I parameter to 0. =head2 Extraction Modes The default mode of extraction for HTML::TableExtract is raw text or HTML. In this mode, embedded tables are completely decoupled from one another. In this case, HTML::TableExtract is a subclass of HTML::Parser: use HTML::TableExtract; Alternativevly, tables can be extracted as HTML::ElementTable structures, which are in turn embedded in an HTML::Element tree representing the entire HTML document. Embedded tables are not decoupled from one another since this tree structure must be manitained. In this case, HTML::TableExtract is a subclass of HTML::TreeBuilder (itself a subclass of HTML:::Parser): use HTML::TableExtract qw(tree); In either case, the basic interface for HTML::TableExtract and the resulting table objects remains the same -- all that changes is what you can do with the resulting data. HTML::TableExtract is a subclass of HTML::Parser, and as such inherits all of its basic methods such as C and C. During scans, C, C, and C are utilized. Feel free to override them, but if you do not eventually invoke them in the SUPER class with some content, results are not guaranteed. =head2 Advice The main point of this module was to provide a flexible method of extracting tabular information from HTML documents without relying to heavily on the document layout. For that reason, I suggest using I whenever possible -- that way, you are anchoring your extraction on what the document is trying to communicate rather than some feature of the HTML comprising the document (other than the fact that the data is contained in a table). =head1 METHODS The following are the top-level methods of the HTML::TableExtract object. Tables that have matched a query are actually returned as separate objects of type HTML::TableExtract::Table. These table objects have their own methods, documented further below. =head2 CONSTRUCTOR =over =item new() Return a new HTML::TableExtract object. Valid attributes are: =over =item headers Passed as an array reference, headers specify strings of interest at the top of columns within targeted tables. They can be either strings or regular expressions (qr//). If they are strings, they will eventually be passed through a non-anchored, case-insensitive regular expression, so regexp special characters are allowed. The table row containing the headers is B returned, unless C was specified or you are extracting into an element tree. In either case the header row can be accessed via the hrow() method from within the table object. Columns that are not beneath one of the provided headers will be ignored unless C was set to 0. Columns will, by default, be rearranged into the same order as the headers you provide (see the I parameter for more information) I C is 0. Additionally, by default columns are considered what you would see visually beneath that header when the table is rendered in a browser. See the C parameter for more information. HTML within a header is stripped before the match is attempted, unless the C parameter was specified and C is false. =item depth Specify how embedded in other tables your tables of interest should be. Top-level tables in the HTML document have a depth of 0, tables within top-level tables have a depth of 1, and so on. =item count Specify which table within each depth you are interested in, beginning with 0. =item attribs Passed as a hash reference, attribs specify attributes of interest within the HTML EtableE tag itself. =item automap Automatically applies the ordering reported by column_map() to the rows returned by rows(). This only makes a difference if you have specified I and they turn out to be in a different order in the table than what you specified. Automap will rearrange the columns in the same order as the headers appear. To get the original ordering, you will need to take another slice of each row using column_map(). I is enabled by default. =item slice_columns Enabled by default, this option controls whether vertical slices are returned from under headers that match. When disabled, all columns of the matching table are retained, regardles of whether they had a matching header above them. Disabling this also disables C. =item keep_headers Disabled by default, and only applicable when header constraints have been specified, C will retain the matching header row as the first row of table data when enabled. This option has no effect if extracting into an element tree tructure. In any case, the header row is accessible from the table method C. =item gridmap Controls whether the table contents are returned as a grid or a tree. ROWSPAN and COLSPAN issues are compensated for, and columns really are columns. Empty phantom cells are created where they would have been obscured by ROWSPAN or COLSPAN settings. This really becomes an issue when extracting columns beneath headers. Enabled by default. =item subtables Extract all tables embedded within matched tables. =item decode Automatically decode retrieved text with HTML::Entities::decode_entities(). Enabled by default. Has no effect if C was specified or if extracting into an element tree structure. =item br_translate Translate
tags into newlines. Sometimes the remaining text can be hard to parse if the
tag is simply dropped. Enabled by default. Has no effect if I is enabled or if extracting into an element tree structure. =item keep_html Return the raw HTML contained in the cell, rather than just the visible text. Embedded tables are B retained in the HTML extracted from a cell. Patterns for header matches must take into account HTML in the string if this option is enabled. This option has no effect if extracting into an elment tree structure. =item strip_html_on_match When C is enabled, HTML is stripped by default during attempts at matching header strings (so if C is not enabled and C is, you would have to include potential HTML tags in the regexp for header matches). Stripped header tags are replaced with an empty string, e.g. 'hot dEemEogE/emE' would become 'hot dog' before attempting a match. =item error_handle Filehandle where error messages are printed. STDERR by default. =item debug Prints some debugging information to STDERR, more for higher values. If C was provided, messages are printed there rather than STDERR. =back =back =head2 REGULAR METHODS The following methods are invoked directly from an HTML::TableExtract object. =over =item depths() Returns all depths that contained matched tables in the document. =item counts($depth) For a particular depth, returns all counts that contained matched tables. =item table($depth, $count) For a particular depth and count, return the table object for the table found, if any. =item tables() Return table objects for all tables that matched. Returns an empty list if no tables matched. =item first_table_found() Return the table state object for the first table matched in the document. Returns undef if no tables were matched. =item current_table() Returns the current table object while parsing the HTML. Only useful if you're messing around with overriding HTML::Parser methods. =item tree() If the module was invoked in tree extraction mode, returns a reference to the top node of the HTML::Element tree structure for the entire document (which includes, ultimately, all tables within the document). =item tables_report([$show_content, $col_sep]) Return a string summarizing extracted tables, along with their depth and count. Optionally takes a C<$show_content> flag which will dump the extracted contents of each table as well with columns separated by C<$col_sep>. Default C<$col_sep> is ':'. =item tables_dump([$show_content, $col_sep]) Same as C except dump the information to STDOUT. =item start =item end =item text These are the hooks into HTML::Parser. If you want to subclass this module and have things work, you must at some point call these with content. =back =head2 DEPRECATED METHODS Tables used to be called 'table states'. Accordingly, the following methods still work but have been deprecated: =over =item table_state() Is now table() =item table_states() Is now tables() =item first_table_state_found() Is now first_table_found() =back =head2 TABLE METHODS The following methods are invoked from an HTML::TableExtract::Table object, such as those returned from the C method. =over =item rows() Return all rows within a matched table. Each row returned is a reference to an array containing the text, HTML, or reference to the HTML::Element object of each cell depending the mode of extraction. Tables with rowspan or colspan attributes will have some cells containing undef. Returns a list or a reference to an array depending on context. =item columns() Return all columns within a matched table. Each column returned is a reference to an array containing the text, HTML, or reference to HTML::Element object of each cell depending on the mode of extraction. Tables with rowspan or colspan attributes will have some cells containing undef. =item row($row) Return a particular row from within a matched table either as a list or an array reference, depending on context. =item column($col) Return a particular column from within a matched table as a list or an array reference, depending on context. =item cell($row,$col) Return a particular item from within a matched table, whether it be the text, HTML, or reference to the HTML::Element object of that cell, depending on the mode of extraction. If the cell was covered due to rowspan or colspan effects, will return undef. =item space($row,$col) The same as cell(), except in cases where the given coordinates were covered due to rowspan or colspan issues, in which case the content of the covering cell is returned rather than undef. =item depth() Return the depth at which this table was found. =item count() Return the count for this table within the depth it was found. =item coords() Return depth and count in a list. =item tree() If the module was invoked in tree extraction mode, this accessor provides a reference to the HTML::ElementTable structure encompassing the table. =item hrow() Returns the header row as a list when headers were specified as a constraint. If C was specified initially, this is equivalent to the first row returned by the C method. =item column_map() Return the order (via indices) in which the provided headers were found. These indices can be used as slices on rows to either order the rows in the same order as headers or restore the rows to their natural order, depending on whether the rows have been pre-adjusted using the I parameter. =item lineage() Returns the path of matched tables that led to matching this table. The path is a list of array refs containing depth, count, row, and column values for each ancestor table involved. Note that corresponding table objects will not exist for ancestral tables that did not match specified constraints. =back =head1 NOTES ON TREE EXTRACTION MODE As mentioned above, HTML::TableExtract can be invoked in 'tree' mode where the resulting HTML and extracted tables are encoded in HTML::Element tree structures: use HTML::TableExtract 'tree'; There are a number of things to take note of while using this mode. The entire HTML document is encoded into an HTML::Element tree. Each table is part of this structure, but nevertheless is tracked separately via an HTML::ElementTable structure, which is a specialized form of HTML::Element tree. The HTML::ElementTable objects are accessible by invoking the tree() method from within each table object returned by HTML::TableExtract. The HTML::ElementTable objects have their own row(), col(), and cell() methods (among others). These are not to be confused with the row() and column() methods provided by the HTML::TableExtract::Table objects. For example, the row() method from HTML::ElementTable will provide a reference to a 'glob' of all the elements in that row. Actions (such as setting attributes) performed on that row reference will affect all elements within that row. On the other hand, the row() method from the HTML::TableExtract::Table object will return an array (either by reference or list, depending on context) of the contents of each cell within the row. In tree mode, the content is represented by individual references to each cell -- these are references to the same HTML::Element objects that reside in the HTML::Element tree. The cell() methods provided in both cases will therefore return references to the same object. The exception to this is when a 'cell' in the table grid was originally 'covered' due to rowspan or colspan issues -- in this case the cell content will be undef. Likewise, the row() or column() methods from HTML::TableExtract::Table objects will return arrays potentially containing a mixture of object references and undefs. If you're going to be doing lots of manipulation of the table elements, it might be more efficient to access them via the methods provided by the HTML::ElementTable object instead. See L for more information on how to manipulate those objects. An alternative to the cell() method in HTML::TableExtract::Table is the space() method. It is largely similar to cell(), except when given coordinates of a cell that was covered due to rowspan or colspan effects, it will return the contents of the cell that was covering that space rather than undef. So if, for example, cell (0,0) had a rowspan of 2 and colspan of 2, cell(1,1) would return undef and space(1,1) would return the same content as cell(0,0) or space(0,0). =head1 REQUIRES HTML::Parser(3), HTML::Entities(3) =head1 OPTIONALLY REQUIRES HTML::TreeBuilder(3), HTML::ElementTable(3) =head1 AUTHOR Matthew P. Sisk, EFE =head1 COPYRIGHT Copyright (c) 2000-2006 Matthew P. Sisk. All rights reserved. All wrongs revenged. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO HTML::Parser(3), HTML::TreeBuilder(3), HTML::ElementTable(3), perl(1). =cut In honor of fragmented markup languages and sugar mining: The Good and The Bad Ted Hawkins (1936-1994) Living is good when you have someone to share it with Laughter is bad when there is no one there to share it with Talking is sad if you've got no one to talk to Dying is good when the one you love grows tired of you Sugar is no good once it's cast among the white sand What the point in pulling the gray hairs from among the black strands When you're old you shouldn't walk in the fast lane Oh ain't it useless to keep trying to draw true love from that man He'll hurt you, Yes just for the sake of hurting you and he'll hate you if you try to love him just the same He'll use you and everything you have to offer him On your way girl Get out and find you someone new