Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed text indentation in perl blocks

  • Loading branch information...
commit 652252c4a273e254e7e6b4267757b410e7b58db1 1 parent 36636e9
@vti authored
Showing with 118 additions and 52 deletions.
  1. +41 −26 lib/Text/Haml.pm
  2. +77 −26 t/perl.t
View
67 lib/Text/Haml.pm
@@ -59,6 +59,7 @@ sub new {
# Default attributes
my $attrs = {};
+ $attrs->{pretty} = 1;
$attrs->{vars_as_subs} = 0;
$attrs->{tape} = [];
$attrs->{encoding} = 'utf-8';
@@ -106,6 +107,7 @@ sub vars_as_subs {
@_ > 1 ? $_[0]->{vars_as_subs} = $_[1] : $_[0]->{vars_as_subs};
}
+sub pretty { @_ > 1 ? $_[0]->{pretty} = $_[1] : $_[0]->{pretty} }
sub format { @_ > 1 ? $_[0]->{format} = $_[1] : $_[0]->{format} }
sub tape { @_ > 1 ? $_[0]->{tape} = $_[1] : $_[0]->{tape} }
sub encoding { @_ > 1 ? $_[0]->{encoding} = $_[1] : $_[0]->{encoding} }
@@ -470,9 +472,9 @@ sub parse {
# Continue concatenation
else {
- my $prev_el = $tape->[-1];
- push @{$prev_el->{text}}, $line;
- $prev_el->{line} .= $line . "$1|\n";
+ my $prev_stack_el = $tape->[-1];
+ push @{$prev_stack_el->{text}}, $line;
+ $prev_stack_el->{line} .= $line . "$1|\n";
}
}
@@ -547,15 +549,22 @@ EOF
my $stack = [];
my @lines;
- my $count = 0;
+ my $count = 0;
+ my $in_block = 0;
for my $el (@{$self->tape}) {
+ my $level = $el->{level};
+ $level -= 2 * $in_block if $in_block;
+
my $offset = '';
- $offset .= ' ' x $el->{level};
+ $offset .= ' ' x $level;
+
+ my $prev_el = $self->tape->[$count - 1];
+ my $next_el = $self->tape->[$count + 1];
- my $prev_el = $stack->[-1];
+ my $prev_stack_el = $stack->[-1];
- if ($prev_el && $prev_el->{type} eq 'comment') {
- if ($prev_el->{level} == $el->{level}) {
+ if ($prev_stack_el && $prev_stack_el->{type} eq 'comment') {
+ if ($prev_stack_el->{level} == $el->{level}) {
pop @$stack;
}
else {
@@ -570,13 +579,14 @@ EOF
$escape = 'escape';
}
- if ( $el->{type} ne 'block'
- && $el->{line}
- && $prev_el
- && $prev_el->{level} >= $el->{level})
+ if ( $el->{line}
+ && $prev_stack_el
+ && $prev_stack_el->{level} >= $el->{level})
{
while (my $poped = pop @$stack) {
- my $poped_offset = ' ' x $poped->{level};
+ my $level = $poped->{level};
+ $level -= 2 * $in_block if $in_block;
+ my $poped_offset = ' ' x $level;
my $ending = '';
if ($poped->{type} eq 'tag') {
@@ -586,7 +596,10 @@ EOF
$ending .= "<![endif]" if $poped->{if};
$ending .= "-->";
}
- push @lines, qq|\$_H .= "$poped_offset$ending\n";|;
+
+ if ($poped->{type} ne 'block') {
+ push @lines, qq|\$_H .= "$poped_offset$ending\n";|;
+ }
last if $poped->{level} == $el->{level};
}
@@ -671,19 +684,12 @@ EOF
unless $el->{autoclose};
}
elsif (
- !$self->tape->[$count + 1]
- || ( $self->tape->[$count + 1]
- && $self->tape->[$count + 1]->{level} == $el->{level})
+ !$next_el
+ || ( $next_el
+ && $next_el->{level} <= $el->{level})
)
{
- if ( !$self->tape->[$count + 1]
- || $self->tape->[$count + 1]->{type} ne 'block')
- {
- $output .= qq|. "</$el->{name}>"| unless $el->{autoclose};
- }
- else {
- push @$stack, $el;
- }
+ $output .= qq|. "</$el->{name}>"| unless $el->{autoclose};
}
elsif (!$el->{autoclose}) {
push @$stack, $el;
@@ -712,7 +718,16 @@ EOF
$output .= qq/;/;
}
elsif ($el->{type} eq 'block') {
- push @lines, $el->{text};
+ push @lines, $el->{text};
+ push @$stack, $el;
+
+ if ($prev_el && $prev_el->{level} > $el->{level}) {
+ $in_block--;
+ }
+
+ if ($next_el && $next_el->{level} > $el->{level}) {
+ $in_block++;
+ }
}
elsif ($el->{type} eq 'html_comment') {
$output = qq/"$offset"/;
View
103 t/perl.t
@@ -5,20 +5,21 @@ use warnings;
use Text::Haml;
-use Test::More tests => 10;
+use Test::More tests => 12;
my $haml = Text::Haml->new;
+my $output;
# Inserting Perl: =
-my $output = $haml->render(<<'EOF');
+$output = $haml->render(<<'EOF');
%p
- = join(' ', 'hi', 'there', 'reader!')
- = "yo"
+ = join(' ', 'hi', 'there', 'reader!')
+ = "yo"
EOF
is($output, <<'EOF');
<p>
- hi there reader!
- yo
+ hi there reader!
+ yo
</p>
EOF
@@ -60,39 +61,89 @@ EOF
# Perl Blocks
$output = $haml->render(<<'EOF');
%ul
-- for my $i (42..47) {
- %li= $i
-- }
+ - for my $i (42..47) {
+ %li= $i
+ - }
%p See, I can count!
EOF
is($output, <<'EOF');
<ul>
- <li>42</li>
- <li>43</li>
- <li>44</li>
- <li>45</li>
- <li>46</li>
- <li>47</li>
+ <li>42</li>
+ <li>43</li>
+ <li>44</li>
+ <li>45</li>
+ <li>46</li>
+ <li>47</li>
</ul>
<p>See, I can count!</p>
EOF
$output = $haml->render(<<'EOF');
-%p
- - if (1) {
- = "1!"
- %b bonus
- - } else {
- = "2?"
+%ul
+ - foreach (1..3) {
+ %li
+ %foo
+ - }
+%p End
+EOF
+is($output, <<'EOF');
+<ul>
+ <li>
+ <foo></foo>
+ </li>
+ <li>
+ <foo></foo>
+ </li>
+ <li>
+ <foo></foo>
+ </li>
+</ul>
+<p>End</p>
+EOF
+
+$output = $haml->render(<<'EOF');
+%ul
+ - foreach (1..1) {
+ %li
+ - my $i = 0;
+ - my $j = 1;
+ %a(href="#")= $i || $j
+ %form(method="post")
+ - if (1) {
+ %button Foo
+ - } else {
+ %button Bar
+ - }
- }
- %foo
+%p End
+EOF
+is($output, <<'EOF');
+<ul>
+ <li>
+ <a href='#'>1</a>
+ <form method='post'>
+ <button>Foo</button>
+ </form>
+ </li>
+</ul>
+<p>End</p>
+EOF
+
+$output = $haml->render(<<'EOF');
+%p
+ - if (1) {
+ = "1!"
+ %b bonus
+ - } else {
+ = "2?"
+ - }
+ %foo
EOF
is($output, <<'EOF');
<p>
- 1!
- <b>bonus</b>
- <foo>
- </foo>
+ 1!
+ <b>bonus</b>
+ <foo></foo>
</p>
EOF

0 comments on commit 652252c

Please sign in to comment.
Something went wrong with that request. Please try again.