Permalink
Browse files

Adds line number to tape

This allows users of this parser to report correct line numbering
For instance, Locale::Maketext::Extract::Plugin::Haml can use
it to find localization calls and report lines appropriately :)
  • Loading branch information...
1 parent 8a5af3b commit 8e3a463228491daeed1d92e96b5ec15e2d1f1135 Carlos Lima committed Dec 7, 2012
Showing with 124 additions and 60 deletions.
  1. +13 −1 lib/Text/Haml.pm
  2. +111 −59 t/parse.t
View
@@ -262,7 +262,7 @@ sub parse {
$level = 0;
}
- my $el = {level => $level, type => 'text', line => $line};
+ my $el = {level => $level, type => 'text', line => $line, lineno => $i+1};
# Haml comment
if ($line =~ m/^$comment_token(?: (.*))?/) {
@@ -281,6 +281,7 @@ sub parse {
$prev->{text} .= "\n" if $prev->{text};
$prev->{text} .= $line;
$prev->{line} .= "\n" . (' ' x $el->{level}) . $el->{line};
+ _update_lineno($prev, $i);
next;
}
}
@@ -402,6 +403,7 @@ sub parse {
if (!$line) {
$line = $lines[++$i] || last;
$el->{line} .= "\n$line";
+ _update_lineno($el, $i);
}
elsif ($type eq 'perl' && $line =~ s/^$attributes_end//) {
last;
@@ -510,6 +512,7 @@ sub parse {
my $prev_stack_el = $tape->[-1];
push @{$prev_stack_el->{text}}, $line;
$prev_stack_el->{line} .= "\n" . $line . "$1|";
+ _update_lineno($prev_stack_el, $i);
}
}
@@ -527,6 +530,15 @@ sub parse {
}
}
+# Updates lineno entry on the tape element
+# for itens spanning more than one line
+sub _update_lineno {
+ my ($el, $lineno) = @_;
+ $lineno++; # report line numbers starting at 1 instead of 0
+ $el->{lineno} =~ s/^(\d+)(?:-\d+)?/$1-$lineno/;
+ return;
+}
+
sub build {
my $self = shift;
my %vars = @_;
Oops, something went wrong.

0 comments on commit 8e3a463

Please sign in to comment.