我在 Windows 上,所以我无法完全测试它,但您可以调整HTML::Parser附带的htext:
#!/usr/bin/perl
use strict; use warnings;
use HTML::Parser;
use Term::ANSIColor;
use HTML::Parser 3.00 ();
my %inside;
sub tag {
my($tag, $num) = @_;
$inside{$tag} += $num;
print " "; # not for all tags
}
sub text {
return if $inside{script} || $inside{style};
my $esc = 1;
if ( $inside{b} or $inside{strong} ) {
print color 'blue';
}
elsif ( $inside{i} or $inside{em} ) {
print color 'yellow';
}
else {
$esc = 0;
}
print $_[0];
print color 'reset' if $esc;
}
HTML::Parser->new(api_version => 3,
handlers => [
start => [\&tag, "tagname, '+1'"],
end => [\&tag, "tagname, '-1'"],
text => [\&text, "dtext"],
],
marked_sections => 1,
)->parse_file(shift) || die "Can't open file: $!\n";;