| 1 | package HTML::ExtractContent::Util; | ||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | # core | ||||||||||||||
| 7 | 3 | ▶ | use Encode qw/encode_utf8 decode_utf8/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | # cpan | ||||||||||||||
| 10 | 3 | ▶ | use Exporter::Lite; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 3 | ▶ | use HTML::Entities qw(decode_entities); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 3 | ▶ | use HTML::Strip (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | sub strip { | ||||||||||||||
| 15 | 113 | ▶ | my $str = shift; | ||||||||||||
|
Subroutine
strip: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 16 | 113 | $str =~ s/(^\s+|\s+$)//gs; | |||||||||||||
| 17 | 113 | return $str; | |||||||||||||
| 18 | } | ||||||||||||||
| 19 | |||||||||||||||
| 20 | sub strip_tags { | ||||||||||||||
| 21 | 67 | ▶ | my $page = shift; | ||||||||||||
|
Subroutine
strip_tags: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 22 | |||||||||||||||
| 23 | 67 | my $octets = encode_utf8($page); | |||||||||||||
| 24 | 67 | my $hs = HTML::Strip->new; | |||||||||||||
| 25 | 67 | my $stripped = $hs->parse($octets); | |||||||||||||
| 26 | |||||||||||||||
| 27 | 67 | return decode_utf8($stripped); | |||||||||||||
| 28 | } | ||||||||||||||
| 29 | |||||||||||||||
| 30 | sub eliminate_tags { | ||||||||||||||
| 31 | 30 | ▶ | my ($page, $tag) = @_; | ||||||||||||
|
Subroutine
eliminate_tags: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 32 | 30 | $page =~ s/<$tag[\s>].*?<\/$tag\s*>//igs; | |||||||||||||
| 33 | 30 | return $page; | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | sub eliminate_links { | ||||||||||||||
| 37 | 15 | ▶ | return eliminate_tags shift, 'a'; | ||||||||||||
|
Subroutine
eliminate_links: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | sub eliminate_forms { | ||||||||||||||
| 41 | 13 | ▶ | return eliminate_tags shift, 'form'; | ||||||||||||
|
Subroutine
eliminate_forms: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | sub eliminate_br { | ||||||||||||||
| 45 | 59 | ▶ | my $page = shift; | ||||||||||||
|
Subroutine
eliminate_br: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 46 | 59 | $page =~ s/<br[^>]*>/ /igs; | |||||||||||||
| 47 | 59 | return $page; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | sub eliminate_invisible { | ||||||||||||||
| 51 | 0 | ▶ | my $page = shift; | ||||||||||||
|
Subroutine
eliminate_invisible: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 52 | 0 | my $patterns = [ | |||||||||||||
| 53 | qr/<!--.*?-->/is, | ||||||||||||||
| 54 | qr/<(script|style|select|noscript)[^>]*>.*?<\/\1\s*>/is, | ||||||||||||||
| 55 | qr/<div\s[^>]*(id|class)\s*=\s*['"]?\S*(more|menu|side|navi)\S*["']?[^>]*>/is, | ||||||||||||||
| 56 | ]; | ||||||||||||||
| 57 | 0 | for my $pat (@$patterns) { | |||||||||||||
| 58 | 0 | $page =~ s/$pat//igs; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | 0 | return $page; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | sub extract_alt { | ||||||||||||||
| 64 | 0 | ▶ | my $page = shift; | ||||||||||||
|
Subroutine
extract_alt: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 65 | 0 | $page =~ s{ | |||||||||||||
| 66 | # no backgrack or otherwise the time complexity will become O(n^2) | ||||||||||||||
| 67 | <img \s [^>]* \b alt \s* = \s* (?> | ||||||||||||||
| 68 | " ([^"]*) " | ' ([^']*) ' | ([^\s"'<>]+) | ||||||||||||||
| 69 | ) [^>]* > | ||||||||||||||
| 70 | }{ | ||||||||||||||
| 71 | 0 | ▶ | defined $1 ? $1 : defined $2 ? $2 : $3 | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 72 | }xigse; | ||||||||||||||
| 73 | 0 | return $page; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | sub unescape { | ||||||||||||||
| 77 | 59 | ▶ | my $page = shift; | ||||||||||||
|
Subroutine
unescape: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 78 | 59 | decode_entities($page); | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | sub reduce_ws { | ||||||||||||||
| 82 | 59 | ▶ | my $page = shift; | ||||||||||||
|
Subroutine
reduce_ws: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 83 | 59 | $page =~ s/[ \t]+/ /g; | |||||||||||||
| 84 | 59 | $page =~ s/\n\s*/\n/gs; | |||||||||||||
| 85 | 59 | return $page; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | sub decode { | ||||||||||||||
| 89 | 59 | ▶ | return strip (reduce_ws (unescape (strip_tags (eliminate_br shift)))); | ||||||||||||
|
Subroutine
decode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | sub to_text { | ||||||||||||||
| 93 | 15 | ▶ | my ($html, $opts) = @_; | ||||||||||||
|
Subroutine
to_text: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||
| 94 | 15 | ▶ | $opts ||= {}; | ||||||||||||
|
Condition50%1/2
$opts ||= {}
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||
| 95 | 15 | ▶ | $html = extract_alt $html if $opts->{with_alt}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 96 | 15 | return decode $html; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | sub match_count { | ||||||||||||||
| 100 | 40 | ▶ | my ($str, $exp) = @_; | ||||||||||||
|
Subroutine
match_count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 101 | 40 | my @list = ($str =~ $exp); | |||||||||||||
| 102 | 40 | return $#list + 1; | |||||||||||||
| 103 | } | ||||||||||||||
| 104 | |||||||||||||||
| 105 | 3 | 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 | 3 | 1; | |||||||||||||