add comment to tell people to stop being scared of the parser
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / Parser / BuiltIn.pm
1 package HTML::Zoom::Parser::BuiltIn;
2
3 use strictures 1;
4 use base qw(HTML::Zoom::SubObject);
5
6 sub html_to_events {
7   my ($self, $text) = @_;
8   my @events;
9   _hacky_tag_parser($text => sub { push @events, $_[0] });
10   return \@events;
11 }
12
13 sub html_to_stream {
14   my ($self, $text) = @_;
15   return $self->_zconfig->stream_utils
16               ->stream_from_array(@{$self->html_to_events($text)});
17 }
18
19 # DO NOT BE AFRAID.
20 #
21 # Well, ok. Be afraid. A little. But this is lexing HTML with a regexp,
22 # not really parsing (since the structure nesting isn't handled here) so
23 # it's relatively not dangerous.
24 #
25 # Certainly it's not really any more or any less heinous than anything else
26 # I could do in a handful of lines of pure perl.
27
28 sub _hacky_tag_parser {
29   my ($text, $handler) = @_;
30   while (
31     $text =~ m{
32       (
33         (?:[^<]*) < (?:
34             ( / )? ( [^/!<>\s"'=]+ )
35             ( (?:"[^"]*"|'[^']*'|[^/"'<>])+? )?
36         |   
37             (!-- .*? -- | ![^\-] .*? )
38         ) (\s*/\s*)? >
39       )
40       ([^<]*)
41     }sxg
42   ) {
43     my ($whole, $is_close, $tag_name, $attributes, $is_special,
44         $in_place_close, $content)
45       = ($1, $2, $3, $4, $5, $6, $7, $8);
46     if ($is_special) {
47       $handler->({ type => 'SPECIAL', raw => $whole });
48     } else {
49       $tag_name =~ tr/A-Z/a-z/;
50       if ($is_close) {
51         $handler->({ type => 'CLOSE', name => $tag_name, raw => $whole });
52       } else {
53         $attributes = '' if !defined($attributes) or $attributes =~ /^ +$/;
54         $handler->({
55           type => 'OPEN',
56           name => $tag_name,
57           is_in_place_close => $in_place_close,
58           _hacky_attribute_parser($attributes),
59           raw_attrs => $attributes||'',
60           raw => $whole,
61         });
62         if ($in_place_close) {
63           $handler->({
64             type => 'CLOSE', name => $tag_name, raw => '',
65             is_in_place_close => 1
66           });
67         }
68       }
69     }
70     if (length $content) {
71       $handler->({ type => 'TEXT', raw => $content });
72     }
73   }
74 }
75
76 sub _hacky_attribute_parser {
77   my ($attr_text) = @_;
78   my (%attrs, @attr_names);
79   while (
80     $attr_text =~ m{
81       ([^\s\=\"\']+)(\s*=\s*(?:(")(.*?)"|(')(.*?)'|([^'"\s=]+)['"]*))?
82     }sgx
83   ) {
84     my $key  = $1;
85     my $test = $2;
86     my $val  = ( $3 ? $4 : ( $5 ? $6 : $7 ));
87     my $lckey = lc($key);
88     if ($test) {
89       $attrs{$lckey} = _simple_unescape($val);
90     } else {
91       $attrs{$lckey} = $lckey;
92     }
93     push(@attr_names, $lckey);
94   }
95   (attrs => \%attrs, attr_names => \@attr_names);
96 }
97
98 sub _simple_unescape {
99   my $str = shift;
100   $str =~ s/&quot;/"/g;
101   $str =~ s/&lt;/</g;
102   $str =~ s/&gt;/>/g;
103   $str =~ s/&amp;/&/g;
104   $str;
105 }
106
107 sub _simple_escape {
108   my $str = shift;
109   $str =~ s/&/&amp;/g;
110   $str =~ s/"/&quot;/g;
111   $str =~ s/</&lt;/g;
112   $str =~ s/>/&gt;/g;
113   $str;
114 }
115
116 sub html_escape { _simple_escape($_[1]) }
117
118 sub html_unescape { _simple_unescape($_[1]) }
119
120 1;