use HTML::Parser ();
my $parser = HTML::Parser->new(
api_version => 3,
start_h => [\&start_tag, "tagname, attr, text"],
text_h => [\&text_content, "text"],
end_h => [\&end_tag, "tagname, text"],
);
my ( $font, %colored );
sub start_tag {
my ($tagname, $attr, $text) = @_;
if ($tagname eq 'font') {
$font++;
if (my $color = $attr->{'color'}) {
print "[color=$color]";
$colored{$font}++;
return;
}
}
print $text;
}
sub text_content {
my $text = shift;
print $text;
}
sub end_tag {
my ($tagname, $text) = @_;
if ($tagname ne 'font') {
print $text;
return;
}
if ($colored{$font}) {
print "[/color]";
}
else {
print $text;
}
$font--;
}
my $html1 = q|
Some text,
an a|;
$parser->parse($html1);
$parser->eof;
print "\n\n";
my $html2 = q| breaker
Some text,
an a|;
$parser->parse($html2);
$parser->eof;