| 1 | package XML::Lenient; | ||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Method::Signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | our $VERSION = '1.0.1'; #See www.semver.org | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | has verbatim => ( | ||||||||||||||||||||||||||||
| 10 | is => 'rw', | ||||||||||||||||||||||||||||
| 11 | 6 | ▶ | default => sub{['applet', | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 'code', | ||||||||||||||||||||||||||||
| 13 | 'pre', | ||||||||||||||||||||||||||||
| 14 | 'script']}, | ||||||||||||||||||||||||||||
| 15 | 6 | ); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 6 | has tagl => ( | |||||||||||||||||||||||||||
| 18 | is => 'rw', | ||||||||||||||||||||||||||||
| 19 | default => '<', | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 6 | has tagr => ( | |||||||||||||||||||||||||||
| 23 | is => 'rw', | ||||||||||||||||||||||||||||
| 24 | default => '>', | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 6 | has tagc => ( | |||||||||||||||||||||||||||
| 28 | is => 'rw', | ||||||||||||||||||||||||||||
| 29 | default => '/', | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 6 | ▶ | method within($full, $tag, $n = 1) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
within: called CC=10 SCAR=23.1
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 33 | 83 | ▶ | if (!defined $tag or 0 == length($tag)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $tag or 0 == length $tag
MC/DC100%2/2
not defined $tag or 0 == length $tag
not defined $tag
0 == length $tag
Truth table100%3/3
not(A) or B
A= not defined $tagB= 0 == length $tag
| |||||||||||||||||||||||||||||
| 34 | 2 | return ''; | |||||||||||||||||||||||||||
| 35 | } else { | ||||||||||||||||||||||||||||
| 36 | 81 | my @vtagged; | |||||||||||||||||||||||||||
| 37 | 81 | $self->_stripverbatim(\@vtagged, \$full); | |||||||||||||||||||||||||||
| 38 | 81 | my $left = $self->{tagl} . $tag . '[ ' . $self->{tagr} . ']'; | |||||||||||||||||||||||||||
| 39 | 81 | ▶ | my $count = () = $full =~ /$left/g if defined $full; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 81 | my $wanted = ''; | |||||||||||||||||||||||||||
| 41 | 81 | _tidyindex(\$n); | |||||||||||||||||||||||||||
| 42 | 81 | my @stack; | |||||||||||||||||||||||||||
| 43 | 81 | $self->_buildstack(\@stack, $full, $tag); | |||||||||||||||||||||||||||
| 44 | 81 | ▶ | if ($n <= $#stack) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 77 | ▶ | $wanted = substr($full, $stack[$n][0], $stack[$n][1] - $stack[$n][0]) if scalar @stack; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 77 | $self->_restoreverbatim(\@vtagged, \$wanted); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 81 | return $wanted; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 6 | ▶ | func _tidyindex($refn) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
_tidyindex: called CC=7 SCAR=19.5
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 53 | 86 | ▶ | $$refn =~ s/[^-0-9]//g if defined $$refn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 86 | ▶ | $$refn = 1 unless defined $$refn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 86 | ▶ | $$refn = 1 if 0 == length($$refn); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 86 | my $right = substr($$refn, 1); | |||||||||||||||||||||||||||
| 57 | 86 | $right =~ s/-//g; | |||||||||||||||||||||||||||
| 58 | 86 | $$refn = substr($$refn, 0, 1) . $right; | |||||||||||||||||||||||||||
| 59 | 86 | ▶ | $$refn-- unless $$refn < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 6 | ▶ | method _buildstack($refstack, $full, $tag) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
_buildstack: called CC=12 SCAR=25.0
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 63 | my $regex = '((?:' . $self->{tagl} | ||||||||||||||||||||||||||||
| 64 | . $self->{tagc} . '?' . $tag . ') *?.*?' | ||||||||||||||||||||||||||||
| 65 | 88 | . $self->{tagr} . ')'; | |||||||||||||||||||||||||||
| 66 | 88 | ▶ | if (defined $full) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 87 | ▶ | while ($full =~ m/$regex/gi) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 399 | ▶ | if ($self->{tagl} . $self->{tagc} . $tag . $self->{tagr} eq $1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 191 | ▶ | for (my $idx = $#$refstack; 0 <= $idx; $idx--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 254 | ▶ | if (!defined $$refstack[$idx][1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 191 | $$refstack[$idx][1] = pos($full) - length($1); | |||||||||||||||||||||||||||
| 72 | 191 | last; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||
| 76 | 208 | push @$refstack, [pos($full), undef]; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 88 | for my $lineref (@$refstack) { | |||||||||||||||||||||||||||
| 81 | 208 | ▶ | if (!defined $$lineref[1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 17 | $$lineref[1] = length($full); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 6 | ▶ | method _restoreverbatim($refvtagged, $reftext) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
_restoreverbatim: called CC=9 SCAR=22.3
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 88 | 77 | for my $vtag(@{$self->{verbatim}}) { | |||||||||||||||||||||||||||
| 89 | 308 | my $lv = $self->{tagl} . $vtag . $self->{tagr}; | |||||||||||||||||||||||||||
| 90 | 308 | my $rv = $self->{tagl} . $self->{tagc} . $vtag . $self->{tagr}; | |||||||||||||||||||||||||||
| 91 | 308 | my $regex = $lv . '(\d+?)' . $rv; #Should be impossible for a D to appear. | |||||||||||||||||||||||||||
| 92 | 308 | my @local; | |||||||||||||||||||||||||||
| 93 | 308 | ▶ | if (scalar @$refvtagged and defined $$reftext) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar @$refvtagged and defined $$reftext
MC/DC50%1/2
scalar @$refvtagged and defined $$reftext
scalar @$refvtagged
defined $$reftext
Truth table66%2/3
A and B
A= scalar @$refvtaggedB= defined $$reftext
| |||||||||||||||||||||||||||||
| 94 | 12 | @local = $$reftext =~ /$regex/g; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 308 | for my $idx (@local) { | |||||||||||||||||||||||||||
| 97 | 1 | ▶ | if (defined $$refvtagged[$idx]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 1 | $regex = $lv . $idx . $rv; | |||||||||||||||||||||||||||
| 99 | 1 | $$reftext =~ s/$regex/$$refvtagged[$idx]/; | |||||||||||||||||||||||||||
| 100 | 1 | $$refvtagged[$idx] = undef; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 6 | ▶ | method _stripverbatim($refvtagged, $reffull) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
_stripverbatim: called CC=8 SCAR=20.9
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 107 | 82 | for my $vtag(@{$self->{verbatim}}) { | |||||||||||||||||||||||||||
| 108 | 328 | my $lv = $self->{tagl} . $vtag . '(?: |' . $self->{tagr} . ')'; | |||||||||||||||||||||||||||
| 109 | 328 | my $rv = $self->{tagl} . $self->{tagc} . $vtag . $self->{tagr}; | |||||||||||||||||||||||||||
| 110 | 328 | my $regex = $lv . '.+?' . $rv; | |||||||||||||||||||||||||||
| 111 | 328 | my $nbase = scalar @$refvtagged; | |||||||||||||||||||||||||||
| 112 | 328 | my @local; | |||||||||||||||||||||||||||
| 113 | 328 | ▶ | @local = $$reffull =~ /($regex)/gmsi if defined $$reffull; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 328 | push @$refvtagged, @local; | |||||||||||||||||||||||||||
| 115 | 328 | ▶ | if (-1 < $#$refvtagged) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 10 | for my $n($nbase..$#$refvtagged) { | |||||||||||||||||||||||||||
| 117 | 5 | my $rplc = $self->{tagl} . $vtag . $self->{tagr} . $n . $rv; | |||||||||||||||||||||||||||
| 118 | 5 | $$reffull =~ s/$$refvtagged[$n]/$rplc/ms; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 6 | ▶ | method innertext($full) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
innertext: called CC=4 SCAR=13.9
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 125 | 25 | ▶ | if ($self->{tagl} ne substr($full, 0, length($self->{tagl}))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 8 | return $full; | |||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||
| 128 | 17 | my $regex = '^' . $self->{tagl} . '(.+?)(?: |' . $self->{tagr} . ')'; | |||||||||||||||||||||||||||
| 129 | 17 | my ($tag) = $full =~ m/$regex/i; | |||||||||||||||||||||||||||
| 130 | 17 | return $self->innertext($self->within($full, $tag)); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 6 | ▶ | method tagval($full, $tag, $n = 1) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
tagval: called CC=9 SCAR=22.0
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 135 | 7 | ▶ | if (!defined $tag or 0 == length($tag)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $tag or 0 == length $tag
MC/DC100%2/2
not defined $tag or 0 == length $tag
not defined $tag
0 == length $tag
Truth table100%3/3
not(A) or B
A= not defined $tagB= 0 == length $tag
| |||||||||||||||||||||||||||||
| 136 | 2 | return ''; | |||||||||||||||||||||||||||
| 137 | } else { | ||||||||||||||||||||||||||||
| 138 | 5 | my @stack; | |||||||||||||||||||||||||||
| 139 | 5 | $self->_buildstack(\@stack, $full, $tag); | |||||||||||||||||||||||||||
| 140 | 5 | _tidyindex(\$n); | |||||||||||||||||||||||||||
| 141 | 5 | ▶ | if (defined $stack[$n][0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 4 | my $end = $stack[$n][1]; | |||||||||||||||||||||||||||
| 143 | 4 | my $ltag = $self->{tagl} . $tag . ' '; | |||||||||||||||||||||||||||
| 144 | 4 | my $start = index($full, $ltag) + length($ltag); | |||||||||||||||||||||||||||
| 145 | 4 | ▶ | if (0 < $n) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 3 | my $offset = $stack[$n-1][0]; | |||||||||||||||||||||||||||
| 147 | 3 | $start = index($full, $ltag, $offset) + length($ltag); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 4 | my $subml = substr($full, $start, $end - $start); | |||||||||||||||||||||||||||
| 150 | 4 | my $tagval = substr($full, $start, ); | |||||||||||||||||||||||||||
| 151 | 4 | $tagval =~ s/$self->{tagr}.*$//ms; | |||||||||||||||||||||||||||
| 152 | 4 | return $tagval; | |||||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||||
| 154 | 1 | return ''; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 6 | ▶ | method wpath($full, $path) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
wpath: called CC=6 SCAR=18.1
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 160 | 9 | my @steps = split(/\//, $path); | |||||||||||||||||||||||||||
| 161 | 9 | for (@steps) { | |||||||||||||||||||||||||||
| 162 | 42 | my ($tag, $idx) = split(/\[/, $_); | |||||||||||||||||||||||||||
| 163 | 42 | ▶ | $full = $self->within($full, $tag, $idx) if defined $tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 9 | return $full; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 6 | ▶ | method tagcount($full, $tag) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
tagcount: called CC=4 SCAR=14.1
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 169 | 2 | my @stack; | |||||||||||||||||||||||||||
| 170 | 2 | $self->_buildstack(\@stack, $full, $tag); | |||||||||||||||||||||||||||
| 171 | 2 | return scalar @stack; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =pod | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head1 NAME | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | XML::Lenient - extracts strings from HTML, XML and similarly tagged text. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | use XML::Lenient; | ||||||||||||||||||||||||||||
| 183 | my $p = XML::Lenient->new(); #This is the same as: | ||||||||||||||||||||||||||||
| 184 | my $p = XML::Lenient->new( | ||||||||||||||||||||||||||||
| 185 | tagl => '<', | ||||||||||||||||||||||||||||
| 186 | tagr => '>', | ||||||||||||||||||||||||||||
| 187 | tagc => '/', | ||||||||||||||||||||||||||||
| 188 | verbatim => ['applet', 'code', 'pre', 'script'] | ||||||||||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||||||||||
| 190 | my $subtext = $p->within($ml, $tag, $index); | ||||||||||||||||||||||||||||
| 191 | $subtext = $p->innertext($ml); | ||||||||||||||||||||||||||||
| 192 | $subtext = $p->wpath($ml, $path); | ||||||||||||||||||||||||||||
| 193 | my $tagvalue = $p->tagval($ml, $tag, $index); | ||||||||||||||||||||||||||||
| 194 | my $count = $p->tagcount($ml, $tag); | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head2 What | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | XML::Lenient is meant to parse markup languages such as HTML and | ||||||||||||||||||||||||||||
| 201 | XML in the knowledge that someone, somewhere, is going to break | ||||||||||||||||||||||||||||
| 202 | every rule in the book. It will handle malformed XML, wrongly | ||||||||||||||||||||||||||||
| 203 | nested HTML tags and everything else that I have thrown at it. | ||||||||||||||||||||||||||||
| 204 | The results may not be what the author of the mistake expected, | ||||||||||||||||||||||||||||
| 205 | but that's their headache. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head2 Why | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | My original intent when writing it was to enable me to write | ||||||||||||||||||||||||||||
| 210 | tests when developing code that generates HTML. I want to be | ||||||||||||||||||||||||||||
| 211 | able to change any aspect of the HTML without breaking every | ||||||||||||||||||||||||||||
| 212 | test previously written. I could find no way of doing this with | ||||||||||||||||||||||||||||
| 213 | existing tools, so I wrote this. It is based on a module I | ||||||||||||||||||||||||||||
| 214 | wrote for Excel in VBA that I use for web scraping. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head2 How | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | All methods take a string and return a string (an integer in one | ||||||||||||||||||||||||||||
| 219 | case). There are no complicated data structures. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =head3 The object | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | my $p = XML::Lenient->new(); | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | For most cases, the defaults will be all you need. | ||||||||||||||||||||||||||||
| 226 | You could specify the version below, but it merely applies the | ||||||||||||||||||||||||||||
| 227 | defaults. However, it shows how to specify the properties | ||||||||||||||||||||||||||||
| 228 | should you have a need. | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | my $p = XML::Lenient->new( | ||||||||||||||||||||||||||||
| 231 | tagl => '<', | ||||||||||||||||||||||||||||
| 232 | tagr => '>', | ||||||||||||||||||||||||||||
| 233 | tagc => '/', | ||||||||||||||||||||||||||||
| 234 | verbatim => ['applet', 'code', 'pre', 'script'] | ||||||||||||||||||||||||||||
| 235 | ); | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | The parser object has four properties. Three of these describe | ||||||||||||||||||||||||||||
| 238 | how a tag appears. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | Property Default Meaning | ||||||||||||||||||||||||||||
| 241 | tagl < The leftmost character(s) of a tag | ||||||||||||||||||||||||||||
| 242 | tagr > The rightmost character(s) of a tag | ||||||||||||||||||||||||||||
| 243 | tagc / The character(s) indicating a closing tag | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | While the code has been written to handle multiple characters | ||||||||||||||||||||||||||||
| 246 | in each delimiter, I know of no use case for this and have | ||||||||||||||||||||||||||||
| 247 | written no tests. I do know that | ||||||||||||||||||||||||||||
| 248 | L<Template::Toolkit|http://search.cpan.org/~abw/Template-Toolkit-2.26/lib/Template/Toolkit.pod> | ||||||||||||||||||||||||||||
| 249 | defaults to two character delimiters but cannot see a situation in | ||||||||||||||||||||||||||||
| 250 | which it would constitute tag delimited text. | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | The fourth property is an array of "verbatim" tags. Data within | ||||||||||||||||||||||||||||
| 253 | such tags will not be treated as tags. If you want to use this | ||||||||||||||||||||||||||||
| 254 | parser on data within verbatim tags, create a parser object | ||||||||||||||||||||||||||||
| 255 | without the verbatim tag(s) that are the problem. There is | ||||||||||||||||||||||||||||
| 256 | nothing to prevent you having two parser objects with different | ||||||||||||||||||||||||||||
| 257 | properties and using the right one for each task. | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head3 Indexing | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | Several of the methods allow an index, although this is always optional. | ||||||||||||||||||||||||||||
| 262 | The L</wpath> method attempts compatibility with XPath. In consequence, | ||||||||||||||||||||||||||||
| 263 | all positive indices are 1 based. However, negative indices work as in | ||||||||||||||||||||||||||||
| 264 | Perl generally, meaning that zero and negative indices are zero based. | ||||||||||||||||||||||||||||
| 265 | The effect of this is that there is finally a use case where 0 == 1, | ||||||||||||||||||||||||||||
| 266 | which I have to admit causes me a little amusement. When working with | ||||||||||||||||||||||||||||
| 267 | an index, it is the appearance of the opening tag that is counted; | ||||||||||||||||||||||||||||
| 268 | nesting is ignored. If an index is given that is higher than can be found, | ||||||||||||||||||||||||||||
| 269 | a zero length string will be returned. An index of zero (or undef or a | ||||||||||||||||||||||||||||
| 270 | zero length string) will return the first item. | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | =head3 within | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | $p->within($ml, $tag, $index); | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | within returns the text between an opening tag and a closing | ||||||||||||||||||||||||||||
| 277 | tag. So: | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | my $p = XML::Lenient->new(); | ||||||||||||||||||||||||||||
| 280 | my $ml = '<x>asdf</x>'; | ||||||||||||||||||||||||||||
| 281 | my $within = $p->within($ml, 'x'); | ||||||||||||||||||||||||||||
| 282 | ok ('asdf' eq $within, "Simple within works"); | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | The index is optional and defaults to 1. See L</Indexing> for more | ||||||||||||||||||||||||||||
| 285 | information. So: | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | $ml = '<x><x><x>asdf</x></x></x><x>qwer</x>'; | ||||||||||||||||||||||||||||
| 288 | $within = $p->within($ml, 'x', 2); | ||||||||||||||||||||||||||||
| 289 | ok ('<x>asdf</x>' eq $within, "Indexing works with nested tags"); | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | Zero and negative indices work in the same was as in Perl generally. | ||||||||||||||||||||||||||||
| 292 | Again, see L</Indexing> for more information. So: | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | $ml = '<x><x><x>asdf</x></x></x><x>qwer</x>'; | ||||||||||||||||||||||||||||
| 295 | $within = $p->within($ml, 'x', 0); | ||||||||||||||||||||||||||||
| 296 | ok ('<x><x>asdf</x></x>' eq $within, | ||||||||||||||||||||||||||||
| 297 | "Zero index is the first element"); | ||||||||||||||||||||||||||||
| 298 | $within = $p->within($ml, 'x', -1); | ||||||||||||||||||||||||||||
| 299 | ok ('qwer' eq $within, | ||||||||||||||||||||||||||||
| 300 | "Negative index works as in Perl"); | ||||||||||||||||||||||||||||
| 301 | $within = $p->within($ml, 'x', -2); | ||||||||||||||||||||||||||||
| 302 | ok ('asdf' eq $within, | ||||||||||||||||||||||||||||
| 303 | "Negative indices continue working backwards"); | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | Extraneous tags are ignored whether they are matched properly: | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | $ml = '<x><y><z>asdf</z></y></x>'; | ||||||||||||||||||||||||||||
| 308 | $within = $p->within($ml, 'z'); | ||||||||||||||||||||||||||||
| 309 | ok ('asdf' eq $within, 'Outer non-target tags ignored'); | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | or mismatched: | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | $ml = '<x><y>asdf</x></y>'; | ||||||||||||||||||||||||||||
| 314 | $within = $p->within($ml, 'x'); | ||||||||||||||||||||||||||||
| 315 | ok ('<y>asdf' eq $within, "Mismatched tags return something sensible"); | ||||||||||||||||||||||||||||
| 316 | $within = $p->within($ml, 'y'); | ||||||||||||||||||||||||||||
| 317 | ok ('asdf</x>' eq $within, | ||||||||||||||||||||||||||||
| 318 | "Mismatched tags return something sensible again"); | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | An unclosed tag is considered to be closed implicitly at the end | ||||||||||||||||||||||||||||
| 321 | of the data. With nested unmatched tags, the closing tag(s) are | ||||||||||||||||||||||||||||
| 322 | allocated to the innermost open tag first. So: | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | $ml = '<x><x>asdf</x>'; | ||||||||||||||||||||||||||||
| 325 | $within = $p->within($ml, 'x'); | ||||||||||||||||||||||||||||
| 326 | ok ('<x>asdf</x>' eq $within, "Handles unclosed tags"); | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =head3 wpath | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | $subtext = $p->wpath($ml, $path); | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | This is a wrapper around within that allows a path to be | ||||||||||||||||||||||||||||
| 333 | specified in XPath format. A valid XPath that uses no | ||||||||||||||||||||||||||||
| 334 | indexing is always a valid wpath. But wpath is far more lenient | ||||||||||||||||||||||||||||
| 335 | and will accept the omission of any step in the path that is | ||||||||||||||||||||||||||||
| 336 | not required to identify the wanted text. Indeed, wpath will be | ||||||||||||||||||||||||||||
| 337 | faster if such steps are omitted. So, with 80 lines of HTML I | ||||||||||||||||||||||||||||
| 338 | sha'n't repeat here: | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | my $text = $p->wpath($ml, | ||||||||||||||||||||||||||||
| 341 | '/html/body/div/div/div/table[1]/tbody/tr[2]/td[2]'); | ||||||||||||||||||||||||||||
| 342 | ok('17' eq $text, "Full xpath works"); | ||||||||||||||||||||||||||||
| 343 | $text = $p->wpath($ml, 'tbody/tr[2]/td[2]'); | ||||||||||||||||||||||||||||
| 344 | ok('17' eq $text, "Abbreviated wpath works"); | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | Both variants work with the second being faster. If you know | ||||||||||||||||||||||||||||
| 347 | there are no <tr> tags in the table header (there are in the | ||||||||||||||||||||||||||||
| 348 | test HTML), the tbody could be omitted, too. Since each step | ||||||||||||||||||||||||||||
| 349 | in a path is split using the '[' character and non-numeric | ||||||||||||||||||||||||||||
| 350 | characters deleted, it is perfectly acceptable in a wpath to | ||||||||||||||||||||||||||||
| 351 | omit the closing ']'. Indeed, the lenient approach means you | ||||||||||||||||||||||||||||
| 352 | can do some spectacularly improper things to the wpath and it | ||||||||||||||||||||||||||||
| 353 | will still work. Just don't tell anyone I advised it. | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | An XPath that uses an index will differ from a wpath if and | ||||||||||||||||||||||||||||
| 356 | only if the tag is repeated both at the same nesting level | ||||||||||||||||||||||||||||
| 357 | and at a deeper nesting level before the tag that XPath would | ||||||||||||||||||||||||||||
| 358 | select. So: | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | my $ml = '<x><x>asdf</x></x><x>qwer</x>'; | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | Using x[2], XPath would return qwer while wpath would return | ||||||||||||||||||||||||||||
| 363 | asdf. | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =head3 tagval | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | $p->tagval($ml, $tag, $index); | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | I have never needed this, but I can envisage doing so. tagval | ||||||||||||||||||||||||||||
| 370 | returns the attributes inside a tag as opposed to the data | ||||||||||||||||||||||||||||
| 371 | between an opening and closing tag. So: | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | my $ml = '<a href="www.example.com">Click</a>'; | ||||||||||||||||||||||||||||
| 374 | my $val = $p->tagval($ml, 'a'); | ||||||||||||||||||||||||||||
| 375 | ok ('href="www.example.com"' eq $val, 'Simple tagval works'); | ||||||||||||||||||||||||||||
| 376 | $within = $p->within($ml, 'a'); | ||||||||||||||||||||||||||||
| 377 | ok ('Click' eq $within, "Within tags with values works"); | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | As usual, the index is optional and defaults to 1. | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | =head3 innertext | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | $p->innertext($ml); | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | This is something I need when scraping web pages that change | ||||||||||||||||||||||||||||
| 386 | their format without warning. Provided I have some pointers | ||||||||||||||||||||||||||||
| 387 | to the data, innertext will return the text from within any | ||||||||||||||||||||||||||||
| 388 | tags it may find, starting at the first non-tag character. So: | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | $ml = '<x><x><x><x>asdf</x></x></x></x>'; | ||||||||||||||||||||||||||||
| 391 | $inner = $p->innertext($ml); | ||||||||||||||||||||||||||||
| 392 | ok('asdf' eq $inner, 'Tags 4 deep works'); | ||||||||||||||||||||||||||||
| 393 | $ml = '<x>asdf<x><x><x>asdf</x></x></x></x>'; | ||||||||||||||||||||||||||||
| 394 | $inner = $p->innertext($ml); | ||||||||||||||||||||||||||||
| 395 | ok('asdf<x><x><x>asdf</x></x></x>' eq $inner, | ||||||||||||||||||||||||||||
| 396 | 'Deep tags returned after text starts'); | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | Provided there is no text between the leading tags, mismatched | ||||||||||||||||||||||||||||
| 399 | tags will not cause problems: | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | $ml = '<x><y>asdf</x></y>'; | ||||||||||||||||||||||||||||
| 402 | $inner = $p->innertext($ml); | ||||||||||||||||||||||||||||
| 403 | ok('asdf' eq $inner, 'Magic happens to mismatched tags'); | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =head3 tagcount | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | my $count = $p->tagcount($ml, $tag); | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | Returns the number of opening target tags in $ml or zero if it | ||||||||||||||||||||||||||||
| 410 | doesn't exist. This is the exception that returns an integer | ||||||||||||||||||||||||||||
| 411 | rather than a string. | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | my $ml = '<div><div><div><div>asdf</div></div></div></div>'; | ||||||||||||||||||||||||||||
| 414 | my $n = $p->tagcount($ml, 'div'); | ||||||||||||||||||||||||||||
| 415 | ok (4 == $n, 'Correct number of div tags'); | ||||||||||||||||||||||||||||
| 416 | $n = $p->tagcount($ml, 'x'); | ||||||||||||||||||||||||||||
| 417 | ok (0 == $n, 'Correct number of x tags'); | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | =head1 LIMITATIONS | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | XML::Lenient cannot handle more text than will fit in memory. | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | Tags cannot be split over more than one line (but having | ||||||||||||||||||||||||||||
| 424 | the opening and closing tags on different lines is fine). If | ||||||||||||||||||||||||||||
| 425 | you have split tags, a possible workaround is to change | ||||||||||||||||||||||||||||
| 426 | carriage returns and line feeds to spaces before passing the | ||||||||||||||||||||||||||||
| 427 | string to the parser. | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | Neither YAML nor JSON can be handled. | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | Internally, XML::Lenient uses lots of regexes (and other things as | ||||||||||||||||||||||||||||
| 432 | well. I know regexes alone aren't enough). I have NOT tried to | ||||||||||||||||||||||||||||
| 433 | prevent users from writing regex-style elements in tags, but I | ||||||||||||||||||||||||||||
| 434 | dread to think what will happen if someone tries. By all means | ||||||||||||||||||||||||||||
| 435 | try, though. And may your God go with you. | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =head1 PRESENTATION VIDEO | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | I presented this to the London Perl Mongers on 2016-04-21. | ||||||||||||||||||||||||||||
| 440 | There is a YouTube video at L<https://www.youtube.com/watch?v=3d_pkd4OkHQ&index=6&list=PL9L8-lcxZ4z0yP0jRd_7HCygBblDbXNO5>. | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | John Davies | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | =head1 MODULES USED | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | L<Moo|http://search.cpan.org/~haarg/Moo-2.001001/lib/Moo.pm> | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | L<Method::Signatures|http://search.cpan.org/~barefoot/Method-Signatures-20160315/lib/Method/Signatures.pm> | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | I tried - and had problems with - several modules. I may be | ||||||||||||||||||||||||||||
| 455 | the problem. But I tried: | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | L<HTML::Parser|http://search.cpan.org/dist/HTML-Parser/Parser.pm> | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | L<HTML::TreeBuilder|http://search.cpan.org/~cjm/HTML-Tree-5.03/lib/HTML/TreeBuilder.pm> | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | L<HTML::Element|http://search.cpan.org/~cjm/HTML-Tree-5.03/lib/HTML/Element.pm> | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | L<XML::XPath|http://search.cpan.org/~msergeant/XML-XPath-1.13/XPath.pm> | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | L<XML::LibXML|http://search.cpan.org/dist/XML-LibXML/LibXML.pod> | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | and they may be better for you than this. | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | =head1 VERSION | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | 1.0.1. See L<http://www.semver.org>. | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | Copyright (c) 2016, John Davies. All rights reserved. | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | =head1 LICENCE | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | This module is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 480 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | =cut | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | 6 | 1; | |||||||||||||||||||||||||||