| 1 | package HTML::TableContentParser; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use parent qw{ HTML::Parser }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '0.305_01'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | our $CLASSIC = 0; | |||||||||||||||||||||||||||
| 11 | 2 | our $DEBUG = 0; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | my @stacked = qw{ current_table current_row current_element }; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub new | ||||||||||||||||||||||||||||
| 16 | { | ||||||||||||||||||||||||||||
| 17 | 3 | ▶ | my ( $class, %arg ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 3 | my $classic = delete $arg{classic}; | |||||||||||||||||||||||||||
| 19 | 3 | my $self = $class->SUPER::new( %arg ); | |||||||||||||||||||||||||||
| 20 | 3 | ▶ | $self->{ATTR}{classic} = defined $classic ? $classic : $CLASSIC; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 3 | return $self; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub classic | ||||||||||||||||||||||||||||
| 25 | { | ||||||||||||||||||||||||||||
| 26 | 1 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
classic: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 1 | return $self->{ATTR}{classic}; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub start | ||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||
| 32 | # my ($self, $tag, $attr, $attrseq, $origtext) = @_; | ||||||||||||||||||||||||||||
| 33 | 85 | ▶ | my ($self, $tag, $attr, undef, $origtext) = @_; | ||||||||||||||||||||||||||
|
Subroutine
start: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 85 | $tag = lc($tag); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Store the incoming details in the current 'object'. | ||||||||||||||||||||||||||||
| 38 | 85 | ▶ | if ($tag eq 'table') { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 39 | 6 | my $table = $attr; | |||||||||||||||||||||||||||
| 40 | 6 | push @{ $self->{STORE}{stack} }, { | |||||||||||||||||||||||||||
| 41 | 6 | map { $_ => $self->{STORE}{$_} } @stacked }; | |||||||||||||||||||||||||||
| 42 | 6 | push @{$self->{STORE}->{tables}}, $table; | |||||||||||||||||||||||||||
| 43 | 6 | $self->{STORE}->{current_table} = $table; | |||||||||||||||||||||||||||
| 44 | 6 | $self->{STORE}->{current_row} = undef; | |||||||||||||||||||||||||||
| 45 | 6 | $self->{STORE}->{current_element} = undef; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | } elsif ($tag eq 'th') { | ||||||||||||||||||||||||||||
| 48 | 8 | my $th = $attr; | |||||||||||||||||||||||||||
| 49 | 8 | push @{$self->{STORE}->{current_table}->{headers}}, $th; | |||||||||||||||||||||||||||
| 50 | 8 | ▶ | unless ( $self->{ATTR}{classic} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 7 | push @{$self->{STORE}->{current_row}->{cells}}, undef; | |||||||||||||||||||||||||||
| 52 | 7 | push @{$self->{STORE}->{current_row}->{headers}}, $th; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 8 | $self->{STORE}->{current_element} = $th; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | } elsif ($tag eq 'tr') { | ||||||||||||||||||||||||||||
| 57 | 20 | my $tr = $attr; | |||||||||||||||||||||||||||
| 58 | 20 | push @{$self->{STORE}->{current_table}->{rows}}, $tr; | |||||||||||||||||||||||||||
| 59 | 20 | $self->{STORE}->{current_row} = $tr; | |||||||||||||||||||||||||||
| 60 | 20 | $self->{STORE}->{current_element} = $tr; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | } elsif ($tag eq 'td') { | ||||||||||||||||||||||||||||
| 63 | 29 | my $td = $attr; | |||||||||||||||||||||||||||
| 64 | 29 | push @{$self->{STORE}->{current_row}->{cells}}, $td; | |||||||||||||||||||||||||||
| 65 | 29 | ▶ | unless ( $self->{ATTR}{classic} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 25 | push @{$self->{STORE}->{current_row}->{headers}}, undef; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 29 | $self->{STORE}->{current_element} = $td; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | } elsif ($tag eq 'caption') { | ||||||||||||||||||||||||||||
| 71 | 2 | my $cap = $attr; | |||||||||||||||||||||||||||
| 72 | 2 | $self->{STORE}->{current_table}->{caption} = $cap; | |||||||||||||||||||||||||||
| 73 | 2 | $self->{STORE}->{current_element} = $cap; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||
| 76 | ## Found a non-table related tag. Push it into the currently-defined td | ||||||||||||||||||||||||||||
| 77 | ## or th (if one exists). | ||||||||||||||||||||||||||||
| 78 | 20 | my $elem = $self->{STORE}->{current_element}; | |||||||||||||||||||||||||||
| 79 | 20 | ▶ | if ($elem) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 4 | ▶ | $self->_debug('TEXT(tag) = ', $origtext) if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 4 | $elem->{data} .= $origtext; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 85 | ▶ | $self->_debug($origtext) if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 85 | return; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub text | ||||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||
| 95 | 113 | ▶ | my ($self, $text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
text: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 113 | my $elem = $self->{STORE}->{current_element}; | |||||||||||||||||||||||||||
| 97 | 113 | ▶ | if (!$elem) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 69 | return; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 44 | ▶ | $self->_debug('TEXT = ', $text) if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 44 | $elem->{data} .= $text; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 44 | return; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub end | ||||||||||||||||||||||||||||
| 110 | { | ||||||||||||||||||||||||||||
| 111 | 85 | ▶ | my ($self, $tag, $origtext) = @_; | ||||||||||||||||||||||||||
|
Subroutine
end: called CC=17 SCAR=28.4
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 85 | $tag = lc($tag); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Turn off the current object | ||||||||||||||||||||||||||||
| 115 | 85 | ▶ | if ($tag eq 'table') { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 116 | 6 | ▶ | my $prev = pop @{ $self->{STORE}{stack} } || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
pop @{$$self{'STORE'}{'stack'};} || []
MC/DC0%0/1
pop @{$$self{'STORE'}{'stack'};} || []
pop @{$$self{'STORE'}{'stack'};}
Truth table50%1/2
A
A= pop @{$$self{'STORE'}{'stack'};}
| |||||||||||||||||||||||||||||
| 117 | 6 | $self->{STORE}{$_} = $prev->{$_} for @stacked; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | } elsif ($tag eq 'th') { | ||||||||||||||||||||||||||||
| 120 | 8 | $self->{STORE}->{current_element} = undef; | |||||||||||||||||||||||||||
| 121 | } elsif ($tag eq 'tr') { | ||||||||||||||||||||||||||||
| 122 | 20 | ▶ | for my $key ( 'cells', $self->{ATTR}{classic} ? () : 'headers' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 35 | ▶ | my $data = $self->{STORE}{current_row}{$key} || []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'STORE'}{'current_row'}{$key} || []
MC/DC100%1/1
$self->{'STORE'}{'current_row'}{$key} || []
$self->{'STORE'}{'current_row'}{$key}
Truth table100%2/2
A
A= $self->{'STORE'}{'current_row'}{$key}
| |||||||||||||||||||||||||||||
| 124 | 35 | ▶ | pop @{ $data } while @{ $data } && ! $data->[-1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{$data;} and not $data->[-1]
MC/DC100%2/2
@{$data;} and not $data->[-1]
@{$data;}
not $data->[-1]
Truth table100%3/3
A and not(B)
A= @{$data;}B= not $data->[-1]
| |||||||||||||||||||||||||||||
| 125 | delete $self->{STORE}{current_row}{$key} | ||||||||||||||||||||||||||||
| 126 | 35 | ▶ | unless @{ $data }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 20 | $self->{STORE}->{current_row} = undef; | |||||||||||||||||||||||||||
| 129 | 20 | $self->{STORE}->{current_element} = undef; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | } elsif ($tag eq 'td') { | ||||||||||||||||||||||||||||
| 132 | 29 | $self->{STORE}->{current_element} = undef; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | } elsif ($tag eq 'caption') { | ||||||||||||||||||||||||||||
| 135 | 2 | $self->{STORE}->{current_element} = undef; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | } else { | ||||||||||||||||||||||||||||
| 138 | ## Found a non-table related close tag. Push it into the currently-defined | ||||||||||||||||||||||||||||
| 139 | ## td or th (if one exists). | ||||||||||||||||||||||||||||
| 140 | 20 | my $elem = $self->{STORE}->{current_element}; | |||||||||||||||||||||||||||
| 141 | 20 | ▶ | if ($elem) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 4 | ▶ | $self->_debug('TEXT(tag) = ', $origtext) if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 4 | $elem->{data} .= $origtext; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 85 | ▶ | $self->_debug($origtext) if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 85 | return; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub parse | ||||||||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||||||||
| 156 | 5 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 5 | ▶ | unless ( defined $data ) { # RT 7262 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 1 | require Carp; | |||||||||||||||||||||||||||
| 160 | 1 | Carp::croak( 'Argument must be defined' ); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | $self->{STORE} = { | ||||||||||||||||||||||||||||
| 164 | 4 | stack => [], | |||||||||||||||||||||||||||
| 165 | }; | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 4 | $self->SUPER::parse($data); | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 4 | my $tables = $self->{STORE}{tables}; | |||||||||||||||||||||||||||
| 170 | 4 | delete $self->{STORE}; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 4 | return $tables; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | sub _debug | ||||||||||||||||||||||||||||
| 179 | { | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my ( $self, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_debug: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 181 | 0 | my $class = ref($self); | |||||||||||||||||||||||||||
| 182 | 0 | warn "$class: ", join( '', @args ), "\n"; | |||||||||||||||||||||||||||
| 183 | 0 | return; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 2 | 1; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||||||||