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

Implement preserve filter

This commit is contained in:
Takashi Kokubun 2015-03-29 02:46:35 +09:00
parent 3ccf51dc40
commit 29c29d578f
4 changed files with 39 additions and 4 deletions

View file

@ -2,6 +2,7 @@ require 'hamlit/concerns/included'
require 'hamlit/concerns/registerable'
require 'hamlit/filters/escaped'
require 'hamlit/filters/plain'
require 'hamlit/filters/preserve'
module Hamlit
module Compilers
@ -11,8 +12,9 @@ module Hamlit
included do
extend Concerns::Registerable
register :escaped, Filters::Escaped
register :plain, Filters::Plain
register :escaped, Filters::Escaped
register :plain, Filters::Plain
register :preserve, Filters::Preserve
end
def on_haml_filter(name, lines)

View file

@ -32,12 +32,24 @@ module Hamlit
def read_lines
lines = []
spaces = ' ' * (@current_indent * 2)
while count_indent(next_line, strict: false) >= @current_indent
while read_line?
lines << @lines[@current_lineno + 1].gsub(/\A#{spaces}/, '')
@current_lineno += 1
end
lines
end
private
def read_line?
return true if count_indent(next_line, strict: false) >= @current_indent
line = @lines[@current_lineno + 1]
return false unless line
# NOTE: preserve filter also requires an empty line
line.gsub(/ /, '').length == 0
end
end
end
end

View file

@ -3,7 +3,7 @@ module Hamlit
class Plain
def compile(lines)
ast = [:multi]
text = lines.join("\n")
text = strip_last(lines).join("\n")
ast << [:haml, :text, text]
ast << [:static, "\n"] if string_interpolated?(text)
ast
@ -14,6 +14,14 @@ module Hamlit
def string_interpolated?(text)
text =~ /\#{[^\#{}]*}/
end
def strip_last(lines)
lines = lines.dup
while lines.last && lines.last.length == 0
lines.delete_at(-1)
end
lines
end
end
end
end

View file

@ -0,0 +1,13 @@
require 'hamlit/helpers'
module Hamlit
module Filters
class Preserve
def compile(lines)
ast = [:multi]
ast << [:haml, :text, lines.join('&#x000A;')]
ast
end
end
end
end