File: htext

package info (click to toggle)
libhtml-parser-perl 3.83-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 944 kB
  • sloc: ansic: 7,624; perl: 1,669; makefile: 13
file content (32 lines) | stat: -rwxr-xr-x 612 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/perl

# Extract all plain text from an HTML file

use strict;
use warnings;
use Encode ();
use HTML::Parser ();

my %inside;

sub tag {
    my ($tag, $num) = @_;
    $inside{$tag} += $num;
    print " ";    # not for all tags
}

sub text {
    return if $inside{script} || $inside{style};
    print encode('utf8', $_[0]);
}

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";