| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package HTML::Zoom::StreamUtils; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 13 |  |  | 13 |  | 59 | use strictures 1; | 
|  | 13 |  |  |  |  | 80 |  | 
|  | 13 |  |  |  |  | 324 |  | 
| 4 | 13 |  |  | 13 |  | 972 | use base qw(HTML::Zoom::SubObject); | 
|  | 13 |  |  |  |  | 22 |  | 
|  | 13 |  |  |  |  | 1138 |  | 
| 5 | 13 |  |  | 13 |  | 70 | use Scalar::Util (); | 
|  | 13 |  |  |  |  | 17 |  | 
|  | 13 |  |  |  |  | 208 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 13 |  |  | 13 |  | 3251 | use HTML::Zoom::CodeStream; | 
|  | 13 |  |  |  |  | 32 |  | 
|  | 13 |  |  |  |  | 292 |  | 
| 8 | 13 |  |  | 13 |  | 65 | use HTML::Zoom::FilterStream; | 
|  | 13 |  |  |  |  | 19 |  | 
|  | 13 |  |  |  |  | 267 |  | 
| 9 | 13 |  |  | 13 |  | 5337 | use HTML::Zoom::ArrayStream; | 
|  | 13 |  |  |  |  | 26 |  | 
|  | 13 |  |  |  |  | 6349 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub stream_from_code { | 
| 12 | 134 |  |  | 134 | 0 | 139 | my ($self, $code) = @_; | 
| 13 | 134 |  |  |  |  | 300 | HTML::Zoom::CodeStream->new({ | 
| 14 |  |  |  |  |  |  | code => $code, | 
| 15 |  |  |  |  |  |  | zconfig => $self->_zconfig, | 
| 16 |  |  |  |  |  |  | }) | 
| 17 |  |  |  |  |  |  | } | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub stream_from_array { | 
| 20 | 404 |  |  | 404 | 0 | 392 | my $self = shift; | 
| 21 | 404 |  |  |  |  | 672 | my @array = @_; | 
| 22 | 404 |  |  |  |  | 976 | HTML::Zoom::ArrayStream->new({ | 
| 23 |  |  |  |  |  |  | array => \@array, | 
| 24 |  |  |  |  |  |  | zconfig => $self->_zconfig, | 
| 25 |  |  |  |  |  |  | }) | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub stream_concat { | 
| 29 | 134 |  |  | 134 | 0 | 236 | shift->stream_from_array(@_)->flatten; | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | sub stream_from_proto { | 
| 33 | 151 |  |  | 151 | 0 | 179 | my ($self, $proto) = @_; | 
| 34 | 151 |  |  |  |  | 198 | my $ref = ref $proto; | 
| 35 | 151 | 100 | 33 |  |  | 390 | if (not $ref) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 36 | 98 |  |  |  |  | 243 | return $self->stream_from_array({ | 
| 37 |  |  |  |  |  |  | type => 'TEXT', | 
| 38 |  |  |  |  |  |  | raw => $self->_zconfig->parser->html_escape($proto) | 
| 39 |  |  |  |  |  |  | }); | 
| 40 |  |  |  |  |  |  | } elsif ($ref eq 'ARRAY') { | 
| 41 | 28 |  |  |  |  | 60 | return $self->stream_from_array(@$proto); | 
| 42 |  |  |  |  |  |  | } elsif ($ref eq 'CODE') { | 
| 43 | 24 |  |  |  |  | 58 | return $proto->(); | 
| 44 |  |  |  |  |  |  | } elsif ($ref eq 'SCALAR') { | 
| 45 | 0 |  |  |  |  | 0 | return $self->_zconfig->parser->html_to_stream($$proto); | 
| 46 |  |  |  |  |  |  | } elsif (Scalar::Util::blessed($proto) && $proto->can('to_stream')) { | 
| 47 | 1 |  |  |  |  | 5 | my $stream = $proto->to_stream; | 
| 48 | 1 |  |  | 13 |  | 6 | return $self->stream_from_code(sub { $stream->next }); | 
|  | 13 |  |  |  |  | 16 |  | 
| 49 |  |  |  |  |  |  | } | 
| 50 | 0 |  |  |  |  | 0 | die "Don't know how to turn $proto (ref $ref) into a stream"; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub wrap_with_filter { | 
| 54 | 0 |  |  | 0 | 0 | 0 | my ($self, $stream, $match, $filter) = @_; | 
| 55 | 0 |  |  |  |  | 0 | HTML::Zoom::FilterStream->new({ | 
| 56 |  |  |  |  |  |  | stream => $stream, | 
| 57 |  |  |  |  |  |  | match => $match, | 
| 58 |  |  |  |  |  |  | filter => $filter, | 
| 59 |  |  |  |  |  |  | zconfig => $self->_zconfig, | 
| 60 |  |  |  |  |  |  | }) | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub stream_to_array { | 
| 64 | 85 |  |  | 85 | 0 | 103 | my $stream = $_[1]; | 
| 65 | 85 |  |  |  |  | 81 | my @array; | 
| 66 | 85 |  |  |  |  | 272 | while (my ($evt) = $stream->next) { push @array, $evt } | 
|  | 1187 |  |  |  |  | 2698 |  | 
| 67 | 85 |  |  |  |  | 468 | return @array; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | sub flatten_stream_of_streams { | 
| 71 | 18 |  |  | 18 | 0 | 28 | my ($self, $source_stream) = @_; | 
| 72 | 18 |  |  |  |  | 23 | my $cur_stream; | 
| 73 |  |  |  |  |  |  | HTML::Zoom::CodeStream->new({ | 
| 74 |  |  |  |  |  |  | code => sub { | 
| 75 | 306 | 50 |  | 306 |  | 529 | return unless $source_stream; | 
| 76 | 306 |  |  |  |  | 236 | my $next; | 
| 77 | 306 | 100 |  |  |  | 688 | until (($next) = ($cur_stream ? $cur_stream->next : ())) { | 
| 78 | 62 | 100 |  |  |  | 144 | unless (($cur_stream) = $source_stream->next) { | 
| 79 | 18 |  |  |  |  | 23 | undef $source_stream; return; | 
|  | 18 |  |  |  |  | 132 |  | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  | } | 
| 82 | 288 |  |  |  |  | 828 | return $next; | 
| 83 |  |  |  |  |  |  | } | 
| 84 | 18 |  |  |  |  | 134 | }); | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | 1; |