The following code is a shortened version of an example from HTML::Parser
#!/usr/bin/perl -w
use strict;
my $code = shift || usage();
sub edit_print { local $_ = shift; tr/a-z/n-za-m/; print }
use HTML::Parser 3.05;
my $p = HTML::Parser->new(unbroken_text => 1,
default_h => [ sub { print @_; }, "text" ],
text_h => [ \&edit_print, "text" ],
);
my $file = shift;
$p->parse_file($file)
This code works pretty well, but it has a disadvantage that it also rewrites the text inside <script>
and also <head>
sections. I've adapted the example above to do what I want, but unfortunately there is a remaining bug where it rewrites things like the text inside the <title>
tag which I don't want to rewrite.
Does anyone know how to write something like the above, but without mangling the JavaScript, <title>
, or other sections? I'm happy to use another module apart from HTML::Parser if necessary.