Subject: | Provide a way to exclude tags from a twig-wide trim() |
The twig-wide trim() function is a great way to clean up ugly XML! However, some tags (in my case, <pre></pre>) need to be excluded from trimming.
I'm not sure if $elt->set_as() is appropriate to indicate exclusion, or if it's orthogonal and a separate set_trim() is needed instead; this is left to the developer's discretion.
(Side note: trim() leaves single spaces at the beginnings and ends of non-head, non-tail tags that are trimmed. It would be nice not to have these.)
Subject: | trim.pl |
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
my $xml = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<html>
<body>
<p>
This is
some text.
</p>
<p>
This is
some text.
</p>
<pre>
This is
preformatted text.
</pre>
<p>
This is
some text.
</p>
<p>
This is
some text.
</p>
</body>
</html>
EOF
my $twig=XML::Twig->new;
$twig->setTwigHandlers({
'pre' => sub { $_->set_asis(); },
});
$twig->parse($xml);
$twig->trim;
$twig->print(pretty_print => 'indented');