Permalink
Browse files

Parse interpolation inside of text

  • Loading branch information...
1 parent ef39191 commit 43b439df15a1058baea98dc4fd9431bb33fb4628 @vti committed Dec 6, 2011
Showing with 29 additions and 27 deletions.
  1. +12 −2 lib/Text/Haml.pm
  2. +17 −25 t/interpolation.t
View
@@ -7,7 +7,7 @@ use IO::File;
use Scalar::Util qw/weaken/;
use Encode qw/decode/;
-our $VERSION = '0.990104';
+our $VERSION = '0.990105';
use constant CHUNK_SIZE => 4096;
@@ -874,6 +874,7 @@ sub _parse_text {
$output .= qq/$prefix".$1."/;
}
else {
+ $text = $self->_parse_interpolation($text);
$text =~ s/\\\#/\#/g;
$output .= $expr ? $text : quotemeta($text);
last;
@@ -883,6 +884,15 @@ sub _parse_text {
return $expr ? qq/"$output"/ : $output;
}
+sub _parse_interpolation {
+ my $self = shift;
+ my ($text) = @_;
+
+ $text =~ s/(?<!\\)\#\{(.+?)\}/eval "$1"/msge;
+
+ return $text;
+}
+
sub compile {
my $self = shift;
@@ -1268,7 +1278,7 @@ Norman Clarke
=head1 COPYRIGHT AND LICENSE
-Copyright (C) 2009-2010, Viacheslav Tykhanovskyi.
+Copyright (C) 2009-2011, Viacheslav Tykhanovskyi.
This program is free software, you can redistribute it and/or modify it under
the terms of the Artistic License version 2.0.
View
@@ -3,7 +3,7 @@
use strict;
use warnings;
-use Test::More tests => 3;
+use Test::More tests => 4;
use Text::Haml;
@@ -16,13 +16,6 @@ is($output, <<'EOF');
<p>This is scrumptious cake!</p>
EOF
-#$output = $haml->render(<<'EOF', quality => 'scrumptious');
-#%p= "This is #{quality} cake!"
-#EOF
-#is($output, <<'EOF');
-#<p>This is scrumptious cake!</p>
-#EOF
-
$output = $haml->render(<<'EOF', var => 'foo');
%p \#{$var}
%p \\#{$var}
@@ -43,20 +36,19 @@ is($output, <<'EOF');
And yon presence thereof: \{$foo}
</p>
EOF
-#
-#Interpolation can also be used within filters. For example:
-#
-#:javascript
-# $(document).ready(function() {
-# alert(#{@message.to_json});
-# });
-#
-#might compile to
-#
-#<script type='text/javascript'>
-# //<![CDATA[
-# $(document).ready(function() {
-# alert("Hi there!");
-# });
-# //]]>
-#</script>
+
+$output = $haml->render(<<'EOF');
+:javascript
+ $(document).ready(function() {
+ alert(#{1 + 1});
+ });
+EOF
+is($output, <<'EOF');
+<script type='text/javascript'>
+ //<![CDATA[
+ $(document).ready(function() {
+alert(2);
+});
+ //]]>
+</script>
+EOF

0 comments on commit 43b439d

Please sign in to comment.