HTML-Zoom
view release on metacpan or search on metacpan
lib/HTML/Zoom.pm view on Meta::CPAN
package HTML::Zoom;
use strictures 1;
use HTML::Zoom::ZConfig;
use HTML::Zoom::ReadFH;
use HTML::Zoom::Transform;
use HTML::Zoom::TransformBuilder;
use Scalar::Util ();
our $VERSION = '0.009009';
$VERSION = eval $VERSION;
sub new {
my ($class, $args) = @_;
my $new = {};
$new->{zconfig} = HTML::Zoom::ZConfig->new($args->{zconfig}||{});
bless($new, $class);
}
sub zconfig { shift->_self_or_new->{zconfig} }
sub _self_or_new {
ref($_[0]) ? $_[0] : $_[0]->new
}
sub _with {
bless({ %{$_[0]}, %{$_[1]} }, ref($_[0]));
}
sub from_events {
my $self = shift->_self_or_new;
$self->_with({
initial_events => shift,
});
}
sub from_html {
my $self = shift->_self_or_new;
$self->from_events($self->zconfig->parser->html_to_events($_[0]))
}
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?"
unless $self->{initial_events};
my $sutils = $self->zconfig->stream_utils;
my $stream = $sutils->stream_from_array(@{$self->{initial_events}});
$stream = $_->apply_to_stream($stream) for @{$self->{transforms}||[]};
$stream
}
sub to_fh {
HTML::Zoom::ReadFH->from_zoom(shift);
}
sub to_events {
my $self = shift;
[ $self->zconfig->stream_utils->stream_to_array($self->to_stream) ];
}
sub run {
my $self = shift;
$self->to_events;
return
}
sub apply {
my ($self, $code) = @_;
local $_ = $self;
$self->$code;
}
sub apply_if {
my ($self, $predicate, $code) = @_;
if($predicate) {
local $_ = $self;
$self->$code;
}
else {
$self;
}
}
sub to_html {
my $self = shift;
$self->zconfig->producer->html_from_stream($self->to_stream);
}
sub memoize {
my $self = shift;
ref($self)->new($self)->from_html($self->to_html);
}
sub with_transform {
my $self = shift->_self_or_new;
my ($transform) = @_;
$self->_with({
transforms => [
@{$self->{transforms}||[]},
( run in 0.808 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )