1 package HTML::Zoom::StreamUtils;
4 use base qw(HTML::Zoom::SubObject);
7 use HTML::Zoom::CodeStream;
8 use HTML::Zoom::FilterStream;
9 use HTML::Zoom::ArrayStream;
11 sub stream_from_code {
12 my ($self, $code) = @_;
13 HTML::Zoom::CodeStream->new({
15 zconfig => $self->_zconfig,
19 sub stream_from_array {
22 HTML::Zoom::ArrayStream->new({
24 zconfig => $self->_zconfig,
29 shift->stream_from_array(@_)->flatten;
32 sub stream_from_proto {
33 my ($self, $proto) = @_;
36 return $self->stream_from_array({
38 raw => $self->_zconfig->parser->html_escape($proto)
40 } elsif ($ref eq 'ARRAY') {
41 return $self->stream_from_array(@$proto);
42 } elsif ($ref eq 'CODE') {
44 } elsif ($ref eq 'SCALAR') {
45 return $self->_zconfig->parser->html_to_stream($$proto);
46 } elsif (Scalar::Util::blessed($proto) && $proto->can('to_stream')) {
47 my $stream = $proto->to_stream;
48 return $self->stream_from_code(sub { $stream->next });
50 die "Don't know how to turn $proto (ref $ref) into a stream";
53 sub wrap_with_filter {
54 my ($self, $stream, $match, $filter) = @_;
55 HTML::Zoom::FilterStream->new({
59 zconfig => $self->_zconfig,
66 while (my ($evt) = $stream->next) { push @array, $evt }
70 sub flatten_stream_of_streams {
71 my ($self, $source_stream) = @_;
73 HTML::Zoom::CodeStream->new({
75 return unless $source_stream;
77 until (($next) = ($cur_stream ? $cur_stream->next : ())) {
78 unless (($cur_stream) = $source_stream->next) {
79 undef $source_stream; return;