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:
parent
3ccf51dc40
commit
29c29d578f
4 changed files with 39 additions and 4 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
13
lib/hamlit/filters/preserve.rb
Normal file
13
lib/hamlit/filters/preserve.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
require 'hamlit/helpers'
|
||||
|
||||
module Hamlit
|
||||
module Filters
|
||||
class Preserve
|
||||
def compile(lines)
|
||||
ast = [:multi]
|
||||
ast << [:haml, :text, lines.join('
')]
|
||||
ast
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue