Commit | Line | Data |
456a815d |
1 | package HTML::Zoom::Parser::BuiltIn; |
2 | |
1cf03540 |
3 | use strictures 1; |
d80786d0 |
4 | use base qw(HTML::Zoom::SubObject); |
456a815d |
5 | |
6 | sub html_to_events { |
d80786d0 |
7 | my ($self, $text) = @_; |
456a815d |
8 | my @events; |
9 | _hacky_tag_parser($text => sub { push @events, $_[0] }); |
10 | return \@events; |
11 | } |
12 | |
13 | sub html_to_stream { |
d80786d0 |
14 | my ($self, $text) = @_; |
15 | return $self->_zconfig->stream_utils |
16 | ->stream_from_array(@{$self->html_to_events($text)}); |
456a815d |
17 | } |
18 | |
af6300be |
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 | |
456a815d |
28 | sub _hacky_tag_parser { |
29 | my ($text, $handler) = @_; |
30 | while ( |
31 | $text =~ m{ |
32 | ( |
33 | (?:[^<]*) < (?: |
34 | ( / )? ( [^/!<>\s"'=]+ ) |
abc91e12 |
35 | ( (?:"[^"]*"|'[^']*'|[^/"'<>])+? )? |
456a815d |
36 | | |
37 | (!-- .*? -- | ![^\-] .*? ) |
38 | ) (\s*/\s*)? > |
39 | ) |
40 | ([^<]*) |
41 | }sxg |
42 | ) { |
e32e7b90 |
43 | my ($whole, $is_close, $tag_name, $attributes, $is_special, |
456a815d |
44 | $in_place_close, $content) |
45 | = ($1, $2, $3, $4, $5, $6, $7, $8); |
e32e7b90 |
46 | if ($is_special) { |
47 | $handler->({ type => 'SPECIAL', raw => $whole }); |
456a815d |
48 | } else { |
e32e7b90 |
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 =~ /^ +$/; |
456a815d |
54 | $handler->({ |
e32e7b90 |
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, |
456a815d |
61 | }); |
e32e7b90 |
62 | if ($in_place_close) { |
63 | $handler->({ |
64 | type => 'CLOSE', name => $tag_name, raw => '', |
65 | is_in_place_close => 1 |
66 | }); |
67 | } |
456a815d |
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/"/"/g; |
101 | $str =~ s/</</g; |
102 | $str =~ s/>/>/g; |
103 | $str =~ s/&/&/g; |
104 | $str; |
105 | } |
106 | |
107 | sub _simple_escape { |
108 | my $str = shift; |
109 | $str =~ s/&/&/g; |
110 | $str =~ s/"/"/g; |
111 | $str =~ s/</</g; |
112 | $str =~ s/>/>/g; |
113 | $str; |
114 | } |
115 | |
f8ed299b |
116 | sub html_escape { _simple_escape($_[1]) } |
117 | |
118 | sub html_unescape { _simple_unescape($_[1]) } |
119 | |
456a815d |
120 | 1; |