Skip to content

Commit

Permalink
Fix newlines around filters
Browse files Browse the repository at this point in the history
  • Loading branch information
eagletmt committed Mar 18, 2015
1 parent 903172d commit 8b02249
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/fast_haml/filter_compilers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def compile_texts(temple, texts, tab_width: 0)
texts.each do |text|
temple << [:static, tabs] << text_compiler.compile(text) << [:static, "\n"] << [:newline]
end
temple.pop # discard last [:newline]
nil
end

Expand Down
2 changes: 1 addition & 1 deletion lib/fast_haml/filter_compilers/cdata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module FastHaml
module FilterCompilers
class Cdata < Base
def compile(texts)
temple = [:multi, [:static, "<![CDATA[\n"]]
temple = [:multi, [:static, "<![CDATA[\n"], [:newline]]
compile_texts(temple, strip_last_empty_lines(texts), tab_width: 4)
temple << [:static, "]]>"]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fast_haml/filter_compilers/css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module FastHaml
module FilterCompilers
class Css < Base
def compile(texts)
temple = [:multi, [:static, "\n"]]
temple = [:multi, [:static, "\n"], [:newline]]
compile_texts(temple, strip_last_empty_lines(texts), tab_width: 2)
[:haml, :tag, 'style', false, [:html, :attrs], temple]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fast_haml/filter_compilers/escaped.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Escaped < Base
include Temple::Utils

def compile(texts)
temple = [:multi]
temple = [:multi, [:newline]]
compile_texts(temple, strip_last_empty_lines(texts))
escape_code = Temple::Filters::Escapable.new.instance_variable_get(:@escape_code)
sym = unique_name
Expand Down
2 changes: 1 addition & 1 deletion lib/fast_haml/filter_compilers/javascript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module FastHaml
module FilterCompilers
class Javascript < Base
def compile(texts)
temple = [:multi, [:static, "\n"]]
temple = [:multi, [:static, "\n"], [:newline]]
compile_texts(temple, strip_last_empty_lines(texts), tab_width: 2)
[:haml, :tag, 'script', false, [:html, :attrs], [:html, :js, temple]]
end
Expand Down
6 changes: 2 additions & 4 deletions lib/fast_haml/filter_compilers/plain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ module FastHaml
module FilterCompilers
class Plain < Base
def compile(texts)
temple = [:multi]
texts = strip_last_empty_lines(texts)
compile_texts(temple, texts[0 .. -2])
temple << text_compiler.compile(texts[-1]) << [:newline]
temple = [:multi, [:newline]]
compile_texts(temple, texts)
temple
end
end
Expand Down
5 changes: 2 additions & 3 deletions lib/fast_haml/filter_compilers/preserve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ class Preserve < Base
include Temple::Utils

def compile(texts)
temple = [:multi]
temple = [:multi, [:newline]]
texts.each do |text|
temple << text_compiler.compile(text)
unless texts.last.equal?(text)
temple << [:static, "\n"]
temple << [:static, "\n"] << [:newline]
end
temple << [:newline]
end
sym = unique_name
[:multi,
Expand Down
2 changes: 1 addition & 1 deletion lib/fast_haml/filter_compilers/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module FastHaml
module FilterCompilers
class Ruby < Base
def compile(texts)
[:multi, [:code, strip_last_empty_lines(texts).join("\n")], [:newline]]
[:multi, [:newline], [:code, strip_last_empty_lines(texts).join("\n")]]
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/render/filters/plain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe 'Plain filter rendering', type: :render do
it 'renders plain filter' do
expect(render_string(<<HAML)).to eq("<span>\nhello\n<span>world</span>\n</span>\n")
expect(render_string(<<HAML)).to eq("<span>\nhello\n\n<span>world</span>\n</span>\n")
%span
:plain
he#{'llo'}
Expand All @@ -11,7 +11,7 @@
end

it 'strips last empty lines' do
expect(render_string(<<HAML)).to eq("<span>\nhello\n\nabc\n<span>world</span>\n</span>\n")
expect(render_string(<<HAML)).to eq("<span>\nhello\n\nabc\n\n\n<span>world</span>\n</span>\n")
%span
:plain
he#{'llo'}
Expand Down

0 comments on commit 8b02249

Please sign in to comment.