1 package HTML::Zoom::StreamUtils;
4 use base qw(HTML::Zoom::SubObject);
8 use HTML::Zoom::CodeStream;
9 use HTML::Zoom::FilterStream;
10 use HTML::Zoom::ArrayStream;
12 sub stream_from_code {
13 my ($self, $code) = @_;
14 HTML::Zoom::CodeStream->new({
16 zconfig => $self->_zconfig,
20 sub stream_from_array {
23 HTML::Zoom::ArrayStream->new({
25 zconfig => $self->_zconfig,
30 shift->stream_from_array(@_)->flatten;
33 sub stream_from_proto {
34 my ($self, $proto) = @_;
37 return $self->stream_from_array({
39 raw => $self->_zconfig->parser->html_escape($proto)
41 } elsif ($ref eq 'ARRAY') {
42 return $self->stream_from_array(@$proto);
43 } elsif ($ref eq 'CODE') {
45 } elsif ($ref eq 'SCALAR') {
46 return $self->_zconfig->parser->html_to_stream($$proto);
47 } elsif (Scalar::Util::blessed($proto) && $proto->can('to_stream')) {
48 my $stream = $proto->to_stream;
49 return $self->stream_from_code(sub { $stream->next });
51 die "Don't know how to turn $proto (ref $ref) into a stream";
54 sub wrap_with_filter {
55 my ($self, $stream, $match, $filter) = @_;
56 HTML::Zoom::FilterStream->new({
60 zconfig => $self->_zconfig,
67 while (my ($evt) = $stream->next) { push @array, $evt }
71 sub flatten_stream_of_streams {
72 my ($self, $source_stream) = @_;
74 HTML::Zoom::CodeStream->new({
76 return unless $source_stream;
78 until (($next) = ($cur_stream ? $cur_stream->next : ())) {
79 unless (($cur_stream) = $source_stream->next) {
80 undef $source_stream; return;