use HTML::Zoom::ZConfig;
use HTML::Zoom::MatchWithoutFilter;
+use HTML::Zoom::ReadFH;
sub new {
my ($class, $args) = @_;
});
}
+sub from_file {
+ my $self = shift->_self_or_new;
+ my $filename = shift;
+ $self->from_html(do { local (@ARGV, $/) = ($filename); <> });
+}
+
sub to_stream {
my $self = shift;
die "No events to build from - forgot to call from_html?"
$stream
}
+sub to_fh {
+ HTML::Zoom::ReadFH->from_zoom(shift);
+}
+
+sub run {
+ my $self = shift;
+ $self->zconfig->stream_utils->stream_to_array($self->to_stream);
+ return
+}
+
+sub apply {
+ my ($self, $code) = @_;
+ local $_ = $self;
+ $self->$code;
+}
+
sub to_html {
my $self = shift;
$self->zconfig->producer->html_from_stream($self->to_stream);
use strict;
use warnings FATAL => 'all';
-
-sub new { bless({}, $_[0]) }
-
-sub with_zconfig { shift }
+use base qw(HTML::Zoom::SubObject);
sub html_from_stream {
- my ($class, $stream) = @_;
- my $html;
- while (my ($evt) = $stream->next) { $html .= $class->_event_to_html($evt) }
- return $html;
+ my ($self, $stream) = @_;
+ return
+ join '',
+ map $self->event_to_html($_),
+ $self->_zconfig->stream_utils->stream_to_array($stream)
}
sub html_from_events {
- my ($class, $events) = @_;
- join '', map $class->_event_to_html($_), @$events;
+ my ($self, $events) = @_;
+ join '', map $self->event_to_html($_), @$events;
}
-sub _event_to_html {
+sub event_to_html {
my ($self, $evt) = @_;
# big expression
if (defined $evt->{raw}) {
--- /dev/null
+package HTML::Zoom::ReadFH;
+
+use strict;
+use warnings FATAL => 'all';
+
+sub from_zoom {
+ my ($class, $zoom) = @_;
+ bless({ _zoom => $zoom }, $class)
+}
+
+sub to_zoom {
+ my $self = shift;
+ # A small defense against accidental footshots. I hope.
+ # If this turns out to merely re-aim the gun at your left nipple, please
+ # come complain with a documented use case and we'll discuss deleting it.
+ die "Already started reading - there ain't no going back now"
+ if $self->{_stream};
+ $self->{_zoom}
+}
+
+sub getline {
+ my $self = shift;
+ my $html;
+ my $stream = $self->{_stream} ||= $self->{_zoom}->to_stream;
+ my $producer = $self->{_producer} ||= $self->{_zoom}->zconfig->producer;
+ while (my ($evt) = $stream->next) {
+ $html .= $producer->event_to_html($evt);
+ last if $evt->{flush};
+ }
+ return $html
+}
+
+sub close { "The door shuts behind you with a ominous boom" }
+
+1;
use warnings FATAL => 'all';
use base qw(HTML::Zoom::SubObject);
+use Scalar::Util ();
+
use HTML::Zoom::CodeStream;
use HTML::Zoom::FilterStream;
return $proto->();
} elsif ($ref eq 'SCALAR') {
return $self->_zconfig->parser->html_to_stream($$proto);
+ } elsif (blessed($proto) && $proto->can('as_stream')) {
+ return $proto->as_stream;
}
die "Don't know how to turn $proto (ref $ref) into a stream";
}
})
}
+sub stream_to_array {
+ my $stream = $_[1];
+ my @array;
+ while (my ($evt) = $stream->next) { push @array, $evt }
+ return @array;
+}
+
1;