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