Many of the tools used to parse HTML use
HTML::Parser under the hood, and it is worthwhile knowing how it works. This script gathers up all the content of each list item, including other elements, into a variable. When it meets the closing list item tag, you can do what you need to the content before printing it out.
use HTML::Parser;
my $inside_li = 0;
my $list_item = '';
sub start {
my ($tag, $text) = @_;
if ($inside_li) {
$list_item .= $text;
return;
}
if ($tag eq 'li') {
$inside_li = 1;
}
print $text;
};
sub text {
my ($text) = @_;
if ($inside_li) {
$list_item .= $text;
return;
}
print $text;
};
sub end {
my ($tag, $text) = @_;
if ($tag eq 'li') {
$inside_li = 0;
# do things to <li> content
$list_item =~ s/^\s+//;
print $list_item;
$list_item = '';
}
if ($inside_li) {
$list_item .= $text;
return;
}
print $text;
};
my $parser = HTML::Parser->new(
api_version => 3,
start_h => [\&start, "tagname, text"],
text_h => [\&text, "text"],
end_h => [\&end, "tagname, text"],
default_h => [\&text, "text"],
);
$parser->parse_file(\*DATA);
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.