| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | or return $self->_set_error('First argument to ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | . 'extract_text() must be a hashref'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 11 | ▶ | defined $html | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | ▶ | blessed $obj | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
@results or $self->ignore_not_found
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | if not @results and not $self->ignore_not_found; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 22 | ▶ | if ( defined (my $sep = $self->separator) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 7 | ▶ | return $_->attr('alt')//'' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition90%9/10
$tag eq "img" or $tag eq "input" and ($_->attr("type") // "") eq "image"
$tag eq "input" and ($_->attr("type") // "") eq "image"
$_->attr("type") // ""
$_->attr("alt") // ""
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")
Truth table100%2/2
A
A= $_->attr("alt")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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") // ""
MC/DC100%1/1
$_->attr("value") // ""
$_->attr("value")
Truth table100%2/2
A
A= $_->attr("value")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||