introduce ZConfig system, first cut at HTML::Zoom itself
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / StreamUtils.pm
1 package HTML::Zoom::StreamUtils;
2
3 use strict;
4 use warnings FATAL => 'all';
5 use base qw(HTML::Zoom::SubObject);
6
7 use HTML::Zoom::CodeStream;
8 use HTML::Zoom::FilterStream;
9
10 sub stream_from_code {
11   my ($self, $code) = @_;
12   HTML::Zoom::CodeStream->new({
13     code => $code,
14     zconfig => $self->_zconfig,
15   })
16 }
17
18 sub 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
27 sub stream_concat {
28   shift->stream_from_array(@_)->flatten;
29 }
30
31 sub 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
49 sub 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
59 1;