"Andrew" <tech7890@yahoo.com> wrote:

: Does anyone know of a perl script I may download or have a perl script of
: your own that lines up html table tags through indentation?

The code in Synopsis section of the HTML::PrettyPrinter pod is a
pretty good foundation. Season to taste with the set_nl_before() and
set_nl_after() methods, and voila.

Here's the one I've been using for some time now. Not bulletproof,
but it whips most HTML samples into reasonable shape.

#!perl
use warnings;
use strict;
use HTML::TreeBuilder;
use HTML::PrettyPrinter;
use File::Slurp qw(write_file);

die "Usage: $0 file1 [file2 file3 ...]\n"
unless @ARGV;
for(@ARGV) {
print "$_ : reading...";
my $html = pretty( tree($_) );
# Backup original file. No check on whether file exists
# already or if rename succeeds. Use at your own risk.
rename $_, "$_.bak";
print "writing...";
write_file( $_, $html );
print "done.\n";
}

sub tree {
my($filename) = @_;
my $tree = HTML::TreeBuilder->new(
store_comments => 1,
warn => 1,
);
$tree->parse_file($filename);
return $tree;
}

sub pretty {
# tags that get newlines before & after
my @nl_before_after = qw(
h1 h2 h3 h4 h5
dl dd dt
table td th tr
ul li ol
body html p div style img map
);
# tags that get newline before
my @nl_before = qw( a head link meta title script br );
# tags that get newline after
my @nl_after = qw( );

my($tree) = @_;
my $hpp = HTML::PrettyPrinter->new(
tabify => 0,
linelength => 1e6,
quote_attr => 1,
allow_forced_nl => 1,
);
# Screw any defaults. I want it my way.
$hpp->set_nl_before(0, 'all!');
$hpp->set_nl_after(0, 'all!');
$hpp->set_force_nl(1, qw / br /);
$hpp->set_nl_before(1, @nl_before_after, @nl_before);
$hpp->set_nl_after(1, @nl_before_after, @nl_after);
local $" = '';
return "@{ $hpp->format($tree) }";
}