1 package HTML::Zoom::FilterBuilder;
4 use warnings FATAL => 'all';
5 use HTML::Zoom::CodeStream;
7 sub new { bless({}, shift) }
9 sub _stream_from_code {
10 HTML::Zoom::CodeStream->new({ code => $_[1] })
13 sub _stream_from_array {
15 HTML::Zoom::CodeStream->from_array(@_)
18 sub _stream_from_proto {
19 my ($self, $proto) = @_;
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') {
27 } elsif ($ref eq 'SCALAR') {
28 require HTML::Zoom::Parser::BuiltIn;
29 return HTML::Zoom::Parser::BuiltIn->html_to_stream($$proto);
31 die "Don't know how to turn $proto (ref $ref) into a stream";
35 shift->_stream_from_array(@_)->flatten;
39 my ($self, $args) = @_;
40 my ($name, $value) = @{$args}{qw(name value)};
42 my $a = (my $evt = $_[0])->{attrs};
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
48 : (attr_names => [ @{$evt->{attr_names}}, $name ]))
54 my ($self, $args) = @_;
55 my ($name, $value) = @{$args}{qw(name value)};
57 my $a = (my $evt = $_[0])->{attrs};
58 my $e = exists $a->{$name};
59 +{ %$evt, raw => undef, raw_attrs => undef,
62 $name => join(' ', ($e ? $a->{$name} : ()), $value)
64 ($e # add to name list if not present
66 : (attr_names => [ @{$evt->{attr_names}}, $name ]))
71 sub remove_attribute {
72 my ($self, $args) = @_;
73 my $name = $args->{name};
75 my $a = (my $evt = $_[0])->{attrs};
76 return $evt unless exists $a->{$name};
77 $a = { %$a }; delete $a->{$name};
78 +{ %$evt, raw => undef, raw_attrs => undef,
80 attr_names => [ grep $_ ne $name, @{$evt->{attr_names}} ]
86 my ($self, $options) = @_;
87 my ($into, $passthrough, $inside) = @{$options}{qw(into passthrough inside)};
89 my ($evt, $stream) = @_;
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
95 @$into = $inside ? () : ($evt);
97 if ($evt->{is_in_place_close}) {
98 return $evt if $passthrough || $inside;
101 my $name = $evt->{name};
103 my $_next = $inside ? 'peek' : 'next';
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');
112 push(@$into, $evt) if $into;
113 return $evt if $passthrough;
116 push(@$into, $evt) if $into;
117 $stream->next if $inside;
118 return $evt if $passthrough;
120 die "Never saw closing </${name}> before end of source";
122 return ($passthrough||$inside) ? [ $evt, $collector ] : $collector;
127 my ($self, $events) = @_;
128 sub { return $self->_stream_from_array(@$events, $_[0]) };
132 my ($self, $events) = @_;
133 my $coll_proto = $self->collect({ passthrough => 1 });
136 my $emit = $self->_stream_from_array(@$events);
137 my $coll = &$coll_proto;
138 return ref($coll) eq 'HASH' # single event, no collect
140 : [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
145 my ($self, $events) = @_;
148 if ($evt->{is_in_place_close}) {
149 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
150 return [ $evt, $self->_stream_from_array(
151 @$events, { type => 'CLOSE', name => $evt->{name} }
154 return $self->_stream_from_array($evt, @$events);
159 my ($self, $events) = @_;
160 my $coll_proto = $self->collect({ passthrough => 1, inside => 1 });
163 if ($evt->{is_in_place_close}) {
164 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
165 return [ $evt, $self->_stream_from_array(
166 @$events, { type => 'CLOSE', name => $evt->{name} }
169 my $coll = &$coll_proto;
170 my $emit = $self->_stream_from_array(@$events);
171 return [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
176 my ($self, $replace_with, $options) = @_;
177 my $coll_proto = $self->collect($options);
179 my ($evt, $stream) = @_;
180 my $emit = $self->_stream_from_proto($replace_with);
181 my $coll = &$coll_proto;
182 # For a straightforward replace operation we can, in fact, do the emit
183 # -before- the collect, and my first cut did so. However in order to
184 # use the captured content in generating the new content, we need
185 # the collect stage to happen first - and it seems highly unlikely
186 # that in normal operation the collect phase will take long enough
187 # for the difference to be noticeable
190 ? (ref $coll eq 'ARRAY'
191 ? [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ]
192 : $self->_stream_concat($coll, $emit)
200 my ($self, $repeat_for, $options) = @_;
201 $options->{into} = \my @into;
202 my $map_repeat = sub {
203 local $_ = $self->_stream_from_array(@into);
207 $self->_stream_from_proto($repeat_for)
211 $self->replace($repeater, $options);