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 &nbsp; abc <% abc ( '<input value="abc" /> abc &nbsp; 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> &nbsp; <b>abc</b> <% abc ( '<input value="abc" /> abc &nbsp; abc <span> %>', $abc ); %>

Actual output:

<input value="abc" /> <b>abc</b> &nbsp; <b>abc</b> <% <b>abc</b> ( '<input value="abc" /> <b>abc</b> &nbsp; <b>abc</b> <s +pan> %>', $<b>abc</b> ); %>

In reply to Parsing ASP files by thewebsi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.