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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |