I have a Blogger-based weblog, with special DIV tags around the individual posts, so I can parse the resulting file. The tags are like <div CLASS="post" ID="5736047" UNUSEDATTRIBUTE="9/17/2001 01:31:37 PM"> and can contain any HTML (although usually it's fairly basic formatting).

I have an HTML::Parser based search function for my site that can parse these files, but as a side effect, I lose all HTML tags between the DIVs. At the moment, that's OK because I can then grep more reliably. However, for a different project, I'd like the same thing but with the original HTML between the DIVs - how does this work in HTML::Parser 3? I have to confess I don't really understand how the parser is structured...

Here's the current guts of the search, basically adapted from one of the examples:

my $p = HTML::Parser->new(api_version => 3, start_h => [\&div_start_ha +ndler, "self,tagname,attr"]); $p->parse_file($file); do_stuff(); sub div_start_handler { my($self, $tag, $attr) = @_; my($blogdate); return unless ($tag eq "div"); return unless exists $attr->{class}; return unless $attr->{class} eq 'post'; #global, so the endhandler knows what the last ID was $blogid = $attr->{id}; $BlogArticles{$blogid}{DateText} = $attr->{unusedattribute}; $self->handler(text => [], "dtext" ); $self->handler(end => \&div_end_handler, "self,tagname"); } sub div_end_handler { my($self, $tag) = @_; return unless $tag eq "div"; my $text = join("", map $_->[0], @{$self->handler("text")}); $BlogArticles{$blogid}{BodyText} = $text; $self->handler("text", undef); $self->handler("start", \&div_start_handler); $self->handler("end", undef); }

In reply to HTML::Parser - getting all contained HTML? by howie

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.