Skip to content

Commit

Permalink
fixed small bug in template parser
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 27, 2011
1 parent c98f9d4 commit 3a3d28f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
49 changes: 24 additions & 25 deletions lib/Mojo/Template.pm
Expand Up @@ -283,46 +283,45 @@ sub parse {
$
/x;

# Replace line regex
my $replace_re = qr/^(\s*)$line_start$replace/;

# Tokenize
my $state = 'text';
my @capture_token;
my $trimming = 0;
for my $line (split /\n/, $tmpl) {

# Perl line
if ($line !~ s/$replace_re/$1$raw_line_start/ && $line =~ $line_re) {
my @token = ();
unless ($line =~ s/^(\s*)$line_start$replace/$1$raw_line_start/) {
if ($line =~ $line_re) {
my @token = ();

# Capture end
push @token, 'cpen', undef if $4;
# Capture end
push @token, 'cpen', undef if $4;

# Capture start
push @token, 'cpst', undef if $6;
# Capture start
push @token, 'cpst', undef if $6;

# Expression
if ($2) {
unshift @token, 'text', $1;
push @token, $3 ? 'escp' : 'expr', $5;
# Expression
if ($2) {
unshift @token, 'text', $1;
push @token, $3 ? 'escp' : 'expr', $5;

# Hint at end
push @token, 'text', '';
# Hint at end
push @token, 'text', '';

# Line ending
push @token, 'text', "\n";
}
# Line ending
push @token, 'text', "\n";
}

# Code
else { push @token, 'code', $5 }
# Code
else { push @token, 'code', $5 }

push @{$self->tree}, \@token;
next;
}
push @{$self->tree}, \@token;
next;
}

# Comment line
next if $line =~ /^\s*$line_start$cmnt(?:.+)?$/;
# Comment line
elsif ($line =~ /^\s*$line_start$cmnt/) {next}
}

# Escaped line ending
if ($line =~ /(\\+)$/) {
Expand Down
12 changes: 11 additions & 1 deletion t/mojo/template.t
Expand Up @@ -19,7 +19,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 196;
use Test::More tests => 198;

use File::Spec;
use File::Temp;
Expand Down Expand Up @@ -100,6 +100,11 @@ EOF
is $output, "lalala <%= 1 +\n 1 %> 12\n34\n",
'expression tag has been replaced';

# Replace comment tag
$mt = Mojo::Template->new;
$output = $mt->render('<%%# 1 + 1 %>');
is $output, "<%# 1 + 1 %>\n", 'comment tag has been replaced';

# Replace line
$mt = Mojo::Template->new;
$output = $mt->render('%% my $foo = 23;');
Expand All @@ -115,6 +120,11 @@ $mt = Mojo::Template->new;
$output = $mt->render('%%= 1 + 1');
is $output, "%= 1 + 1\n", 'expression line has been replaced';

# Replace comment line
$mt = Mojo::Template->new;
$output = $mt->render(' %%# 1 + 1');
is $output, " %# 1 + 1\n", 'comment line has been replaced';

# Replace mixed
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
Expand Down

0 comments on commit 3a3d28f

Please sign in to comment.