move collect method to a more sensible place in FilterBuilder
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / FilterBuilder.pm
CommitLineData
456a815d 1package HTML::Zoom::FilterBuilder;
2
3use Devel::Dwarn;
4
5use strict;
6use warnings FATAL => 'all';
7use HTML::Zoom::CodeStream;
8
9sub new { bless({}, shift) }
10
11sub _stream_from_code {
12 HTML::Zoom::CodeStream->new({ code => $_[1] })
13}
14
15sub _stream_from_array {
16 shift; # lose $self
17 HTML::Zoom::CodeStream->from_array(@_)
18}
19
20sub _stream_concat {
21 shift; # lose $self
22 my @streams = @_;
23 my $cur_stream = shift(@streams) or die "No streams passed";
24 HTML::Zoom::CodeStream->new({
25 code => sub {
26 return unless $cur_stream;
27 my $evt;
28 until (($evt) = $cur_stream->next) {
29 return unless $cur_stream = shift(@streams);
30 }
31 return $evt;
32 }
33 });
34}
35
36sub set_attribute {
37 my ($self, $args) = @_;
38 my ($name, $value) = @{$args}{qw(name value)};
39 sub {
8f962884 40 my $a = (my $evt = $_[0])->{attrs};
456a815d 41 my $e = exists $a->{$name};
42 +{ %$evt, raw => undef, raw_attrs => undef,
43 attrs => { %$a, $name => $value },
44 ($e # add to name list if not present
45 ? ()
46 : (attr_names => [ @{$evt->{attr_names}}, $name ]))
47 }
48 };
49}
50
51sub add_attribute {
52 my ($self, $args) = @_;
53 my ($name, $value) = @{$args}{qw(name value)};
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) = @_;
71 my $name = $args->{name};
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) = @_;
85 my ($into, $passthrough, $inside) = @{$options}{qw(into passthrough inside)};
86 sub {
87 my ($evt, $stream) = @_;
88 push(@$into, $evt) if $into && !$inside;
89 if ($evt->{is_in_place_close}) {
90 return $evt if $passthrough || $inside;
91 return;
92 }
93 my $name = $evt->{name};
94 my $depth = 1;
95 my $_next = $inside ? 'peek' : 'next';
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 $inside;
104 push(@$into, $evt) if $into;
105 return $evt if $passthrough;
106 return;
107 }
108 push(@$into, $evt) if $into;
109 $stream->next if $inside;
110 return $evt if $passthrough;
111 }
112 die "Never saw closing </${name}> before end of source";
113 });
114 return ($passthrough||$inside) ? [ $evt, $collector ] : $collector;
115 };
116}
117
456a815d 118sub add_before {
119 my ($self, $events) = @_;
8f962884 120 sub { return $self->_stream_from_array(@$events, $_[0]) };
456a815d 121}
122
123sub add_after {
124 my ($self, $events) = @_;
125 sub {
8f962884 126 my ($evt) = @_;
456a815d 127 my $emit = $self->_stream_from_array(@$events);
cac9d0a5 128 my $coll = $self->collect({ passthrough => 1 })->(@_);
995bc8be 129 return ref($coll) eq 'HASH' # single event, no collect
130 ? [ $coll, $emit ]
131 : [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
456a815d 132 };
8f962884 133}
456a815d 134
135sub prepend_inside {
136 my ($self, $events) = @_;
137 sub {
8f962884 138 my ($evt) = @_;
456a815d 139 if ($evt->{is_in_place_close}) {
140 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
141 return [ $evt, $self->_stream_from_array(
142 @$events, { type => 'CLOSE', name => $evt->{name} }
143 ) ];
144 }
145 return $self->_stream_from_array($evt, @$events);
146 };
147}
148
8f962884 149sub append_inside {
150 my ($self, $events) = @_;
151 sub {
152 my ($evt) = @_;
153 if ($evt->{is_in_place_close}) {
154 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
155 return [ $evt, $self->_stream_from_array(
156 @$events, { type => 'CLOSE', name => $evt->{name} }
157 ) ];
158 }
159 my $coll = $self->collect({ passthrough => 1, inside => 1 })->(@_);
160 my $emit = $self->_stream_from_array(@$events);
161 return [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
162 };
163}
164
456a815d 165sub replace {
11cc25dd 166 my ($self, $events, $options) = @_;
456a815d 167 sub {
168 my ($evt, $stream) = @_;
169 my $emit = $self->_stream_from_array(@$events);
11cc25dd 170 my $coll = $self->collect($options)->(@_);
171 return
172 ($coll
173 ? (ref $coll eq 'ARRAY'
174 ? [ $coll->[0], $self->_stream_concat($emit, $coll->[1]) ]
175 : $self->_stream_concat($emit, $coll)
176 )
177 : $emit
178 );
456a815d 179 };
180}
181
456a815d 1821;