few extra utility routines
Matt S Trout [Fri, 19 Feb 2010 21:47:13 +0000 (21:47 +0000)]
lib/HTML/Zoom.pm
lib/HTML/Zoom/Producer/BuiltIn.pm
lib/HTML/Zoom/ReadFH.pm [new file with mode: 0644]
lib/HTML/Zoom/StreamUtils.pm

index 3234576..0ae18d2 100644 (file)
@@ -5,6 +5,7 @@ use warnings FATAL => 'all';
 
 use HTML::Zoom::ZConfig;
 use HTML::Zoom::MatchWithoutFilter;
+use HTML::Zoom::ReadFH;
 
 sub new {
   my ($class, $args) = @_;
@@ -30,6 +31,12 @@ sub from_html {
   });
 }
 
+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?"
@@ -42,6 +49,22 @@ sub to_stream {
   $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);
index 071d3d5..6ae1ccc 100644 (file)
@@ -2,24 +2,22 @@ package HTML::Zoom::Producer::BuiltIn;
 
 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}) {
diff --git a/lib/HTML/Zoom/ReadFH.pm b/lib/HTML/Zoom/ReadFH.pm
new file mode 100644 (file)
index 0000000..9fb2ad9
--- /dev/null
@@ -0,0 +1,35 @@
+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;
index fcd341c..cdfa430 100644 (file)
@@ -4,6 +4,8 @@ use strict;
 use warnings FATAL => 'all';
 use base qw(HTML::Zoom::SubObject);
 
+use Scalar::Util ();
+
 use HTML::Zoom::CodeStream;
 use HTML::Zoom::FilterStream;
 
@@ -42,6 +44,8 @@ sub stream_from_proto {
     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";
 }
@@ -56,4 +60,11 @@ sub wrap_with_filter {
   })
 }
 
+sub stream_to_array {
+  my $stream = $_[1];
+  my @array;
+  while (my ($evt) = $stream->next) { push @array, $evt }
+  return @array;
+}
+
 1;