#!/usr/common/bin/perl

# prints table contents from file of HTML

use strict subs;
use HTML::TableExtract;
use Getopt::Std;

$ret = getopts('qc');

sub print_tables {
    my ($table, $row, $cell);
    my $tc = 0;
    my $table_extractor = new HTML::TableExtract();
    $table_extractor->parse($_[0]);
    foreach $table ($table_extractor->table_states) {
	print "TABLE $tc:\n" unless $opt_q; $tc++;
        my $rc = 0;
        foreach $row ($table->rows) {
	    print "ROW $rc:\n" unless $opt_q; $rc++;
            foreach $cell ( @$row ) {
                $cell =~ s/\n/ /g;
		$cell =~ s/[ \t]+/ /g;
		$cell =~ s/^[ \t]//;
		$cell =~ s/[ \t]$//;
		$cell =~ s/ *<\/td *//g;
		if ($opt_c)
		{
			$cell =~ s/[\240\r\n\t]//g;
			$cell =~ s/^ *//;
			$cell =~ s/ *$//;
		}
                print "$cell|";
            }
            print "\n";
        }
    }
}

sub main {
    	my ($content);
	while(<>)
	{
		$content .= $_;
	}
	print_tables ($content);
}

main ();

