Commit | Line | Data |
456a815d |
1 | package HTML::Zoom::FilterBuilder; |
2 | |
456a815d |
3 | use strict; |
4 | use warnings FATAL => 'all'; |
5 | use HTML::Zoom::CodeStream; |
6 | |
7 | sub new { bless({}, shift) } |
8 | |
9 | sub _stream_from_code { |
10 | HTML::Zoom::CodeStream->new({ code => $_[1] }) |
11 | } |
12 | |
13 | sub _stream_from_array { |
14 | shift; # lose $self |
15 | HTML::Zoom::CodeStream->from_array(@_) |
16 | } |
17 | |
3cdbc13f |
18 | sub _stream_from_proto { |
19 | my ($self, $proto) = @_; |
20 | my $ref = ref $proto; |
21 | if (not $ref) { |
22 | return $self->_stream_from_array({ type => 'TEXT', raw => $proto }); |
23 | } elsif ($ref eq 'ARRAY') { |
24 | return $self->_stream_from_array(@$proto); |
25 | } elsif ($ref eq 'CODE') { |
26 | return $proto->(); |
27 | } elsif ($ref eq 'SCALAR') { |
28 | require HTML::Zoom::Parser::BuiltIn; |
29 | return HTML::Zoom::Parser::BuiltIn->html_to_stream($$proto); |
30 | } |
626752d4 |
31 | die "Don't know how to turn $proto (ref $ref) into a stream"; |
3cdbc13f |
32 | } |
33 | |
456a815d |
34 | sub _stream_concat { |
3cdbc13f |
35 | shift->_stream_from_array(@_)->flatten; |
456a815d |
36 | } |
37 | |
38 | sub set_attribute { |
39 | my ($self, $args) = @_; |
40 | my ($name, $value) = @{$args}{qw(name value)}; |
41 | sub { |
8f962884 |
42 | my $a = (my $evt = $_[0])->{attrs}; |
456a815d |
43 | my $e = exists $a->{$name}; |
44 | +{ %$evt, raw => undef, raw_attrs => undef, |
45 | attrs => { %$a, $name => $value }, |
46 | ($e # add to name list if not present |
47 | ? () |
48 | : (attr_names => [ @{$evt->{attr_names}}, $name ])) |
49 | } |
50 | }; |
51 | } |
52 | |
53 | sub add_attribute { |
54 | my ($self, $args) = @_; |
55 | my ($name, $value) = @{$args}{qw(name value)}; |
56 | sub { |
8f962884 |
57 | my $a = (my $evt = $_[0])->{attrs}; |
456a815d |
58 | my $e = exists $a->{$name}; |
59 | +{ %$evt, raw => undef, raw_attrs => undef, |
60 | attrs => { |
61 | %$a, |
62 | $name => join(' ', ($e ? $a->{$name} : ()), $value) |
63 | }, |
64 | ($e # add to name list if not present |
65 | ? () |
66 | : (attr_names => [ @{$evt->{attr_names}}, $name ])) |
67 | } |
68 | }; |
69 | } |
70 | |
71 | sub remove_attribute { |
72 | my ($self, $args) = @_; |
73 | my $name = $args->{name}; |
74 | sub { |
8f962884 |
75 | my $a = (my $evt = $_[0])->{attrs}; |
456a815d |
76 | return $evt unless exists $a->{$name}; |
77 | $a = { %$a }; delete $a->{$name}; |
78 | +{ %$evt, raw => undef, raw_attrs => undef, |
79 | attrs => $a, |
80 | attr_names => [ grep $_ ne $name, @{$evt->{attr_names}} ] |
81 | } |
82 | }; |
83 | } |
84 | |
76cecb10 |
85 | sub collect { |
86 | my ($self, $options) = @_; |
865bb5d2 |
87 | my ($into, $passthrough, $content) = @{$options}{qw(into passthrough content)}; |
76cecb10 |
88 | sub { |
89 | my ($evt, $stream) = @_; |
b4d044eb |
90 | # We wipe the contents of @$into here so that other actions depending |
91 | # on this (such as a repeater) can be invoked multiple times easily. |
92 | # I -suspect- it's better for that state reset to be managed here; if it |
93 | # ever becomes painful the decision should be revisited |
94 | if ($into) { |
865bb5d2 |
95 | @$into = $content ? () : ($evt); |
b4d044eb |
96 | } |
76cecb10 |
97 | if ($evt->{is_in_place_close}) { |
865bb5d2 |
98 | return $evt if $passthrough || $content; |
76cecb10 |
99 | return; |
100 | } |
101 | my $name = $evt->{name}; |
102 | my $depth = 1; |
865bb5d2 |
103 | my $_next = $content ? 'peek' : 'next'; |
76cecb10 |
104 | my $collector = $self->_stream_from_code(sub { |
105 | return unless $stream; |
106 | while (my ($evt) = $stream->$_next) { |
107 | $depth++ if ($evt->{type} eq 'OPEN'); |
108 | $depth-- if ($evt->{type} eq 'CLOSE'); |
109 | unless ($depth) { |
110 | undef $stream; |
865bb5d2 |
111 | return if $content; |
76cecb10 |
112 | push(@$into, $evt) if $into; |
113 | return $evt if $passthrough; |
114 | return; |
115 | } |
116 | push(@$into, $evt) if $into; |
865bb5d2 |
117 | $stream->next if $content; |
76cecb10 |
118 | return $evt if $passthrough; |
119 | } |
120 | die "Never saw closing </${name}> before end of source"; |
121 | }); |
865bb5d2 |
122 | return ($passthrough||$content) ? [ $evt, $collector ] : $collector; |
76cecb10 |
123 | }; |
124 | } |
125 | |
865bb5d2 |
126 | sub collect_content { |
127 | my ($self, $options) = @_; |
128 | $self->collect({ %{$options||{}}, content => 1 }) |
129 | } |
130 | |
456a815d |
131 | sub add_before { |
132 | my ($self, $events) = @_; |
8f962884 |
133 | sub { return $self->_stream_from_array(@$events, $_[0]) }; |
456a815d |
134 | } |
135 | |
136 | sub add_after { |
137 | my ($self, $events) = @_; |
b616863d |
138 | my $coll_proto = $self->collect({ passthrough => 1 }); |
456a815d |
139 | sub { |
8f962884 |
140 | my ($evt) = @_; |
456a815d |
141 | my $emit = $self->_stream_from_array(@$events); |
b616863d |
142 | my $coll = &$coll_proto; |
995bc8be |
143 | return ref($coll) eq 'HASH' # single event, no collect |
144 | ? [ $coll, $emit ] |
145 | : [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ]; |
456a815d |
146 | }; |
8f962884 |
147 | } |
456a815d |
148 | |
865bb5d2 |
149 | sub prepend_content { |
456a815d |
150 | my ($self, $events) = @_; |
151 | sub { |
8f962884 |
152 | my ($evt) = @_; |
456a815d |
153 | if ($evt->{is_in_place_close}) { |
154 | $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)}; |
155 | return [ $evt, $self->_stream_from_array( |
156 | @$events, { type => 'CLOSE', name => $evt->{name} } |
157 | ) ]; |
158 | } |
159 | return $self->_stream_from_array($evt, @$events); |
160 | }; |
161 | } |
162 | |
865bb5d2 |
163 | sub append_content { |
8f962884 |
164 | my ($self, $events) = @_; |
865bb5d2 |
165 | my $coll_proto = $self->collect({ passthrough => 1, content => 1 }); |
8f962884 |
166 | sub { |
167 | my ($evt) = @_; |
168 | if ($evt->{is_in_place_close}) { |
169 | $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)}; |
170 | return [ $evt, $self->_stream_from_array( |
171 | @$events, { type => 'CLOSE', name => $evt->{name} } |
172 | ) ]; |
173 | } |
b616863d |
174 | my $coll = &$coll_proto; |
8f962884 |
175 | my $emit = $self->_stream_from_array(@$events); |
176 | return [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ]; |
177 | }; |
178 | } |
179 | |
456a815d |
180 | sub replace { |
3cdbc13f |
181 | my ($self, $replace_with, $options) = @_; |
b616863d |
182 | my $coll_proto = $self->collect($options); |
456a815d |
183 | sub { |
184 | my ($evt, $stream) = @_; |
3cdbc13f |
185 | my $emit = $self->_stream_from_proto($replace_with); |
b616863d |
186 | my $coll = &$coll_proto; |
451b3b30 |
187 | # For a straightforward replace operation we can, in fact, do the emit |
188 | # -before- the collect, and my first cut did so. However in order to |
189 | # use the captured content in generating the new content, we need |
190 | # the collect stage to happen first - and it seems highly unlikely |
191 | # that in normal operation the collect phase will take long enough |
192 | # for the difference to be noticeable |
11cc25dd |
193 | return |
194 | ($coll |
195 | ? (ref $coll eq 'ARRAY' |
451b3b30 |
196 | ? [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ] |
197 | : $self->_stream_concat($coll, $emit) |
11cc25dd |
198 | ) |
199 | : $emit |
200 | ); |
456a815d |
201 | }; |
202 | } |
203 | |
865bb5d2 |
204 | sub replace_content { |
205 | my ($self, $replace_with, $options) = @_; |
206 | $self->replace($replace_with, { %{$options||{}}, content => 1 }) |
207 | } |
208 | |
3cdbc13f |
209 | sub repeat { |
210 | my ($self, $repeat_for, $options) = @_; |
211 | $options->{into} = \my @into; |
212 | my $map_repeat = sub { |
213 | local $_ = $self->_stream_from_array(@into); |
214 | $_[0]->($_) |
215 | }; |
216 | my $repeater = sub { |
217 | $self->_stream_from_proto($repeat_for) |
218 | ->map($map_repeat) |
219 | ->flatten |
220 | }; |
221 | $self->replace($repeater, $options); |
222 | } |
223 | |
865bb5d2 |
224 | sub repeat_content { |
225 | my ($self, $repeat_for, $options) = @_; |
226 | $self->repeat($repeat_for, { %{$options||{}}, content => 1 }) |
227 | } |
228 | |
456a815d |
229 | 1; |