| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 125 | 0 | return 1; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 2855 | my $i = 0; | |||||||||||||||||||||||||||
| 129 | 2855 | my @stack; | |||||||||||||||||||||||||||
| 130 | 2855 | ▶ | while ($i < $len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 136 | 9904 | push @stack, $tag; | |||||||||||||||||||||||||||
| 137 | } else { | ||||||||||||||||||||||||||||
| 138 | 9005 | ▶ | while (scalar(@stack) && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar @stack and $stack[scalar(@stack) - 1] ne $tag
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
| |||||||||||||||||||||||||||||
| 139 | ($stack[scalar(@stack) - 1] ne $tag)) { | ||||||||||||||||||||||||||||
| 140 | 576 | ▶ | if ($self->is_unclosed_tag($stack[scalar(@stack) - 1])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 257 | pop @stack; | |||||||||||||||||||||||||||
| 142 | } else { | ||||||||||||||||||||||||||||
| 143 | 319 | return 0; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 8686 | ▶ | if (!scalar(@stack)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 158 | 805 | my $top = pop @stack; | |||||||||||||||||||||||||||
| 159 | 805 | ▶ | if (!$self->is_unclosed_tag($top)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||