pre-filter for collect/repeat/replace
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / FilterBuilder.pm
CommitLineData
456a815d 1package HTML::Zoom::FilterBuilder;
2
456a815d 3use strict;
4use warnings FATAL => 'all';
5use HTML::Zoom::CodeStream;
6
7sub new { bless({}, shift) }
8
9sub _stream_from_code {
10 HTML::Zoom::CodeStream->new({ code => $_[1] })
11}
12
13sub _stream_from_array {
14 shift; # lose $self
15 HTML::Zoom::CodeStream->from_array(@_)
16}
17
3cdbc13f 18sub _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 34sub _stream_concat {
3cdbc13f 35 shift->_stream_from_array(@_)->flatten;
456a815d 36}
37
38sub 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
53sub 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
71sub 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 85sub collect {
86 my ($self, $options) = @_;
dae33531 87 my ($into, $passthrough, $content, $filter) =
88 @{$options}{qw(into passthrough content filter)};
76cecb10 89 sub {
90 my ($evt, $stream) = @_;
b4d044eb 91 # We wipe the contents of @$into here so that other actions depending
92 # on this (such as a repeater) can be invoked multiple times easily.
93 # I -suspect- it's better for that state reset to be managed here; if it
94 # ever becomes painful the decision should be revisited
95 if ($into) {
865bb5d2 96 @$into = $content ? () : ($evt);
b4d044eb 97 }
76cecb10 98 if ($evt->{is_in_place_close}) {
865bb5d2 99 return $evt if $passthrough || $content;
76cecb10 100 return;
101 }
102 my $name = $evt->{name};
103 my $depth = 1;
865bb5d2 104 my $_next = $content ? 'peek' : 'next';
dae33531 105 $stream = $filter->($stream) if $filter;
76cecb10 106 my $collector = $self->_stream_from_code(sub {
107 return unless $stream;
108 while (my ($evt) = $stream->$_next) {
109 $depth++ if ($evt->{type} eq 'OPEN');
110 $depth-- if ($evt->{type} eq 'CLOSE');
111 unless ($depth) {
112 undef $stream;
865bb5d2 113 return if $content;
76cecb10 114 push(@$into, $evt) if $into;
115 return $evt if $passthrough;
116 return;
117 }
118 push(@$into, $evt) if $into;
865bb5d2 119 $stream->next if $content;
76cecb10 120 return $evt if $passthrough;
121 }
122 die "Never saw closing </${name}> before end of source";
123 });
865bb5d2 124 return ($passthrough||$content) ? [ $evt, $collector ] : $collector;
76cecb10 125 };
126}
127
865bb5d2 128sub collect_content {
129 my ($self, $options) = @_;
130 $self->collect({ %{$options||{}}, content => 1 })
131}
132
456a815d 133sub add_before {
134 my ($self, $events) = @_;
8f962884 135 sub { return $self->_stream_from_array(@$events, $_[0]) };
456a815d 136}
137
138sub add_after {
139 my ($self, $events) = @_;
b616863d 140 my $coll_proto = $self->collect({ passthrough => 1 });
456a815d 141 sub {
8f962884 142 my ($evt) = @_;
456a815d 143 my $emit = $self->_stream_from_array(@$events);
b616863d 144 my $coll = &$coll_proto;
995bc8be 145 return ref($coll) eq 'HASH' # single event, no collect
146 ? [ $coll, $emit ]
147 : [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
456a815d 148 };
8f962884 149}
456a815d 150
865bb5d2 151sub prepend_content {
456a815d 152 my ($self, $events) = @_;
153 sub {
8f962884 154 my ($evt) = @_;
456a815d 155 if ($evt->{is_in_place_close}) {
156 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
157 return [ $evt, $self->_stream_from_array(
158 @$events, { type => 'CLOSE', name => $evt->{name} }
159 ) ];
160 }
161 return $self->_stream_from_array($evt, @$events);
162 };
163}
164
865bb5d2 165sub append_content {
8f962884 166 my ($self, $events) = @_;
865bb5d2 167 my $coll_proto = $self->collect({ passthrough => 1, content => 1 });
8f962884 168 sub {
169 my ($evt) = @_;
170 if ($evt->{is_in_place_close}) {
171 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
172 return [ $evt, $self->_stream_from_array(
173 @$events, { type => 'CLOSE', name => $evt->{name} }
174 ) ];
175 }
b616863d 176 my $coll = &$coll_proto;
8f962884 177 my $emit = $self->_stream_from_array(@$events);
178 return [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
179 };
180}
181
456a815d 182sub replace {
3cdbc13f 183 my ($self, $replace_with, $options) = @_;
b616863d 184 my $coll_proto = $self->collect($options);
456a815d 185 sub {
186 my ($evt, $stream) = @_;
3cdbc13f 187 my $emit = $self->_stream_from_proto($replace_with);
b616863d 188 my $coll = &$coll_proto;
451b3b30 189 # For a straightforward replace operation we can, in fact, do the emit
190 # -before- the collect, and my first cut did so. However in order to
191 # use the captured content in generating the new content, we need
192 # the collect stage to happen first - and it seems highly unlikely
193 # that in normal operation the collect phase will take long enough
194 # for the difference to be noticeable
11cc25dd 195 return
196 ($coll
197 ? (ref $coll eq 'ARRAY'
451b3b30 198 ? [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ]
199 : $self->_stream_concat($coll, $emit)
11cc25dd 200 )
201 : $emit
202 );
456a815d 203 };
204}
205
865bb5d2 206sub replace_content {
207 my ($self, $replace_with, $options) = @_;
208 $self->replace($replace_with, { %{$options||{}}, content => 1 })
209}
210
3cdbc13f 211sub repeat {
212 my ($self, $repeat_for, $options) = @_;
213 $options->{into} = \my @into;
214 my $map_repeat = sub {
215 local $_ = $self->_stream_from_array(@into);
216 $_[0]->($_)
217 };
218 my $repeater = sub {
219 $self->_stream_from_proto($repeat_for)
220 ->map($map_repeat)
221 ->flatten
222 };
223 $self->replace($repeater, $options);
224}
225
865bb5d2 226sub repeat_content {
227 my ($self, $repeat_for, $options) = @_;
228 $self->repeat($repeat_for, { %{$options||{}}, content => 1 })
229}
230
456a815d 2311;