add stub synopsis.t to fail if you forgot to extract the real one
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / StreamUtils.pm
CommitLineData
d80786d0 1package HTML::Zoom::StreamUtils;
2
3use strict;
4use warnings FATAL => 'all';
5use base qw(HTML::Zoom::SubObject);
6
7use HTML::Zoom::CodeStream;
8use HTML::Zoom::FilterStream;
9
10sub stream_from_code {
11 my ($self, $code) = @_;
12 HTML::Zoom::CodeStream->new({
13 code => $code,
14 zconfig => $self->_zconfig,
15 })
16}
17
18sub stream_from_array {
19 my $self = shift;
20 my @array = @_;
21 $self->stream_from_code(sub {
22 return unless @array;
23 return shift @array;
24 });
25}
26
27sub stream_concat {
28 shift->stream_from_array(@_)->flatten;
29}
30
31sub stream_from_proto {
32 my ($self, $proto) = @_;
33 my $ref = ref $proto;
34 if (not $ref) {
35 return $self->stream_from_array({
36 type => 'TEXT',
37 raw => $self->_zconfig->parser->html_escape($proto)
38 });
39 } elsif ($ref eq 'ARRAY') {
40 return $self->stream_from_array(@$proto);
41 } elsif ($ref eq 'CODE') {
42 return $proto->();
43 } elsif ($ref eq 'SCALAR') {
44 return $self->_zconfig->parser->html_to_stream($$proto);
45 }
46 die "Don't know how to turn $proto (ref $ref) into a stream";
47}
48
49sub wrap_with_filter {
50 my ($self, $stream, $match, $filter) = @_;
51 HTML::Zoom::FilterStream->new({
52 stream => $stream,
53 match => $match,
54 filter => $filter,
55 zconfig => $self->_zconfig,
56 })
57}
58
591;