in reply to Re: Re: X(ht)ML Source Formatting
in thread X(ht)ML Source Formatting

Sorry, saw that you were using HTML::TreeBuilder, and assumed you were mainly concerned with indenting.

Actually, the modules take care of most of what you want including:

  1. makes sure there are no improperly nested elements
  2. automatically lowercasing element and attribute names.
  3. closes all tags, if you pass is an empty hashref to the as_HTML() method (\%optional_end_tags).
  4. quotes attributes

But you still have to deal with closing empty elements like <br> which you could do fix like this (you'll have to play around with trying to fix <img> and others):

use strict;
use HTML::TreeBuilder;

my $root = HTML::TreeBuilder->new;
my $html = $root->parse_file('a.htm');
my @br = $html->look_down('_tag','br');
my  $literal = HTML::Element->new('~literal','text' => '<br />');
foreach (@br) {
  $_->replace_with($literal)->delete;
}
print $html->as_HTML('<>', ' ',{});

The line with $literal is kind of a kludge, I don't know if it will break the tree (shouldn't because these types of elements should be empty...

HTH - ko