Debian lenny version packages
[pkg-perl] / deb-src / libhtml-parser-perl / libhtml-parser-perl-3.56 / eg / hstrip
1 #!/usr/bin/perl -w
2
3 # This script cleans up an HTML document
4
5 use strict;
6 use HTML::Parser ();
7
8 # configure these values
9 my @ignore_attr =
10     qw(bgcolor background color face style link alink vlink text
11        onblur onchange onclick ondblclick onfocus onkeydown onkeyup onload
12        onmousedown onmousemove onmouseout onmouseover onmouseup
13        onreset onselect onunload
14       );
15 my @ignore_tags = qw(font big small b i);
16 my @ignore_elements = qw(script style);
17
18 # make it easier to look up attributes
19 my %ignore_attr = map { $_ => 1} @ignore_attr;
20
21 sub tag
22 {
23     my($pos, $text) = @_;
24     if (@$pos >= 4) {
25         # kill some attributes
26         my($k_offset, $k_len, $v_offset, $v_len) = @{$pos}[-4 .. -1];
27         my $next_attr = $v_offset ? $v_offset + $v_len : $k_offset + $k_len;
28         my $edited;
29         while (@$pos >= 4) {
30             ($k_offset, $k_len, $v_offset, $v_len) = splice @$pos, -4;
31             if ($ignore_attr{lc substr($text, $k_offset, $k_len)}) {
32                 substr($text, $k_offset, $next_attr - $k_offset) = "";
33                 $edited++;
34             }
35             $next_attr = $k_offset;
36         }
37         # if we killed all attributed, kill any extra whitespace too
38         $text =~ s/^(<\w+)\s+>$/$1>/ if $edited;
39     }
40     print $text;
41 }
42
43 sub decl
44 {
45     my $type = shift;
46     print shift if $type eq "doctype";
47 }
48
49 sub text
50 {
51     print shift;
52 }
53
54 HTML::Parser->new(api_version   => 3,
55                   start_h       => [\&tag,   "tokenpos, text"],
56                   process_h     => ["", ""],
57                   comment_h     => ["", ""],
58                   declaration_h => [\&decl,   "tagname, text"],
59                   default_h     => [\&text,   "text"],
60
61                   ignore_tags   => \@ignore_tags,
62                   ignore_elements => \@ignore_elements,
63                  )
64     ->parse_file(shift) || die "Can't open file: $!\n";
65