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