Skip to content

Commit

Permalink
Enable other interpolation styles
Browse files Browse the repository at this point in the history
Enable `#@instance_var` and `#$global_var` styles of interpolation.
  • Loading branch information
mattwildig committed Mar 5, 2014
1 parent 48abdcc commit fb9a2b1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/haml/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def process_line(line)
case line.text[0]
when DIV_CLASS; push div(line)
when DIV_ID
return push plain(line) if line.text[1] == ?{
return push plain(line) if %w[{ @ $].include?(line.text[1])
push div(line)
when ELEMENT; push tag(line)
when COMMENT; push comment(line.text[1..-1].lstrip)
Expand Down
16 changes: 11 additions & 5 deletions lib/haml/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def inspect_obj(obj)
# @return [String] The text remaining in the scanner after all `#{`s have been processed
def handle_interpolation(str)
scan = StringScanner.new(str)
yield scan while scan.scan(/(.*?)(\\*)\#\{/)
yield scan while scan.scan(/(.*?)(\\*)\#([\{@$])/)
scan.rest
end

Expand Down Expand Up @@ -224,20 +224,26 @@ def human_indentation(indentation)
end

def contains_interpolation?(str)
str.include?('#{')
/#[\{$@]/ === str
end

def unescape_interpolation(str, escape_html = nil)
res = ''
rest = Haml::Util.handle_interpolation str.dump do |scan|
escapes = (scan[2].size - 1) / 2
char = scan[3] # '{', '@' or '$'
res << scan.matched[0...-3 - escapes]
if escapes % 2 == 1
res << '#{'
res << "\##{char}"
else
content = eval('"' + balance(scan, ?{, ?}, 1)[0][0...-1] + '"')
interpolated = if char == '{'
balance(scan, ?{, ?}, 1)[0][0...-1]
else
scan.scan(/\w+/)
end
content = eval('"' + interpolated + '"')
content = "Haml::Helpers.html_escape((#{content}))" if escape_html
res << '#{' + content + "}"# Use eval to get rid of string escapes
res << "\##{char}#{content}#{"}" if char == '{'}"
end
end
res + rest
Expand Down

0 comments on commit fb9a2b1

Please sign in to comment.