File Coverage

blib/lib/Moonshine/Parser/HTML.pm
Criterion Covered Total %
statement 12 61 19.6
branch 0 12 0.0
condition n/a
subroutine 4 13 30.7
pod 6 6 100.0
total 22 92 23.9


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