finish converting from MatchWithoutFilter to TransformBuilder
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / FilterBuilder.pm
CommitLineData
456a815d 1package HTML::Zoom::FilterBuilder;
2
456a815d 3use strict;
4use warnings FATAL => 'all';
d80786d0 5use base qw(HTML::Zoom::SubObject);
456a815d 6use HTML::Zoom::CodeStream;
7
456a815d 8sub _stream_from_code {
d80786d0 9 shift->_zconfig->stream_utils->stream_from_code(@_)
456a815d 10}
11
12sub _stream_from_array {
d80786d0 13 shift->_zconfig->stream_utils->stream_from_array(@_)
456a815d 14}
15
3cdbc13f 16sub _stream_from_proto {
d80786d0 17 shift->_zconfig->stream_utils->stream_from_proto(@_)
3cdbc13f 18}
19
456a815d 20sub _stream_concat {
d80786d0 21 shift->_zconfig->stream_utils->stream_concat(@_)
456a815d 22}
23
6d0f20a6 24sub _flatten_stream_of_streams {
25 shift->_zconfig->stream_utils->flatten_stream_of_streams(@_)
26}
27
456a815d 28sub set_attribute {
1c4455ae 29 my $self = shift;
30 my ($name, $value) = $self->_parse_attribute_args(@_);
456a815d 31 sub {
8f962884 32 my $a = (my $evt = $_[0])->{attrs};
456a815d 33 my $e = exists $a->{$name};
34 +{ %$evt, raw => undef, raw_attrs => undef,
35 attrs => { %$a, $name => $value },
36 ($e # add to name list if not present
37 ? ()
38 : (attr_names => [ @{$evt->{attr_names}}, $name ]))
39 }
40 };
41}
42
1c4455ae 43sub _parse_attribute_args {
44 my $self = shift;
45 # allow ->add_attribute(name => 'value')
46 # or ->add_attribute({ name => 'name', value => 'value' })
47 my ($name, $value) = @_ > 1 ? @_ : @{$_[0]}{qw(name value)};
48 return ($name, $self->_zconfig->parser->html_escape($value));
49}
50
456a815d 51sub add_attribute {
1c4455ae 52 my $self = shift;
53 my ($name, $value) = $self->_parse_attribute_args(@_);
456a815d 54 sub {
8f962884 55 my $a = (my $evt = $_[0])->{attrs};
456a815d 56 my $e = exists $a->{$name};
57 +{ %$evt, raw => undef, raw_attrs => undef,
58 attrs => {
59 %$a,
60 $name => join(' ', ($e ? $a->{$name} : ()), $value)
61 },
62 ($e # add to name list if not present
63 ? ()
64 : (attr_names => [ @{$evt->{attr_names}}, $name ]))
65 }
66 };
67}
68
69sub remove_attribute {
70 my ($self, $args) = @_;
1c4455ae 71 my $name = (ref($args) eq 'HASH') ? $args->{name} : $args;
456a815d 72 sub {
8f962884 73 my $a = (my $evt = $_[0])->{attrs};
456a815d 74 return $evt unless exists $a->{$name};
75 $a = { %$a }; delete $a->{$name};
76 +{ %$evt, raw => undef, raw_attrs => undef,
77 attrs => $a,
78 attr_names => [ grep $_ ne $name, @{$evt->{attr_names}} ]
79 }
80 };
81}
82
76cecb10 83sub collect {
84 my ($self, $options) = @_;
1c4455ae 85 my ($into, $passthrough, $content, $filter, $flush_before) =
86 @{$options}{qw(into passthrough content filter flush_before)};
76cecb10 87 sub {
88 my ($evt, $stream) = @_;
b4d044eb 89 # We wipe the contents of @$into here so that other actions depending
90 # on this (such as a repeater) can be invoked multiple times easily.
91 # I -suspect- it's better for that state reset to be managed here; if it
92 # ever becomes painful the decision should be revisited
93 if ($into) {
865bb5d2 94 @$into = $content ? () : ($evt);
b4d044eb 95 }
76cecb10 96 if ($evt->{is_in_place_close}) {
865bb5d2 97 return $evt if $passthrough || $content;
76cecb10 98 return;
99 }
100 my $name = $evt->{name};
101 my $depth = 1;
865bb5d2 102 my $_next = $content ? 'peek' : 'next';
d80786d0 103 $stream = do { local $_ = $stream; $filter->($stream) } if $filter;
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 });
1c4455ae 122 if ($flush_before) {
123 if ($passthrough||$content) {
124 $evt = { %$evt, flush => 1 };
125 } else {
126 $evt = { type => 'EMPTY', flush => 1 };
127 }
128 }
129 return ($passthrough||$content||$flush_before)
130 ? [ $evt, $collector ]
131 : $collector;
76cecb10 132 };
133}
134
865bb5d2 135sub collect_content {
136 my ($self, $options) = @_;
137 $self->collect({ %{$options||{}}, content => 1 })
138}
139
456a815d 140sub add_before {
141 my ($self, $events) = @_;
8f962884 142 sub { return $self->_stream_from_array(@$events, $_[0]) };
456a815d 143}
144
145sub add_after {
146 my ($self, $events) = @_;
b616863d 147 my $coll_proto = $self->collect({ passthrough => 1 });
456a815d 148 sub {
8f962884 149 my ($evt) = @_;
456a815d 150 my $emit = $self->_stream_from_array(@$events);
b616863d 151 my $coll = &$coll_proto;
995bc8be 152 return ref($coll) eq 'HASH' # single event, no collect
153 ? [ $coll, $emit ]
154 : [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
456a815d 155 };
8f962884 156}
456a815d 157
865bb5d2 158sub prepend_content {
456a815d 159 my ($self, $events) = @_;
160 sub {
8f962884 161 my ($evt) = @_;
456a815d 162 if ($evt->{is_in_place_close}) {
163 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
164 return [ $evt, $self->_stream_from_array(
165 @$events, { type => 'CLOSE', name => $evt->{name} }
166 ) ];
167 }
168 return $self->_stream_from_array($evt, @$events);
169 };
170}
171
865bb5d2 172sub append_content {
8f962884 173 my ($self, $events) = @_;
865bb5d2 174 my $coll_proto = $self->collect({ passthrough => 1, content => 1 });
8f962884 175 sub {
176 my ($evt) = @_;
177 if ($evt->{is_in_place_close}) {
178 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
179 return [ $evt, $self->_stream_from_array(
180 @$events, { type => 'CLOSE', name => $evt->{name} }
181 ) ];
182 }
b616863d 183 my $coll = &$coll_proto;
8f962884 184 my $emit = $self->_stream_from_array(@$events);
185 return [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
186 };
187}
188
456a815d 189sub replace {
3cdbc13f 190 my ($self, $replace_with, $options) = @_;
b616863d 191 my $coll_proto = $self->collect($options);
456a815d 192 sub {
193 my ($evt, $stream) = @_;
3cdbc13f 194 my $emit = $self->_stream_from_proto($replace_with);
b616863d 195 my $coll = &$coll_proto;
451b3b30 196 # For a straightforward replace operation we can, in fact, do the emit
197 # -before- the collect, and my first cut did so. However in order to
198 # use the captured content in generating the new content, we need
199 # the collect stage to happen first - and it seems highly unlikely
200 # that in normal operation the collect phase will take long enough
201 # for the difference to be noticeable
11cc25dd 202 return
203 ($coll
204 ? (ref $coll eq 'ARRAY'
451b3b30 205 ? [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ]
206 : $self->_stream_concat($coll, $emit)
11cc25dd 207 )
208 : $emit
209 );
456a815d 210 };
211}
212
865bb5d2 213sub replace_content {
214 my ($self, $replace_with, $options) = @_;
215 $self->replace($replace_with, { %{$options||{}}, content => 1 })
216}
217
3cdbc13f 218sub repeat {
219 my ($self, $repeat_for, $options) = @_;
220 $options->{into} = \my @into;
f8ed299b 221 my @between;
222 my $repeat_between = delete $options->{repeat_between};
223 if ($repeat_between) {
f8ed299b 224 $options->{filter} = sub {
d80786d0 225 $_->select($repeat_between)->collect({ into => \@between })
f8ed299b 226 };
227 }
3cdbc13f 228 my $repeater = sub {
f8ed299b 229 my $s = $self->_stream_from_proto($repeat_for);
230 # We have to test $repeat_between not @between here because
231 # at the point we're constructing our return stream @between
232 # hasn't been populated yet - but we can test @between in the
233 # map routine because it has been by then and that saves us doing
234 # the extra stream construction if we don't need it.
6d0f20a6 235 $self->_flatten_stream_of_streams(do {
236 if ($repeat_between) {
237 $s->map(sub {
238 local $_ = $self->_stream_from_array(@into);
239 (@between && $s->peek)
240 ? $self->_stream_concat(
241 $_[0]->($_), $self->_stream_from_array(@between)
242 )
243 : $_[0]->($_)
244 })
245 } else {
246 $s->map(sub {
247 local $_ = $self->_stream_from_array(@into);
248 $_[0]->($_)
f8ed299b 249 })
6d0f20a6 250 }
251 })
3cdbc13f 252 };
253 $self->replace($repeater, $options);
254}
255
865bb5d2 256sub repeat_content {
257 my ($self, $repeat_for, $options) = @_;
258 $self->repeat($repeat_for, { %{$options||{}}, content => 1 })
259}
260
456a815d 2611;