blib/lib/HTML/ExtractText.pm

filter: Statementstmt 98.8% 88 / 89 Branchbran 90.6% 29 / 32 Conditioncond 86.6% 13 / 15 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 95.4% 21 / 22 Podpod 100% 6 / 6 totaltotal 94.7% 162 / 171 SCAR 33.0
CC
27
Cov
96%
extract
11.0
_process
8.0
__ANON__
4.0
CRAP 27.1
↑ 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.002003'; # 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  
153  
154 5 q|
155 Programming is 10% science, 20% ingenuity,
156 and 70% getting the ingenuity to work with the science.
157 |;
158  
159 __END__
↑ Summary