thewebsi has asked for the wisdom of the Perl Monks concerning the following question:
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> ); %>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parsing ASP files
by Anonymous Monk on Jan 24, 2012 at 21:52 UTC | |
by thewebsi (Scribe) on Jan 26, 2012 at 04:57 UTC | |
by thewebsi (Scribe) on Apr 05, 2012 at 03:47 UTC |