I am looking to parse web site files to enable some simple manipulations (wrapping certain bits of plain text in custom tags). HTML::Parser is sufficient for this (HTML::TreeBuilder is probably overkill). However, some of the web site files contain an embedded tag language (eg, ASP, JSP, PHP). HTML::Parser does not recognize these tags (<% ... %>, etc), and produces some unfavourable results in these cases (I'd like it to treat them like regular tags and just ignore them). I don't see a way to extend HTML::Parser to handle such files.
Is there: A way to extend HTML::Parser? Another module I could use? Should I build my own parser with regex? I found these postings, and they both suggest I build my own parser for this: 1, 2, but thought I'd double-check in case there is a better way.
In case more detail is needed, I include my naive test script below:
#!/usr/bin/perl use HTML::PullParser; my $doc = <<'EOF'; my %options = (); <input value="abc" /> abc abc <% abc ( '<input value="abc" /> abc abc <span> %>', $abc ); %> EOF foreach ( qw{ start end comment declaration default process text } ) { $options{$_} = "\"$_\", text"; } my $p = HTML::PullParser->new ( doc => $doc, %options ); my $output = ""; print "DEBUG --------------------------------------------------------- +-----\n"; while ( my $token = $p->get_token() ) { print "$token->[0]: '$token->[1]'\n"; my $text = $token->[1]; $text =~ s|abc|<b>abc</b>|g if $token->[0] eq 'text'; $output .= $text; } print "--------------------------------------------------------------- +-----\n"; print $output;
Desired output:
<input value="abc" /> <b>abc</b> <b>abc</b> <% abc ( '<input value="abc" /> abc abc <span> %>', $abc ); %>
Actual output:
<input value="abc" /> <b>abc</b> <b>abc</b> <% <b>abc</b> ( '<input value="abc" /> <b>abc</b> <b>abc</b> <s +pan> %>', $<b>abc</b> ); %>
In reply to Parsing ASP files by thewebsi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |