| 1 | package HTML::ListScraper::Interactive; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use HTML::Entities; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 7 | require Exporter; | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use vars qw(@ISA @EXPORT_OK); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 7 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||
| 13 | 7 | @EXPORT_OK = qw(format_tags canonicalize_tags); | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 7 | ▶ | use Class::Generate qw(class); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 7 | class 'HTML::ListScraper::FormTag' => { | |||||||||||||||||||||||||||||||
| 18 | name => { type => '$', required => 1 }, | ||||||||||||||||||||||||||||||||
| 19 | index => { type => '$', required => 1, readonly => 1 }, | ||||||||||||||||||||||||||||||||
| 20 | link => { type => '$', required => 1, readonly => 1 }, | ||||||||||||||||||||||||||||||||
| 21 | text => { type => '$', required => 1, readonly => 1 }, | ||||||||||||||||||||||||||||||||
| 22 | '&close_name' => q{ $name .= '/'; } | ||||||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 907 | ▶ | sub is_opening { | ||||||||||||||||||||||||||||||
|
Subroutine
is_opening: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 26 | 907 | my $tag = shift; | |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 907 | return $tag !~ m~\/~; | |||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 907 | ▶ | sub is_closing { | ||||||||||||||||||||||||||||||
|
Subroutine
is_closing: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 32 | 907 | my $tag = shift; | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 907 | return $tag =~ m~^\/~; | |||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 53 | ▶ | sub format_tags { | ||||||||||||||||||||||||||||||
|
Subroutine
format_tags: called CC=27 SCAR=33.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 38 | 53 | my ($scraper, $tags, $incl) = @_; | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 53 | my $incl_attr; | |||||||||||||||||||||||||||||||
| 41 | my $incl_text; | ||||||||||||||||||||||||||||||||
| 42 | 53 | my $incl_index; | |||||||||||||||||||||||||||||||
| 43 | 53 | ▶ | if (ref($incl)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 44 | 50 | $incl_attr = $incl->{attr}; | |||||||||||||||||||||||||||||||
| 45 | 50 | $incl_text = $incl->{text}; | |||||||||||||||||||||||||||||||
| 46 | 50 | $incl_index = $incl->{index}; | |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 53 | my @buffer; | |||||||||||||||||||||||||||||||
| 50 | my @stack; | ||||||||||||||||||||||||||||||||
| 51 | 53 | foreach my $td (@$tags) { | |||||||||||||||||||||||||||||||
| 52 | 960 | my $name = $td->name; | |||||||||||||||||||||||||||||||
| 53 | 960 | my $tag = $name; | |||||||||||||||||||||||||||||||
| 54 | 960 | $tag =~ s~^\/~~; | |||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 960 | ▶ | my $text = $td->text || ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$td->text || ''
MC/DC100%1/1
$td->text || ''
$td->text
Truth table100%2/2
A
A= $td->text
| |||||||||||||||||||||||||||||||||
| 57 | 960 | $text =~ s/[\s[:cntrl:]]+/ /g; | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 960 | ▶ | my $link = $td->link || ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$td->link || ''
MC/DC100%1/1
$td->link || ''
$td->link
Truth table100%2/2
A
A= $td->link
| |||||||||||||||||||||||||||||||||
| 60 | 960 | $link =~ s/[\s[:cntrl:]]+//g; | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 960 | ▶ | if ($name eq $tag) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | 491 | push @stack, [ $tag, scalar(@buffer) ]; | |||||||||||||||||||||||||||||||
| 64 | 491 | push @buffer, HTML::ListScraper::FormTag->new(name => $name, | |||||||||||||||||||||||||||||||
| 65 | index => $td->index, link => $link, text => $text); | ||||||||||||||||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||||||||||||||
| 67 | 469 | ▶ | while (scalar(@stack) && | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar @stack and $stack[scalar(@stack) - 1][0] ne $tag
MC/DC50%1/2
scalar @stack and $stack[scalar(@stack) - 1][0] ne $tag
scalar @stack
$stack[scalar(@stack) - 1][0] ne $tag
Truth table66%2/3
A and B
A= scalar @stackB= $stack[scalar(@stack) - 1][0] ne $tag
| |||||||||||||||||||||||||||||||||
| 68 | ($stack[scalar(@stack) - 1]->[0] ne $tag)) { | ||||||||||||||||||||||||||||||||
| 69 | 20 | ▶ | if ($scraper->is_unclosed_tag( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | $stack[scalar(@stack) - 1]->[0])) { | ||||||||||||||||||||||||||||||||
| 71 | 20 | my $pair = pop @stack; | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 20 | $buffer[$pair->[1]]->close_name(); | |||||||||||||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||||||||||||||
| 75 | 0 | last; | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 469 | ▶ | if (scalar(@stack)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 80 | 469 | pop @stack; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 469 | push @buffer, HTML::ListScraper::FormTag->new(name => $name, | |||||||||||||||||||||||||||||||
| 84 | index => $td->index, link => $link, text => $text); | ||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 53 | ▶ | while (scalar(@stack)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | 2 | my $pair = pop @stack; | |||||||||||||||||||||||||||||||
| 90 | 2 | $buffer[$pair->[1]]->close_name(); | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 53 | my @out; | |||||||||||||||||||||||||||||||
| 94 | my $prev; | ||||||||||||||||||||||||||||||||
| 95 | 53 | my $prev_index; | |||||||||||||||||||||||||||||||
| 96 | 53 | my $depth = 0; | |||||||||||||||||||||||||||||||
| 97 | 53 | foreach my $ft (@buffer) { | |||||||||||||||||||||||||||||||
| 98 | 960 | my $name = $ft->name; | |||||||||||||||||||||||||||||||
| 99 | 960 | ▶ | if (defined($prev)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 100 | 907 | my $op = is_opening($prev); | |||||||||||||||||||||||||||||||
| 101 | 907 | my $cl = is_closing($name); | |||||||||||||||||||||||||||||||
| 102 | 907 | ▶ | if ($op && !$cl) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$op and not $cl
MC/DC100%2/2
$op and not $cl
$op
not $cl
Truth table100%3/3
A and not(B)
A= $opB= not $cl
| |||||||||||||||||||||||||||||||||
| 103 | 284 | ++$depth; | |||||||||||||||||||||||||||||||
| 104 | ▶ | } elsif (!$op && $cl) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
not $op and $cl
MC/DC100%2/2
not $op and $cl
not $op
$cl
Truth table100%3/3
not(A) and B
A= not $opB= $cl
| |||||||||||||||||||||||||||||||||
| 105 | 284 | ▶ | if ($depth > 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 106 | 284 | --$depth; | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 960 | my $indent = ' ' x (2 * $depth); | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 960 | my $attr = ''; | |||||||||||||||||||||||||||||||
| 114 | 960 | ▶ | if ($incl_attr && $ft->link) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$incl_attr and $ft->link
MC/DC100%2/2
$incl_attr and $ft->link
$incl_attr
$ft->link
Truth table100%3/3
A and B
A= $incl_attrB= $ft->link
| |||||||||||||||||||||||||||||||||
| 115 | 109 | $attr = ' href="' . encode_entities($ft->link, '"') . '"'; | |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 960 | my $lncol = ''; | |||||||||||||||||||||||||||||||
| 119 | 960 | ▶ | if ($incl_index) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | 0 | $lncol = $ft->index . "\t"; | |||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 960 | ▶ | if (defined($prev_index) && (($ft->index - $prev_index) != 1)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $prev_index and $ft->index - $prev_index != 1
MC/DC100%2/2
defined $prev_index and $ft->index - $prev_index != 1
defined $prev_index
$ft->index - $prev_index != 1
Truth table100%3/3
A and B
A= defined $prev_indexB= $ft->index - $prev_index != 1
| |||||||||||||||||||||||||||||||||
| 124 | 26 | push @out, "\n"; | |||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 960 | push @out, "$lncol$indent<$name$attr>\n"; | |||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 960 | ▶ | if ($incl_text && ($ft->text !~ /^[\s\r\n]*$/)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$incl_text and not $ft->text =~ /^[\s\r\n]*$/
MC/DC100%2/2
$incl_text and not $ft->text =~ /^[\s\r\n]*$/
$incl_text
not $ft->text =~ /^[\s\r\n]*$/
Truth table100%3/3
A and not(B)
A= $incl_textB= not $ft->text =~ /^[\s\r\n]*$/
| |||||||||||||||||||||||||||||||||
| 130 | 64 | ▶ | $lncol = $incl_index ? "\t" : ""; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | 64 | push @out, $lncol . $indent . encode_entities($ft->text, "<>&") . "\n"; | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 960 | $prev = $name; | |||||||||||||||||||||||||||||||
| 135 | 960 | $prev_index = $ft->index; | |||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 53 | ▶ | return wantarray ? @out : \@out; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 3 | ▶ | sub canonicalize_tags { | ||||||||||||||||||||||||||||||
|
Subroutine
canonicalize_tags: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 142 | 3 | my @out; | |||||||||||||||||||||||||||||||
| 143 | 3 | foreach (@_) { | |||||||||||||||||||||||||||||||
| 144 | 48 | my $ln = lc $_; | |||||||||||||||||||||||||||||||
| 145 | 48 | $ln =~ s/^\s*<//; | |||||||||||||||||||||||||||||||
| 146 | 48 | $ln =~ s/\/?>[\s\r\n]*$//; | |||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 48 | ▶ | if ($ln) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 149 | 48 | push @out, $ln; | |||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 3 | ▶ | return wantarray ? @out : \@out; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | 7 | 1; | |||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||||||