Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Cleanup

  • Loading branch information...
commit c8120168da635c820bf28a3e56a7e126b2db8fc9 1 parent fa06a7c
Viacheslav Tykhanovskyi authored
Showing with 54 additions and 55 deletions.
  1. +35 −55 lib/Text/APL.pm
  2. +19 −0 lib/Text/APL/Context.pm
90 lib/Text/APL.pm
View
@@ -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;
}
@@ -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;
19 lib/Text/APL/Context.pm
View
@@ -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__
Please sign in to comment.
Something went wrong with that request. Please try again.