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


In reply to Re: Re: Re: X(ht)ML Source Formatting by koku
in thread X(ht)ML Source Formatting by ViceRaid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.