1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Parse string interpolation

This commit is contained in:
Takashi Kokubun 2015-03-17 08:17:31 +09:00
parent f482296c24
commit 74561f3ee3
5 changed files with 37 additions and 2 deletions

View file

@ -6,6 +6,7 @@ require 'hamlit/filter_formatter'
require 'hamlit/multiline_preprocessor'
require 'hamlit/parser'
require 'hamlit/script_compiler'
require 'hamlit/text_compiler'
module Hamlit
class Engine < Temple::Engine
@ -17,6 +18,7 @@ module Hamlit
use FilterFormatter
use FilterCompiler
use ScriptCompiler
use TextCompiler
use DynamicFormatter
html :Fast
filter :Escapable

View file

@ -52,6 +52,7 @@ module Hamlit
scanner = StringScanner.new(line)
scanner.scan(/ +/)
return parse_text(scanner) if scanner.match?(/\#{/)
case scanner.peek(1)
when '!'
@ -149,7 +150,7 @@ module Hamlit
end
def parse_text(scanner)
ast = [:static]
ast = [:haml, :text]
ast << scanner.scan(/.+/)
ast
end

View file

@ -0,0 +1,17 @@
require 'temple/html/filter'
# NOTE: This compiler has an extremely bad effect for performance.
# We should optimize this.
module Hamlit
class TextCompiler < Temple::HTML::Filter
def on_haml_text(exp)
compile_text(exp)
end
private
def compile_text(exp)
[:dynamic, %Q{"#{exp.gsub(/"/, '\"')}"}]
end
end
end

View file

@ -0,0 +1,15 @@
describe Hamlit::Engine do
describe 'text' do
it 'renders string interpolation' do
assert_render(<<-HAML, <<-HTML)
#{ "a#{3}a" }a" #{[1, 2]} b
a#{{ a: 3 }}
<ht#{2}ml>
HAML
a3aa" [1, 2] b
a{:a=>3}
<ht2ml>
HTML
end
end
end

View file

@ -4,7 +4,7 @@ describe Hamlit::Parser do
assert_compile(
'%span a',
[:multi,
[:html, :tag, 'span', [:haml, :attrs], [:static, 'a']],
[:html, :tag, 'span', [:haml, :attrs], [:haml, :text, 'a']],
[:static, "\n"],
[:newline]],
)