blib/lib/HTML/Scrape.pm

filter: Statementstmt 91.9% 80 / 87 Branchbran 77.2% 17 / 22 Conditioncond 57.5% 19 / 33 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 77.8% 130 / 167 SCAR 33.2
CC
24
Cov
82%
_parser_handle_end
20.9
_parser_handle_start
7.2
_parser_handle_text
3.0
CRAP 27.5
↑ Summary
1 package HTML::Scrape;
2  
3 2 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 HTML::Scrape - The great new HTML::Scrape!
10  
11 =head1 VERSION
12  
13 Version 0.1.0
14  
15 =cut
16  
17 2 our $VERSION = '0.1.0';
18  
19 2 ▶use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use HTML::Tagset;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 =head1 SYNOPSIS
24  
25 Handy helpers for common HTML scraping tasks.
26  
27 use HTML::Scrape;
28  
29 my $ids = HTML::Scrape::scrape_all_ids( $html );
30  
31 =head1 FUNCTIONS
32  
33 =head2 scrape_all_ids( $html )
34  
35 Parses the entire web page and returns all the text in a hashref keyed on ID.
36  
37 =cut
38  
39 sub scrape_all_ids {
40 2 ▶ my $html = shift;
Subroutine scrape_all_ids: called CC=2 SCAR=7.0
Pod documented
41  
42 2 my $p = HTML::Parser->new(
43 start_h => [ \&_parser_handle_start, 'self, tagname, attr, line, column' ],
44 end_h => [ \&_parser_handle_end, 'self, tagname, line, column' ],
45 text_h => [ \&_parser_handle_text, 'self, dtext' ],
46 ignore_elements => [qw(script style)],
47 );
48 2 $p->{stack} = [];
49 2 $p->{ids} = {};
50  
51 2 $p->empty_element_tags(1);
52 2 $p->parse($html);
53 2 $p->eof;
54  
55 2 ▶ if ( my $n = scalar @{$p->{stack}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = scalar @{$p->{"stack"};}) 0 2 2
56 0 warn "$n tag(s) unclosed at end of document.\n";
57 }
58  
59 2 return $p->{ids};
60 }
61  
62  
63 sub _parser_handle_start {
64 22 ▶ my $parser = shift;
Subroutine _parser_handle_start: called CC=7 SCAR=19.7
65 22 my $tagname = shift;
66 22 my $attr = shift;
67 22 my $line = shift;
68 22 my $column = shift;
69  
70 22 ▶ return if $HTML::Tagset::emptyElement{$tagname};
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Tagset::emptyElement{$tagname} 3 19 22
71  
72 19 my $id = $attr->{id};
73  
74 # If it's a dupe ID, warn and ignore the ID.
75 19 ▶ if ( defined($id) && exists $parser->{ids}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id and exists $parser->{"ids"}{$id}) 0 19 19
Condition66%2/3
defined $id and exists $parser->{"ids"}{$id}
!ll&&!rl&&r
9100
MC/DC0%0/2
defined $id and exists $parser->{"ids"}{$id}
defined $id exists $parser->{"ids"}{$id}
Truth table66%2/3
A and B
A= defined $idB= exists $parser->{"ids"}{$id}
ABresult
0X0
100
111
76 0 warn "Duplicate ID $id found in <$tagname> at $line:$column\n";
77 0 $id = undef;
78 }
79  
80 19 my $stack = $parser->{stack};
81  
82 # Tags like <p> and <li> that don't have to close themselves get closed another of them comes along.
83 # For example:
84 # <ul>
85 # <li id="x">whatever
86 # <li id="y">thingy
87 # </ul>
88 19 ▶ if ( $HTML::Tagset::optionalEndTag{$tagname} && @{$stack} && $stack->[-1][0] eq $tagname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Tagset::optionalEndTag{$tagname} and @{$stack;} and $stack->[-1][0] eq $tagname) 3 16 19
Condition83%5/6
$HTML::Tagset::optionalEndTag{$tagname} and @{$stack;} and $stack->[-1][0] eq $tagname
!ll&&!rl&&r
1153
$HTML::Tagset::optionalEndTag{$tagname} and @{$stack;}
!ll&&!rl&&r
1108
MC/DC66%2/3
$HTML::Tagset::optionalEndTag{$tagname} and @{$stack;} and $stack->[-1][0] eq $tagname
$HTML::Tagset::optionalEndTag{$tagname} @{$stack;} $stack->[-1][0] eq $tagname
Truth table75%3/4
A and B and C
A= $HTML::Tagset::optionalEndTag{$tagname}B= @{$stack;}C= $stack->[-1][0] eq $tagname
ABCresult
0XX0
10X0
1100
1111
89 3 my $item = pop @{$stack};
90 3 _close_tag( $parser, $item );
91 }
92  
93 19 push @{$stack}, [ $tagname, $id, '' ];
94  
95 19 return;
96 }
97  
98  
99 sub _parser_handle_end {
100 16 ▶ my $parser = shift;
Subroutine _parser_handle_end: called CC=14 SCAR=30.4
101 16 my $tagname = shift;
102 16 my $line = shift;
103 16 my $column = shift;
104  
105 16 ▶ return if $HTML::Tagset::emptyElement{$tagname};
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Tagset::emptyElement{$tagname} 2 14 16
106  
107 14 my $stack = $parser->{stack};
108  
109 # Deal with tags that close others.
110 14 ▶ if ( @{$stack} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$stack;}) 14 0 14
111 14 my $previous_item = $stack->[-1];
112 14 my $previous_tagname = $previous_item->[0];
113  
114 #warn "tagname $tagname hprase markup = " , $HTML::Tagset::isPhraseMarkup{$tagname} // 'undef', ' previous = ' . $previous_tagname;
115 my $this_tag_closes_previous_one =
116 ( $tagname ne $previous_tagname )
117 &&
118 (
119 ( ($tagname eq 'ul' || $tagname eq 'ol') && $previous_tagname eq 'li' )
120 ||
121 ( ($tagname eq 'dl') && ($previous_tagname eq 'dt' || $previous_tagname eq 'dd') )
122 ||
123 14 ▶ ( !$HTML::Tagset::isPhraseMarkup{$tagname} && $previous_tagname eq 'p' )
Condition50%12/24
$tagname ne $previous_tagname && (($tagname eq 'ul' || $tagname eq 'ol') && $previous_tagname eq 'li' || $tagname eq 'dl' && ($previous_tagname eq 'dt' || $previous_tagname eq 'dd') || !$HTML::Tagset::isPhraseMarkup{$tagname} && $previous_tagname eq 'p')
!ll&&!rl&&r
1202
$tagname eq "ul" || $tagname eq "ol" and $previous_tagname eq "li" or $tagname eq "dl" and $previous_tagname eq "dt" || $previous_tagname eq "dd" or not $HTML::Tagset::isPhraseMarkup{$tagname} and $previous_tagname eq "p"
l!l&&r!l&&!r
110
$tagname eq "ul" || $tagname eq "ol" and $previous_tagname eq "li" or $tagname eq "dl" and $previous_tagname eq "dt" || $previous_tagname eq "dd"
l!l&&r!l&&!r
101
$tagname eq "ul" || $tagname eq "ol" and $previous_tagname eq "li"
!ll&&!rl&&r
101
$tagname eq "ul" or $tagname eq "ol"
l!l&&r!l&&!r
101
$tagname eq "dl" and $previous_tagname eq "dt" || $previous_tagname eq "dd"
!ll&&!rl&&r
100
$previous_tagname eq "dt" or $previous_tagname eq "dd"
l!l&&r!l&&!r
000
not $HTML::Tagset::isPhraseMarkup{$tagname} and $previous_tagname eq "p"
!ll&&!rl&&r
001
MC/DC11%1/9
$tagname ne $previous_tagname && (($tagname eq 'ul' || $tagname eq 'ol') && $previous_tagname eq 'li' || $tagname eq 'dl' && ($previous_tagname eq 'dt' || $previous_tagname eq 'dd') || !$HTML::Tagset::isPhraseMarkup{$tagname} && $previous_tagname eq 'p')
$tagname ne $previous_tagname $tagname eq "ul" $tagname eq "ol" $previous_tagname eq "li" $tagname eq "dl" $previous_tagname eq "dt" $previous_tagname eq "dd" not $HTML::Tagset::isPhraseMarkup{$tagname} $previous_tagname eq "p"
Truth table11%3/27
A && B or C and D or E and F or G or not(H) and I
A= $tagname ne $previous_tagnameB= $tagname eq "ul"C= $tagname eq "ol"D= $previous_tagname eq "li"E= $tagname eq "dl"F= $previous_tagname eq "dt"G= $previous_tagname eq "dd"H= not $HTML::Tagset::isPhraseMarkup{$tagname}I= $previous_tagname eq "p"
ABCDEFGHIresult
0XXXXXXXX0
100X0XX0X0
100X0XX100
100X1000X0
100X100100
11X00XX0X0
11X00XX100
11X01000X0
11X0100100
10100XX0X0
10100XX100
10101000X0
1010100100
11X1XXXXX1
1011XXXXX1
100X11XXX1
100X101XX1
11X011XXX1
11X0101XX1
101011XXX1
1010101XX1
100X0XX111
100X100111
11X00XX111
11X0100111
10100XX111
1010100111
124 )
125 ;
126 14 ▶ if ( $this_tag_closes_previous_one ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_tag_closes_previous_one) 2 12 14
127 2 _close_tag( $parser, pop @{$stack} );
128 }
129 }
130  
131 14 ▶ if ( !@{$stack} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$stack;}) 0 14 14
132 0 warn "Unexpected closing </$tagname> at $line:$column\n";
133 0 return;
134 }
135 14 ▶ if ( $tagname ne $stack->[-1][0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagname ne $stack->[-1][0]) 0 14 14
136 0 warn "Unexpected closing </$tagname> at $line:$column: Expecting </$stack->[-1][0]>\n";
137 0 return;
138 }
139  
140 14 _close_tag( $parser, pop @{$stack} );
141  
142 14 return;
143 }
144  
145  
146 sub _parser_handle_text {
147 38 ▶ my $parser = shift;
Subroutine _parser_handle_text: called CC=3 SCAR=11.0
148 38 my $text = shift;
149  
150 38 for my $item ( @{$parser->{stack}} ) {
151 87 ▶ if ( $item->[1] ) { # Only accumulate text for tags with IDs.
Branch: 1 branch
BranchTrueFalseTotal
if ($item->[1]) 17 70 87
152 17 $item->[2] .= $text;
153 }
154 }
155  
156 38 return;
157 }
158  
159  
160 sub _close_tag {
161 19 ▶ my $parser = shift;
Subroutine _close_tag: called CC=2 SCAR=6.9
162 19 my $item = shift;
163  
164 19 my (undef, $id, $text) = @{$item};
165 19 ▶ if ( defined $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 10 9 19
166 10 $text =~ s/^\s+//;
167 10 $text =~ s/\s+$//;
168 10 $text =~ s/\s+/ /g;
169 10 $parser->{ids}{$id} = $text;
170 }
171  
172 19 return;
173 }
174  
175  
176 =head1 AUTHOR
177  
178 Andy Lester, C<< <andy at petdance.com> >>
179  
180 =head1 BUGS
181  
182 Please report any bugs or feature requests at L<https://github.com/petdance/html-scrape/issues>..
183  
184 =head1 SUPPORT
185  
186 You can find documentation for this module with the perldoc command.
187  
188 perldoc HTML::Scrape
189  
190 You can also look for information at:
191  
192 =over 4
193  
194 =item * Search CPAN
195  
196 L<https://metacpan.org/release/HTML-Scrape>
197  
198 =back
199  
200 =head1 LICENSE AND COPYRIGHT
201  
202 This software is Copyright (c) 2023 by Andy Lester.
203  
204 This is free software, licensed under: The Artistic License 2.0 (GPL Compatible)
205  
206 =cut
207  
208 2 1; # End of HTML::Scrape
↑ Summary