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