Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vti committed Feb 2, 2012
1 parent fa06a7c commit c812016
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 55 deletions.
90 changes: 35 additions & 55 deletions lib/Text/APL.pm
Expand Up @@ -34,53 +34,46 @@ sub render {
my $reader = $self->{reader}->build($params{input});
my $writer = $self->{writer}->build($params{output});

my $parser = Text::APL::Parser->new;
my $parser = $self->{parser};

my $tape = [];
my $context = Text::APL::Context->new(
helpers => $params{helpers},
vars => $params{vars}
);
$context->add_helper(__print => sub { $writer->(@_) });
$context->add_helper(
__print_escaped => sub {
my ($input) = @_;

$reader->(
sub {
my ($chunk) = @_;
for ($input) { s/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g; }

if (!defined $chunk) {
my $leftover = $parser->parse();
push @$tape, @$leftover if $leftover;

my $code = $self->_translate($tape);

my $context = $self->_build_context_from_args(
helpers => {
__print => sub { $writer->(@_) },
__print_escaped => sub {
my ($input) = @_;

for ($input) {
s/&/&amp;/g;
s/</&lt;/g;
s/>/&gt;/g;
}

$writer->($input);
},
%{$params{helpers} || {}}
},
vars => $params{vars}
);

my $sub_ref = $self->_compile($code, $context);

$sub_ref->($context);

$writer->();
}
else {
my $subtape = $parser->parse($chunk);
push @$tape, @$subtape if @$subtape;
}
},
$params{input}
$writer->($input);
}
);

my $tape = [];

my $reader_cb; $reader_cb = sub {
my ($chunk) = @_;

if (!defined $chunk) {
my $leftover = $parser->parse();
push @$tape, @$leftover if $leftover;

my $code = $self->_translate($tape);

$self->_compile($code, $context)->($context);

$writer->();
}
else {
my $subtape = $parser->parse($chunk);
push @$tape, @$subtape if @$subtape;
}
};

$reader->($reader_cb, $params{input});

return $self;
}

Expand All @@ -96,19 +89,6 @@ sub _compile {
return $self->{compiler}->compile(@_);
}

sub _build_context_from_args {
my $self = shift;

if ( @_ == 1
&& Scalar::Util::blessed($_[0])
&& $_[0]->isa('Text::APL::Context'))
{
return $_[0];
}

return Text::APL::Context->new(@_);
}

sub _build_reader {
my $self = shift;

Expand Down
19 changes: 19 additions & 0 deletions lib/Text/APL/Context.pm
Expand Up @@ -15,6 +15,25 @@ sub _BUILD {
sub vars { $_[0]->{vars} }
sub helpers { $_[0]->{helpers} }

sub add_helper {
my $self = shift;

$self->add('helpers', @_);
}

sub add_var {
my $self = shift;

$self->add('vars', @_);
}

sub add {
my $self = shift;
my ($type, $key, $value) = @_;

$self->{$type}->{$key} = $value;
}

1;
__END__
Expand Down

0 comments on commit c812016

Please sign in to comment.