Skip to content

Commit

Permalink
improved Mojo::Template to allow trimming of whitespace characters ar…
Browse files Browse the repository at this point in the history
…ound tags to be limited to one side
  • Loading branch information
kraih committed Jan 24, 2014
1 parent cd47d8c commit 093ad62
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 37 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,6 +1,8 @@

4.69 2014-01-24
- Improved router to allow format detection for bridges.
- Improved Mojo::Template to allow trimming of whitespace characters around
tags to be limited to one side.

4.68 2014-01-22
- Added Mojo::DOM::Node.
Expand Down
47 changes: 15 additions & 32 deletions lib/Mojo/Template.pm
Expand Up @@ -144,33 +144,17 @@ sub parse {

my $token_re = qr/
(
\Q$tag$replace\E # Replace
\Q$tag\E(?:\Q$replace\E|\Q$cmnt\E) # Replace
|
\Q$tag$expr$escp\E\s*\Q$cpen\E(?!\w) # Escaped expression (end)
\Q$tag$expr\E(?:\Q$escp\E)?(?:\s*\Q$cpen\E(?!\w))? # Expression
|
\Q$tag$expr$escp\E # Escaped expression
\Q$tag\E(?:\s*\Q$cpen\E(?!\w))? # Code
|
\Q$tag$expr\E\s*\Q$cpen\E(?!\w) # Expression (end)
|
\Q$tag$expr\E # Expression
|
\Q$tag$cmnt\E # Comment
|
\Q$tag\E\s*\Q$cpen\E(?!\w) # Code (end)
|
\Q$tag\E # Code
|
(?<!\w)\Q$cpst\E\s*\Q$trim$end\E # Trim end (start)
|
\Q$trim$end\E # Trim end
|
(?<!\w)\Q$cpst\E\s*\Q$end\E # End (start)
|
\Q$end\E # End
(?:(?<!\w)\Q$cpst\E\s*)?(?:\Q$trim\E{1,3})?\Q$end\E # End
)
/x;
my $cpen_re = qr/^(\Q$tag\E)(?:\Q$expr\E)?(?:\Q$escp\E)?\s*\Q$cpen\E/;
my $end_re = qr/^(?:(\Q$cpst\E)\s*)?(\Q$trim\E)?\Q$end\E$/;
my $end_re = qr/^(?:(\Q$cpst\E)\s*)?(\Q$trim\E{1,3})?\Q$end\E$/;

# Split lines
my $state = 'text';
Expand Down Expand Up @@ -207,10 +191,11 @@ sub parse {
# Capture start
splice @token, -2, 0, 'cpst', undef if $1;

# Trim previous text
# Trim left side
if ($2) {
$trimming = 1;
$self->_trim(\@token);
$trimming = length($2) / length($trim);
$self->_trim(\@token) if $trimming eq 1 || $trimming eq 2;
$trimming = 0 if $trimming eq 2;
}

# Hint at end
Expand All @@ -235,7 +220,7 @@ sub parse {
# Replace
$token = $tag if $token eq "$tag$replace";

# Convert whitespace text to line noise
# Trim right side (convert whitespace to line noise)
if ($trimming && $token =~ s/^(\s+)//) {
push @token, 'code', $1;
$trimming = 0;
Expand Down Expand Up @@ -283,14 +268,10 @@ sub _trim {
return unless $line->[$j] eq 'text';

# Convert whitespace text to line noise
my $value = $line->[$j + 1];
if ($line->[$j + 1] =~ s/(\s+)$//) {
$value = $line->[$j + 1];
splice @$line, $j, 0, 'code', $1;
}
splice @$line, $j, 0, 'code', $1 if $line->[$j + 1] =~ s/(\s+)$//;

# Text left
return if length $value;
return if length $line->[$j + 1];
}
}

Expand Down Expand Up @@ -409,7 +390,9 @@ backslash.
Whitespace characters around tags can be trimmed with a special tag ending.
<%= All whitespace characters around this expression will be trimmed =%>
<%= Trim whitespace characters on both sides of this expression =%>
<%= Trim whitespace characters on the left side of this expression ==%>
<%= Trim whitespace characters on the right side of this expression ===%>
You can capture whole template blocks for reuse later with the C<begin> and
C<end> keywords.
Expand Down
10 changes: 6 additions & 4 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -128,12 +128,14 @@ backslash.
in multiple\\
lines

You can also add an additional equal sign to the end of a tag to have it
automatically remove all surrounding whitespace, this allows free indenting
without ruining the result.
You can also add additional equal signs to the end of a tag to have it
automatically remove surrounding whitespace, this allows you to freely indent
your code without ruining the result.

<% for (1 .. 3) { %>
<%= $foo =%>
<%= 'trim both sides' =%>
<%= 'trim left side' ==%>
<%= 'trim right side' ===%>
<% } %>

Stash values that don't have invalid characters in their name get
Expand Down
12 changes: 11 additions & 1 deletion t/mojo/template.t
Expand Up @@ -29,11 +29,21 @@ $mt->prepend('my @foo = (3, 4);')->parse('<%= @foo %>:<%== @foo %>');
my $output = $mt->build->compile || $mt->interpret;
is $output, "2:2\n", 'same context';

# Trim tag
# Trim tag (both sides)
$mt = Mojo::Template->new;
$output = $mt->render(" ♥ <%= 'test♥' =%> \n");
is $output, ' ♥test♥', 'tag trimmed';

# Trim tag (left side)
$mt = Mojo::Template->new;
$output = $mt->render(" ♥ <%= 'test♥' ==%> \n");
is $output, " ♥test♥ \n", 'tag trimmed';

# Trim tag (right side)
$mt = Mojo::Template->new;
$output = $mt->render(" ♥ <%= 'test♥' ===%> \n");
is $output, ' ♥ test♥', 'tag trimmed';

# Trim expression
$mt = Mojo::Template->new;
$output = $mt->render("<%= '123' %><%= 'test' =%>\n");
Expand Down

0 comments on commit 093ad62

Please sign in to comment.