blib/lib/HTML/ListScraper/Book.pm

filter: Statementstmt 91.2% 83 / 91 Branchbran 76.6% 23 / 30 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 13 totaltotal 80.8% 127 / 157 CC 18 SCAR 15.2
CC
18
Cov
88%
is_presentable
10.0
get_internal_name
6.0
get_all_tags
6.0
CRAP 4.6
« blib/lib/HTML/ListScraper.pm ↑ Summary blib/lib/HTML/ListScraper/Foam.pm »
1 package HTML::ListScraper::Book;
2  
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Class::Generate qw(class);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 class 'HTML::ListScraper::Tag' => {
9 name => { type => '$', required => 1, readonly => 1 },
10 index => { type => '$', required => 1, readonly => 1 },
11 link => { type => '$', readonly => 1 },
12 text => '$',
13 '&append_text' => q{ $text .= $_[0]; }
14 };
15  
16 12 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
17 12 my $class = shift;
18 12 my $self = { shapeless => 0, index => 0,
19 dseq => [ ], next => 0, tseq => [ ], p2t => { } };
20  
21 # the list is from HTML 4.01 Transitional DTD; head and body is
22 # included not because we seriously expect them to be unpaired,
23 # but just to simplify documentation - they aren't going to get
24 # into repeated sequences anyway...
25 12 foreach (qw(area base basefont body br col colgroup dd dt frame head hr img input isindex li link meta option p param tbody td tfoot th thead tr)) {
26 324 $self->{unclosed_tags}->{$_} = 1;
27 }
28  
29 12 bless $self, $class;
30  
31 12 return $self;
32 }
33  
34 80 ▶sub shapeless {
Subroutine shapeless: called CC=2 SCAR=7.6
Pod undocumented
35 80 my $self = shift;
36  
37 80 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 80 80
38 0 $self->{shapeless} = !!$_[0];
39 }
40  
41 80 return $self->{shapeless};
42 }
43  
44 1502 ▶sub is_unclosed_tag {
Subroutine is_unclosed_tag: called CC=1 SCAR=0.0
Pod undocumented
45 1502 my ($self, $name) = @_;
46  
47 1502 return exists($self->{unclosed_tags}->{$name});
48 }
49  
50 4671 ▶sub push_item {
Subroutine push_item: called CC=1 SCAR=0.0
Pod undocumented
51 4671 my ($self, $name) = @_;
52  
53 4671 my $index = ($self->{index})++;
54 4671 $self->_push(HTML::ListScraper::Tag->new(name => $name, index => $index));
55 }
56  
57 630 ▶sub push_link {
Subroutine push_link: called CC=1 SCAR=0.0
Pod undocumented
58 630 my ($self, $name, $link) = @_;
59  
60 630 my $index = ($self->{index})++;
61 630 $self->_push(HTML::ListScraper::Tag->new(
62 name => $name, index => $index, link => $link));
63 }
64  
65 0 ▶sub get_internal_name {
Subroutine get_internal_name: not called CC=2 SCAR=17.9
Pod undocumented
66 0 my ($self, $name) = @_;
67  
68 0 ▶ return exists($self->{p2t}->{$name}) ? $self->{p2t}->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'p2t'}{$name} ? : 0 0 0
69 }
70  
71 5406 ▶sub intern_name {
Subroutine intern_name: called CC=3 SCAR=11.1
Pod undocumented
72 5406 my ($self, $name) = @_;
73  
74 5406 ▶ if (!exists($self->{p2t}->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"p2t"}{$name}) 382 5024 5406
75 11 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 382 my $c = ($self->{next})++;
78 382 ▶ if ($self->{next} > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"next"} > 255) 0 382 382
79 # 18Apr2007: HTML::ListScraper::get_known_sequence
80 # depends on 1-byte internal names
81 0 die "can't handle so many tags";
82 # could probably switch to 2-byte numbers, but is that
83 # useful?
84 }
85  
86 382 $self->{p2t}->{$name} = bytes::chr($c);
87 }
88  
89 5406 return $self->{p2t}->{$name};
90 }
91  
92 5301 ▶sub _push {
Subroutine _push: called CC=1 SCAR=0.0
93 5301 my ($self, $td) = @_;
94  
95 5301 my $name = $td->name;
96 5301 my $iname = $self->intern_name($name);
97 5301 push @{$self->{dseq}}, $td;
98 5301 push @{$self->{tseq}}, $iname;
99 }
100  
101 3604 ▶sub append_text {
Subroutine append_text: called CC=2 SCAR=6.9
Pod undocumented
102 3604 my ($self, $text) = @_;
103  
104 3604 my $count = scalar(@{$self->{dseq}});
105  
106 # ignore text before the first tag
107 3604 ▶ if (!$count) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($count) 3 3601 3604
108 3 return; # if we had a verbose mode, we would warn here
109 }
110  
111 3601 my $td = $self->{dseq}->[$count - 1];
112 3601 $td->append_text($text);
113 }
114  
115 117 ▶sub get_internal_sequence {
Subroutine get_internal_sequence: called CC=2 SCAR=6.9
Pod undocumented
116 117 my $self = shift;
117  
118 117 ▶ return wantarray ? @{$self->{tseq}} : $self->{tseq};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 112 117
119 }
120  
121 2855 ▶sub is_presentable {
Subroutine is_presentable: called CC=10 SCAR=23.0
Pod undocumented
122 2855 my ($self, $start, $len) = @_;
123  
124 2855 ▶ if ($self->{shapeless}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"shapeless"}) 0 2855 2855
125 0 return 1;
126 }
127  
128 2855 my $i = 0;
129 2855 my @stack;
130 2855 ▶ while ($i < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 18909 644 19553
131 18909 my $name = $self->{dseq}->[$start + $i]->name;
132 18909 my $tag = $name;
133 18909 $tag =~ s~^\/~~;
134  
135 18909 ▶ if ($name eq $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $tag) { } 9904 9005 18909
136 9904 push @stack, $tag;
137 } else {
138 9005 ▶ while (scalar(@stack) &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @stack and $stack[scalar(@stack) - 1] ne $tag) 576 8686 9262
Condition100%3/3
scalar @stack and $stack[scalar(@stack) - 1] ne $tag
!ll&&!rl&&r
18926794576
MC/DC100%2/2
scalar @stack and $stack[scalar(@stack) - 1] ne $tag
scalar @stack $stack[scalar(@stack) - 1] ne $tag
Truth table100%3/3
A and B
A= scalar @stackB= $stack[scalar(@stack) - 1] ne $tag
ABresult
0X0
100
111
139 ($stack[scalar(@stack) - 1] ne $tag)) {
140 576 ▶ if ($self->is_unclosed_tag($stack[scalar(@stack) - 1])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_unclosed_tag($stack[scalar(@stack) - 1])) { } 257 319 576
141 257 pop @stack;
142 } else {
143 319 return 0;
144 }
145 }
146  
147 8686 ▶ if (!scalar(@stack)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @stack) 1892 6794 8686
148 1892 return 0;
149 }
150  
151 6794 pop @stack;
152 }
153  
154 16698 ++$i;
155 }
156  
157 644 ▶ while (scalar(@stack)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @stack) 805 106 911
158 805 my $top = pop @stack;
159 805 ▶ if (!$self->is_unclosed_tag($top)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_unclosed_tag($top)) 538 267 805
160 538 return 0;
161 }
162 }
163  
164 106 return 1;
165 }
166  
167 0 ▶sub get_all_tags {
Subroutine get_all_tags: not called CC=2 SCAR=17.9
Pod undocumented
168 0 my $self = shift;
169  
170 0 ▶ return wantarray ? @{$self->{dseq}} : $self->{dseq};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
171 }
172  
173 144 ▶sub get_tags {
Subroutine get_tags: called CC=1 SCAR=0.0
Pod undocumented
174 144 my ($self, $start, $len) = @_;
175  
176 144 my $last = $start + $len - 1;
177 144 return @{$self->{dseq}}[$start .. $last];
178 }
179  
180 1844 ▶sub get_tag {
Subroutine get_tag: called CC=1 SCAR=0.0
Pod undocumented
181 1844 my ($self, $pos) = @_;
182  
183 1844 return $self->{dseq}->[$pos];
184 }
185  
186 11 1;
« blib/lib/HTML/ListScraper.pm ↑ Summary blib/lib/HTML/ListScraper/Foam.pm »