blib/lib/HTML/ExtractText.pm

filter: Statementstmt 91.5% 108 / 118 Branchbran 68.5% 37 / 54 Conditioncond 48.5% 17 / 35 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 96.0% 24 / 25 Podpod 100% 6 / 6 totaltotal 76.9% 197 / 256 CC 45 SCAR 30.2
CC
45
Cov
78%
_text
72.3
extract
11.0
_process
8.0
CRAP 20.5
↑ Summary
1 package HTML::ExtractText;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = '1.002004'; # VERSION
7  
8 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Devel::TakeHashArgs;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 0 ▶use overload q|""| => sub { shift->error },
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 5 ▶ q|%{}| => sub { shift->last_results };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16 sub new {
17 13 ▶ my $self = bless [], shift;
Subroutine new: called CC=3 SCAR=11.1
Pod documented
18 13 ▶ get_args_as_hash( \@_, \ my %args,
Branch: 1 branch
BranchTrueFalseTotal
get_args_as_hash(\@_, \my %args, {"separator", "\n", "ignore_not_found", 1}, scalar [], ["separator", "ignore_not_found"]) or croak($@) 0 13 13
19 { # these are optional with defaults
20 separator => "\n",
21 ignore_not_found => 1,
22 },
23 [ ], # these are mandatory (none, ATM)
24 [ qw/separator ignore_not_found/ ], # only these args are valid ones
25 ) or croak $@;
26  
27 13 $self->$_( $args{$_} ) for keys %args;
28  
29 13 return $self;
30 }
31  
32 sub extract {
33 11 ▶ my ( $self, $what, $html, $obj ) = @_;
Subroutine extract: called CC=11 SCAR=24.0
Pod documented
34  
35 11 $self->error( undef );
36 11 $self->last_results( undef );
37  
38 11 ▶ ref $what eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $what eq "HASH" or return $self->_set_error("First argument to extract_text() must be a hashref") 0 11 11
39 or return $self->_set_error('First argument to '
40 . 'extract_text() must be a hashref');
41  
42 11 ▶ defined $html
Branch: 1 branch
BranchTrueFalseTotal
defined $html or return $self->_set_error("Second argument to extract_text() is an undef, expected HTML") 0 11 11
43 or return $self->_set_error('Second argument to extract_text() is '
44 . 'an undef, expected HTML');
45  
46 11 ▶ if ( defined $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) 3 8 11
47 3 ▶ blessed $obj
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($obj) or return $self->_set_error("Third argument must be an object") 1 2 3
48 or return $self->_set_error('Third argument must be an object');
49  
50 2 for ( keys %$what ) {
51 2 ▶ $obj->can($_)
Branch: 1 branch
BranchTrueFalseTotal
$obj->can($_) or return $self->_set_error('The object your provided does not implement the ->' . $_ . '() method that you requested in the first' . " argument") 1 1 2
52 or return $self->_set_error(
53 'The object your provided does not implement the ->'
54 . $_ . '() method that you requested in the first'
55 . ' argument',
56 );
57 }
58 }
59  
60  
61 9 my $dom = Mojo::DOM->new( $html );
62  
63 9 my $did_have_error = 0;
64 9 for my $selector ( sort keys %$what ) {
65 23 my $result;
66  
67 try {
68 23 ▶ my @results = $self->_extract( $dom, $selector, $what, );
Subroutine __ANON__: called CC=4 SCAR=13.9
69 23 ▶ die "NOT FOUND\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @results or $self->ignore_not_found 1 22 23
Condition66%2/3
@results or $self->ignore_not_found
l!l&&r!l&&!r
2201
MC/DC50%1/2
@results or $self->ignore_not_found
@results $self->ignore_not_found
Truth table66%2/3
A or B
A= @resultsB= $self->ignore_not_found
ABresult
1X1
011
000
70 if not @results and not $self->ignore_not_found;
71  
72 22 ▶ if ( defined (my $sep = $self->separator) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $sep = $self->separator)) { } 10 12 22
73 10 $result = join $sep, @results;
74 }
75 else {
76 12 $result = [ @results ];
77 }
78  
79 } catch {
80 1 ▶ chomp($_);
Subroutine __ANON__: called CC=1 SCAR=0.0
81 1 $self->error("ERROR: [$selector]: $_");
82 1 $result = "ERROR: $_";
83 1 $did_have_error = 1;
84 23 };
85  
86 23 $what->{ $selector } = $result;
87 }
88  
89 9 ▶ if ( defined $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) 1 8 9
90 1 for ( keys %$what ) {
91 1 $obj->$_( $what->{ $_ } );
92 }
93 }
94  
95 9 $self->last_results( $what );
96  
97 9 ▶ if ( $did_have_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($did_have_error) { } 1 8 9
98 1 return;
99 }
100 else {
101 8 return $what;
102 }
103 }
104  
105 sub separator {
106 39 ▶ my $self = shift;
Subroutine separator: called CC=2 SCAR=6.9
Pod documented
107 39 ▶ if ( @_ ) { $self->[0]->{SEPARATOR} = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 15 24 39
108 39 return $self->[0]->{SEPARATOR};
109 }
110  
111 sub ignore_not_found {
112 16 ▶ my $self = shift;
Subroutine ignore_not_found: called CC=2 SCAR=6.9
Pod documented
113 16 ▶ if ( @_ ) { $self->[0]->{IGNORE_NOT_FOUND} = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 2 16
114 16 return $self->[0]->{IGNORE_NOT_FOUND};
115 }
116  
117 sub last_results {
118 33 ▶ my $self = shift;
Subroutine last_results: called CC=2 SCAR=6.9
Pod documented
119 33 ▶ if ( @_ ) { $self->[0]->{LAST_RESULTS} = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 20 13 33
120 33 return $self->[0]->{LAST_RESULTS};
121 }
122  
123 sub error {
124 17 ▶ my $self = shift;
Subroutine error: called CC=2 SCAR=6.9
Pod documented
125 17 ▶ if ( @_ ) { $self->[0]->{ERROR} = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 3 17
126 17 return $self->[0]->{ERROR};
127 }
128  
129 sub _set_error {
130 2 ▶ my ( $self, $error ) = @_;
Subroutine _set_error: called CC=1 SCAR=0.0
131 2 $self->error( $error );
132 2 return;
133 }
134  
135 sub _process {
136 27 ▶ my $tag = $_->tag;
Subroutine _process: called CC=8 SCAR=20.8
137 27 ▶ return _all_text($_) unless $tag =~ /input|img/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /input|img/ 20 7 27
138  
139 7 ▶ return $_->attr('alt')//''
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "img" or $tag eq "input" and ($_->attr("type") // "") eq "image" 3 4 7
Condition90%9/10
$tag eq "img" or $tag eq "input" and ($_->attr("type") // "") eq "image"
l!l&&r!l&&!r
214
$tag eq "input" and ($_->attr("type") // "") eq "image"
!ll&&!rl&&r
041
$_->attr("type") // ""
l!l
41
$_->attr("alt") // ""
l!l
21
MC/DC66%2/3
$tag eq "img" or $tag eq "input" and ($_->attr("type") // "") eq "image"
$tag eq "img" $tag eq "input" $_->attr("type")
MC/DC100%1/1
$_->attr("alt") // ""
$_->attr("alt")
Truth table75%3/4
A or B and C
A= $tag eq "img"B= $tag eq "input"C= $_->attr("type")
ABCresult
1XX1
0111
00X0
0100
Truth table100%2/2
A
A= $_->attr("alt")
Aresult
11
00
140 if $tag eq 'img' or
141 ($tag eq 'input' and ($_->attr('type')//'') eq 'image');
142  
143 4 ▶ return $_->attr('value')//'';
Condition100%2/2
$_->attr("value") // ""
l!l
31
MC/DC100%1/1
$_->attr("value") // ""
$_->attr("value")
Truth table100%2/2
A
A= $_->attr("value")
Aresult
11
00
144 }
145  
146 sub _extract {
147 22 ▶ my ( $self, $dom, $selector, $what ) = @_;
Subroutine _extract: called CC=1 SCAR=0.0
148 return $dom->find( $what->{ $selector } )
149 22 ▶ ->map( sub { $self->_process( @_ ) } )->each;
Subroutine __ANON__: called CC=1 SCAR=0.0
150 }
151  
152 # The _all_text & _text functions copied from Mojo::DOM 6.66.
153 sub _all_text {
154 20 ▶ my ( $dom ) = @_;
Subroutine _all_text: called CC=4 SCAR=15.0
155 20 my $trim = 1;
156  
157 # Detect "pre" tag
158 20 my $tree = $dom->tree;
159 20 ▶ map { $_->[1] eq 'pre' and $trim = 0 } Mojo::DOM::_ancestors( $dom ), $tree
Branch: 2 branches
BranchTrueFalseTotal
if $trim and $tree->[0] ne "root" 20 0 20
$_->[1] eq "pre" and $trim = 0 0 20 20
Condition33%1/3
$trim and $tree->[0] ne "root"
!ll&&!rl&&r
0020
MC/DC0%0/2
$trim and $tree->[0] ne "root"
$trim $tree->[0] ne "root"
Truth table33%1/3
A and B
A= $trimB= $tree->[0] ne "root"
ABresult
0X0
100
111
160 if $trim && $tree->[0] ne 'root';
161  
162 20 return _text( [Mojo::DOM::_nodes($tree)], $trim );
163 }
164  
165 sub _text {
166 20 ▶ my ( $nodes, $trim ) = @_;
Subroutine _text: called CC=16 SCAR=42.8
167  
168 # Merge successive text nodes.
169 20 my $i = 0;
170 20 ▶ while ( my $next = $nodes->[$i + 1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $$nodes[$i + 1]) 0 20 20
171 0 ▶ ++$i and next unless $nodes->[$i][0] eq 'text' && $next->[0] eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $nodes->[$i][0] eq "text" and $next->[0] eq "text" 0 0 0
Condition0%0/8
$nodes->[$i][0] eq 'text' && $next->[0] eq 'text' || ++$i && (next)
l!l&&r!l&&!r
000
$nodes->[$i][0] eq "text" and $next->[0] eq "text"
!ll&&!rl&&r
000
++$i and next
!ll
00
MC/DC0%0/3
$nodes->[$i][0] eq 'text' && $next->[0] eq 'text' || ++$i && (next)
$nodes->[$i][0] eq "text" $next->[0] eq "text" ++$i
Truth table0%0/5
A and B || C
A= $nodes->[$i][0] eq "text"B= $next->[0] eq "text"C= ++$i
ABCresult
11X1
0X11
1011
0X00
1000
172 0 splice @$nodes, $i, 2, ['text', $nodes->[$i][1] . $next->[1]];
173 }
174  
175 20 my $text = '';
176 20 for my $node ( @$nodes ) {
177 20 my $type = $node->[0];
178 20 my $chunk = '';
179  
180 # Text.
181 20 ▶ if ( $type eq 'text' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'text') { } 0 20 20
elsif ($type eq 'cdata' or $type eq 'raw') { } 0 20 20
elsif ($type eq 'tag') { } 0 20 20
182 0 $chunk = $node->[1];
183 0 ▶ if ( $trim ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trim) 0 0 0
184 0 $chunk =~ s/^\s+//;
185 0 $chunk =~ s/\s+$//;
186 0 $chunk =~ s/\s+/ /g;
187 }
188 }
189 # CDATA or raw text.
190 ▶ elsif ( $type eq 'cdata' || $type eq 'raw' ) {
Condition33%1/3
$type eq "cdata" or $type eq "raw"
l!l&&r!l&&!r
0020
MC/DC0%0/2
$type eq "cdata" or $type eq "raw"
$type eq "cdata" $type eq "raw"
Truth table33%1/3
A or B
A= $type eq "cdata"B= $type eq "raw"
ABresult
1X1
011
000
191 0 $chunk = $node->[1];
192 }
193 # Nested tag.
194 elsif ( $type eq 'tag' ) {
195 5 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
196 0 ▶ $chunk = _text( [Mojo::DOM::_nodes($node)], 1, $node->[1] eq 'pre' ? 0 : $trim );
Branch: 1 branch
BranchTrueFalseTotal
$node->[1] eq 'pre' ? : 0 0 0
197 }
198  
199 # Add leading whitespace if punctuation allows it.
200 20 ▶ $chunk = " $chunk" if $text =~ /\S\z/ && $chunk =~ /^[^.!?,;:\s]+/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\S\z/ and $chunk =~ /^[^.!?,;:\s]+/ 0 20 20
Condition33%1/3
$text =~ /\S\z/ and $chunk =~ /^[^.!?,;:\s]+/
!ll&&!rl&&r
2000
MC/DC0%0/2
$text =~ /\S\z/ and $chunk =~ /^[^.!?,;:\s]+/
$text =~ /\S\z/ $chunk =~ /^[^.!?,;:\s]+/
Truth table33%1/3
A and B
A= $text =~ /\S\z/B= $chunk =~ /^[^.!?,;:\s]+/
ABresult
0X0
100
111
201  
202 # Trim whitespace blocks.
203 20 ▶ $text .= $chunk if $chunk =~ /\S+/ || !$trim;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk =~ /\S+/ or not $trim 0 20 20
Condition33%1/3
$chunk =~ /\S+/ or not $trim
l!l&&r!l&&!r
0020
MC/DC0%0/2
$chunk =~ /\S+/ or not $trim
$chunk =~ /\S+/ not $trim
Truth table33%1/3
A or not(B)
A= $chunk =~ /\S+/B= not $trim
ABresult
1X1
011
000
204 }
205  
206 20 return $text;
207 }
208  
209  
210 5 q|
211 Programming is 10% science, 20% ingenuity,
212 and 70% getting the ingenuity to work with the science.
213 |;
214  
215 __END__
↑ Summary