in reply to Re: Re: processing text nodes using HTML::Element
in thread processing text nodes using HTML::Element
Technically, I'm not sure that the '~literal' pseudo-element actually has any effect on how things are printed out (though that's how it's explained in the docs). It seems more sensical to me that it controls whether the text inserted is escaped or not. *shrug* But that's just implementation details. :)
Honestly, I don't really think there's a way to get HTML::TreeBuilder to print out the original HTML exactly the same. You can get pretty close by calling $tree->no_space_compacting(1) before parsing the HTML, but it doesn't seem to be 100% the same.
If all you want to do is make some minor changes (eg, changing '--' to '—') to the text parts, keeping the rest of the HTML exactly the same, you're probably best off switching to HTML::Parser (which HTML::TreeBuilder is based on). Code follows...
#!/usr/bin/perl use warnings; use strict; our $html = <<'EOHTML'; <html> <body> <p>The relationship can be expressed by the following equation:<br> <blockquote> y <= (7x<sup>3</sup> + 3x<sup>2</sup>)/((x - 3)(x - 5) </blockquote> <p>Of course x != 3 & x != 5 -- That goes without saying. <p>:) </body> </html> EOHTML use HTML::Parser; my $parser = HTML::Parser->new( handlers => { default => [sub { print @_ }, 'text'], # print out HTML tags unmod +ified text => [\&process_text, 'text'], }, ); $parser->parse($html); sub process_text { $_ = shift; # Make any changes to the text here... s/--/—/g; s/:\)/<img src="smiley.gif">/g; # and so forth. # Now print out the modified text. print; }
Output:
<html> <body> <p>The relationship can be expressed by the following equation:<br> <blockquote> y <= (7x<sup>3</sup> + 3x<sup>2</sup>)/((x - 3)(x - 5) </blockquote> <p>Of course x != 3 & x != 5 — That goes without saying. <p><img src="smiley.gif"> </body> </html>
bbfu
Black flowers blossum
Fearless on my breath
|
|---|