line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Moonshine::Parser::HTML; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
20
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
19
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
3
|
use base qw/HTML::Parser/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
473
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
4584
|
use Moonshine::Element; |
|
1
|
|
|
|
|
21209
|
|
|
1
|
|
|
|
|
422
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Moonshine::Parser::HTML - Parse html into a Moonshine::Element object. |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 VERSION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Version 0.05 |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub new { |
23
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
24
|
0
|
|
|
|
|
|
my $self = bless { |
25
|
|
|
|
|
|
|
elements => [ ], |
26
|
|
|
|
|
|
|
closed => [ ], |
27
|
|
|
|
|
|
|
base_element => undef, |
28
|
|
|
|
|
|
|
}, $class; |
29
|
0
|
|
|
|
|
|
$self->SUPER::init(@_); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 SYNOPSIS |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
use Moonshine::Parser::HTML; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my $parser = Moonshine::Parser::HTML:->new(); |
37
|
|
|
|
|
|
|
my $moonshine_element = $parser->parse($html); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head2 parse |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Parse html string into a Moonshine::Element Object. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=cut |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub parse { |
48
|
0
|
|
|
0
|
1
|
|
my ( $self, $data ) = @_; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
$self->SUPER::parse($data); |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
return $self->{base_element}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head2 parse_file |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Parse a file that contains html into a Moonshine::Element. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=cut |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub parse_file { |
62
|
0
|
|
|
0
|
1
|
|
my ( $self, $file ) = @_; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
$self->SUPER::parse_file($file); |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
return $self->{base_element}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub start { |
70
|
0
|
|
|
0
|
1
|
|
my ( $self, $tag, $attr) = @_; |
71
|
0
|
|
|
|
|
|
my $closed = delete $attr->{'/'}; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
$attr->{tag} = lc $tag; |
74
|
0
|
|
|
|
|
|
$attr->{data} = [ ]; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
my $element; |
77
|
0
|
0
|
|
|
|
|
if ( my $current_element = $self->_current_element ) { |
78
|
0
|
|
|
|
|
|
$element = $current_element->add_child($attr); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
else { |
81
|
0
|
|
|
|
|
|
$element = Moonshine::Element->new($attr); |
82
|
0
|
0
|
|
|
|
|
if ( my $base_element = $self->{base_element} ) { |
83
|
|
|
|
|
|
|
my $action = |
84
|
|
|
|
|
|
|
$self->_is_closed( $base_element->{guid} ) |
85
|
0
|
0
|
|
|
|
|
? 'add_after_element' |
86
|
|
|
|
|
|
|
: 'add_child'; |
87
|
0
|
|
|
|
|
|
$base_element->$action($element); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
else { |
90
|
0
|
|
|
|
|
|
$self->{base_element} = $element; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
0
|
0
|
|
|
|
|
push @{ $self->{elements} }, $element |
|
0
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
unless $closed; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub text { |
98
|
0
|
|
|
0
|
1
|
|
my ( $self, $text ) = @_; |
99
|
0
|
0
|
|
|
|
|
if ( $text =~ m{\S+}xms ) { |
100
|
0
|
|
|
|
|
|
my $element = $self->_current_element; |
101
|
0
|
|
|
|
|
|
$text =~ s{^\s+|\s+$}{}g; |
102
|
|
|
|
|
|
|
|
103
|
0
|
0
|
|
|
|
|
if ($element->has_children) { |
104
|
0
|
|
|
|
|
|
my $data = $element->children; |
105
|
0
|
|
|
|
|
|
push @{ $element->{data} }, @{ $data }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
$element->children([]); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
$element->data($text); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub end { |
114
|
0
|
|
|
0
|
1
|
|
my ( $self, $tag, $origtext ) = @_; |
115
|
0
|
|
|
|
|
|
my $close = pop @{ $self->{elements} }; |
|
0
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
push @{ $self->{closed} }, $close->{guid}; |
|
0
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub _current_element { |
120
|
0
|
|
|
0
|
|
|
my $count = scalar @{ $_[0]->{elements} }; |
|
0
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
return $_[0]->{elements}[ $count - 1 ]; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub _is_closed { |
125
|
0
|
|
|
0
|
|
|
return grep { $_ =~ m/^$_[1]$/ } @{ $_[0]->{closed} }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 AUTHOR |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Robert Acock, C<< >> |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 BUGS |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
135
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
136
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 SUPPORT |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
perldoc Moonshine::Parser |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
You can also look for information at: |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=over 4 |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
L |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
L |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item * CPAN Ratings |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
L |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item * Search CPAN |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
L |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=back |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Copyright 2017 Robert Acock. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
173
|
|
|
|
|
|
|
under the terms of the the Artistic License (2.0). You may obtain a |
174
|
|
|
|
|
|
|
copy of the full license at: |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
L |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Any use, modification, and distribution of the Standard or Modified |
179
|
|
|
|
|
|
|
Versions is governed by this Artistic License. By using, modifying or |
180
|
|
|
|
|
|
|
distributing the Package, you accept this license. Do not use, modify, |
181
|
|
|
|
|
|
|
or distribute the Package, if you do not accept this license. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
If your Modified Version has been derived from a Modified Version made |
184
|
|
|
|
|
|
|
by someone other than you, you are nevertheless required to ensure that |
185
|
|
|
|
|
|
|
your Modified Version complies with the requirements of this license. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
This license does not grant you the right to use any trademark, service |
188
|
|
|
|
|
|
|
mark, tradename, or logo of the Copyright Holder. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
This license includes the non-exclusive, worldwide, free-of-charge |
191
|
|
|
|
|
|
|
patent license to make, have made, use, offer to sell, sell, import and |
192
|
|
|
|
|
|
|
otherwise transfer the Package with respect to any patent claims |
193
|
|
|
|
|
|
|
licensable by the Copyright Holder that are necessarily infringed by the |
194
|
|
|
|
|
|
|
Package. If you institute patent litigation (including a cross-claim or |
195
|
|
|
|
|
|
|
counterclaim) against any party alleging that the Package constitutes |
196
|
|
|
|
|
|
|
direct or contributory patent infringement, then this Artistic License |
197
|
|
|
|
|
|
|
to you shall terminate on the date that such litigation is filed. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER |
200
|
|
|
|
|
|
|
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. |
201
|
|
|
|
|
|
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
202
|
|
|
|
|
|
|
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY |
203
|
|
|
|
|
|
|
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR |
204
|
|
|
|
|
|
|
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR |
205
|
|
|
|
|
|
|
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, |
206
|
|
|
|
|
|
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=cut |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
1; # End of Moonshine::Parser |