summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/Text/Xslate/Syntax/Handlebars.pm27
-rw-r--r--t/expressions.t2
2 files changed, 20 insertions, 9 deletions
diff --git a/lib/Text/Xslate/Syntax/Handlebars.pm b/lib/Text/Xslate/Syntax/Handlebars.pm
index 9bd1366..831ad7b 100644
--- a/lib/Text/Xslate/Syntax/Handlebars.pm
+++ b/lib/Text/Xslate/Syntax/Handlebars.pm
@@ -6,6 +6,8 @@ use Text::Xslate::Util qw($STRING neat p);
extends 'Text::Xslate::Parser';
+my $nl = qr/\x0d?\x0a/;
+
sub _build_identity_pattern { qr/[A-Za-z_][A-Za-z0-9_?]*/ }
sub _build_comment_pattern { qr/\![^;]*/ }
@@ -32,6 +34,7 @@ sub split_tags {
my @chunks;
my $close_tag;
+ my $standalone = 1;
while ($input) {
if ($close_tag) {
my $start = 0;
@@ -50,6 +53,13 @@ sub split_tags {
$input =~ s/\A\Q$close_tag//
or die "Oops!";
+ if ($code =~ m{^[!#^/]} && $standalone) {
+ $input =~ s/\A$nl//;
+ if (@chunks > 0 && $chunks[-1][0] eq 'text') {
+ $chunks[-1][1] =~ s/^(?:(?!\n)\s)*\z//m;
+ }
+ }
+
push @chunks, [
($close_tag eq '}}}' ? 'raw_code' : 'code'),
$code
@@ -70,7 +80,16 @@ sub split_tags {
}
}
elsif ($input =~ s/\A$lex_text//) {
- push @chunks, [ text => $1 ];
+ my $text = $1;
+ if (length($text)) {
+ push @chunks, [ text => $text ];
+ if ($standalone) {
+ $standalone = $text =~ /(?:^|\n)\s*$/;
+ }
+ else {
+ $standalone = $text =~ /\n\s*$/;
+ }
+ }
}
else {
confess "Oops: unreached code, near " . p($input);
@@ -97,21 +116,15 @@ sub preprocess {
my @chunks = $self->split_tags($input);
my $code = '';
- my $suppress_newline;
for my $chunk (@chunks) {
my ($type, $content) = @$chunk;
if ($type eq 'text') {
$content =~ s/(["\\])/\\$1/g;
- $content =~ s/^\n//
- if $suppress_newline;
$code .= qq{print_raw "$content";\n}
if length($content);
- $suppress_newline = 0;
}
elsif ($type eq 'code') {
$code .= qq{$content;\n};
- $suppress_newline = 1
- if $content =~ m{^[#^/]};
}
elsif ($type eq 'raw_code') {
$code .= qq{mark_raw $content;\n};
diff --git a/t/expressions.t b/t/expressions.t
index c2e928c..4cdc403 100644
--- a/t/expressions.t
+++ b/t/expressions.t
@@ -45,7 +45,6 @@ render_ok(
"backtracking with ../"
);
-{ local $TODO = "autochomping issues";
render_ok(
<<'TEMPLATE',
{{#page}}
@@ -63,7 +62,6 @@ TEMPLATE
RENDERED
"multilevel backtracking with ../"
);
-}
render_ok(
'{{#article}}<h1>{{title}}</h1> - {{../metadata.date}}{{/article}}',