| blib/lib/HTML/ExtractContent/Util.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 51 | 60 | 85.0 |
| branch | 1 | 6 | 16.6 |
| condition | 1 | 2 | 50.0 |
| subroutine | 18 | 20 | 90.0 |
| pod | 0 | 13 | 0.0 |
| total | 71 | 101 | 70.3 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package HTML::ExtractContent::Util; | ||||||
| 2 | 3 | 3 | 15855 | use strict; | |||
| 3 | 4 | ||||||
| 3 | 99 | ||||||
| 3 | 3 | 3 | 14 | use warnings; | |||
| 3 | 4 | ||||||
| 3 | 69 | ||||||
| 4 | 3 | 3 | 606 | use utf8; | |||
| 3 | 10 | ||||||
| 3 | 19 | ||||||
| 5 | |||||||
| 6 | # core | ||||||
| 7 | 3 | 3 | 1751 | use Encode qw/encode_utf8 decode_utf8/; | |||
| 3 | 28796 | ||||||
| 3 | 288 | ||||||
| 8 | |||||||
| 9 | # cpan | ||||||
| 10 | 3 | 3 | 1444 | use Exporter::Lite; | |||
| 3 | 1963 | ||||||
| 3 | 19 | ||||||
| 11 | 3 | 3 | 1648 | use HTML::Entities qw(decode_entities); | |||
| 3 | 29817 | ||||||
| 3 | 313 | ||||||
| 12 | 3 | 3 | 1666 | use HTML::Strip (); | |||
| 3 | 3263 | ||||||
| 3 | 2272 | ||||||
| 13 | |||||||
| 14 | sub strip { | ||||||
| 15 | 113 | 113 | 0 | 286 | my $str = shift; | ||
| 16 | 113 | 828 | $str =~ s/(^\s+|\s+$)//gs; | ||||
| 17 | 113 | 354 | return $str; | ||||
| 18 | } | ||||||
| 19 | |||||||
| 20 | sub strip_tags { | ||||||
| 21 | 67 | 67 | 0 | 520 | my $page = shift; | ||
| 22 | |||||||
| 23 | 67 | 120 | my $octets = encode_utf8($page); | ||||
| 24 | 67 | 352 | my $hs = HTML::Strip->new; | ||||
| 25 | 67 | 2725 | my $stripped = $hs->parse($octets); | ||||
| 26 | |||||||
| 27 | 67 | 1221 | return decode_utf8($stripped); | ||||
| 28 | } | ||||||
| 29 | |||||||
| 30 | sub eliminate_tags { | ||||||
| 31 | 30 | 30 | 0 | 433 | my ($page, $tag) = @_; | ||
| 32 | 30 | 601 | $page =~ s/<$tag[\s>].*?<\/$tag\s*>//igs; | ||||
| 33 | 30 | 77 | return $page; | ||||
| 34 | } | ||||||
| 35 | |||||||
| 36 | sub eliminate_links { | ||||||
| 37 | 15 | 15 | 0 | 29 | return eliminate_tags shift, 'a'; | ||
| 38 | } | ||||||
| 39 | |||||||
| 40 | sub eliminate_forms { | ||||||
| 41 | 13 | 13 | 0 | 28 | return eliminate_tags shift, 'form'; | ||
| 42 | } | ||||||
| 43 | |||||||
| 44 | sub eliminate_br { | ||||||
| 45 | 59 | 59 | 0 | 52 | my $page = shift; | ||
| 46 | 59 | 83 | $page =~ s/ ]*>/ /igs; |
||||
| 47 | 59 | 94 | return $page; | ||||
| 48 | } | ||||||
| 49 | |||||||
| 50 | sub eliminate_invisible { | ||||||
| 51 | 0 | 0 | 0 | 0 | my $page = shift; | ||
| 52 | 0 | 0 | my $patterns = [ | ||||
| 53 | qr//is, | ||||||
| 54 | qr/<(script|style|select|noscript)[^>]*>.*?<\/\1\s*>/is, | ||||||
| 55 | qr/ ]*(id|class)\s*=\s*['"]?\S*(more|menu|side|navi)\S*["']?[^>]*>/is, |
||||||
| 56 | ]; | ||||||
| 57 | 0 | 0 | for my $pat (@$patterns) { | ||||
| 58 | 0 | 0 | $page =~ s/$pat//igs; | ||||
| 59 | } | ||||||
| 60 | 0 | 0 | return $page; | ||||
| 61 | } | ||||||
| 62 | |||||||
| 63 | sub extract_alt { | ||||||
| 64 | 0 | 0 | 0 | 0 | my $page = shift; | ||
| 65 | 0 | 0 | $page =~ s{ | ||||
| 66 | # no backgrack or otherwise the time complexity will become O(n^2) | ||||||
| 67 | |
||||||
| 68 | " ([^"]*) " | ' ([^']*) ' | ([^\s"'<>]+) | ||||||
| 69 | ) [^>]* > | ||||||
| 70 | }{ | ||||||
| 71 | 0 | 0 | 0 | defined $1 ? $1 : defined $2 ? $2 : $3 | |||
| 0 | |||||||
| 72 | }xigse; | ||||||
| 73 | 0 | 0 | return $page; | ||||
| 74 | } | ||||||
| 75 | |||||||
| 76 | sub unescape { | ||||||
| 77 | 59 | 59 | 0 | 786 | my $page = shift; | ||
| 78 | 59 | 158 | decode_entities($page); | ||||
| 79 | } | ||||||
| 80 | |||||||
| 81 | sub reduce_ws { | ||||||
| 82 | 59 | 59 | 0 | 48 | my $page = shift; | ||
| 83 | 59 | 311 | $page =~ s/[ \t]+/ /g; | ||||
| 84 | 59 | 125 | $page =~ s/\n\s*/\n/gs; | ||||
| 85 | 59 | 94 | return $page; | ||||
| 86 | } | ||||||
| 87 | |||||||
| 88 | sub decode { | ||||||
| 89 | 59 | 59 | 0 | 119 | return strip (reduce_ws (unescape (strip_tags (eliminate_br shift)))); | ||
| 90 | } | ||||||
| 91 | |||||||
| 92 | sub to_text { | ||||||
| 93 | 15 | 15 | 0 | 26 | my ($html, $opts) = @_; | ||
| 94 | 15 | 50 | 57 | $opts ||= {}; | |||
| 95 | 15 | 50 | 33 | $html = extract_alt $html if $opts->{with_alt}; | |||
| 96 | 15 | 20 | return decode $html; | ||||
| 97 | } | ||||||
| 98 | |||||||
| 99 | sub match_count { | ||||||
| 100 | 40 | 40 | 0 | 98 | my ($str, $exp) = @_; | ||
| 101 | 40 | 258 | my @list = ($str =~ $exp); | ||||
| 102 | 40 | 111 | return $#list + 1; | ||||
| 103 | } | ||||||
| 104 | |||||||
| 105 | our @EXPORT = qw/strip strip_tags eliminate_tags eliminate_links eliminate_forms eliminate_br eliminate_invisible extract_alt unescape reduce_ws decode to_text match_count/; | ||||||
| 106 | |||||||
| 107 | 1; |