From 8b022496ce63d0ff3f5c64dadc5703ae38eb0e63 Mon Sep 17 00:00:00 2001 From: Kohei Suzuki Date: Wed, 18 Mar 2015 18:16:44 +0900 Subject: [PATCH] Fix newlines around filters --- lib/fast_haml/filter_compilers/base.rb | 1 + lib/fast_haml/filter_compilers/cdata.rb | 2 +- lib/fast_haml/filter_compilers/css.rb | 2 +- lib/fast_haml/filter_compilers/escaped.rb | 2 +- lib/fast_haml/filter_compilers/javascript.rb | 2 +- lib/fast_haml/filter_compilers/plain.rb | 6 ++---- lib/fast_haml/filter_compilers/preserve.rb | 5 ++--- lib/fast_haml/filter_compilers/ruby.rb | 2 +- spec/render/filters/plain_spec.rb | 4 ++-- 9 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/fast_haml/filter_compilers/base.rb b/lib/fast_haml/filter_compilers/base.rb index d881ad6..4bc4fbd 100644 --- a/lib/fast_haml/filter_compilers/base.rb +++ b/lib/fast_haml/filter_compilers/base.rb @@ -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 diff --git a/lib/fast_haml/filter_compilers/cdata.rb b/lib/fast_haml/filter_compilers/cdata.rb index 6677cc4..9def0fb 100644 --- a/lib/fast_haml/filter_compilers/cdata.rb +++ b/lib/fast_haml/filter_compilers/cdata.rb @@ -4,7 +4,7 @@ module FastHaml module FilterCompilers class Cdata < Base def compile(texts) - temple = [:multi, [:static, ""] end diff --git a/lib/fast_haml/filter_compilers/css.rb b/lib/fast_haml/filter_compilers/css.rb index a76d3cd..2e6680f 100644 --- a/lib/fast_haml/filter_compilers/css.rb +++ b/lib/fast_haml/filter_compilers/css.rb @@ -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 diff --git a/lib/fast_haml/filter_compilers/escaped.rb b/lib/fast_haml/filter_compilers/escaped.rb index f3990df..aa7b2ac 100644 --- a/lib/fast_haml/filter_compilers/escaped.rb +++ b/lib/fast_haml/filter_compilers/escaped.rb @@ -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 diff --git a/lib/fast_haml/filter_compilers/javascript.rb b/lib/fast_haml/filter_compilers/javascript.rb index dbd5d2a..0a6d4f9 100644 --- a/lib/fast_haml/filter_compilers/javascript.rb +++ b/lib/fast_haml/filter_compilers/javascript.rb @@ -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 diff --git a/lib/fast_haml/filter_compilers/plain.rb b/lib/fast_haml/filter_compilers/plain.rb index 17fa5f4..11e1535 100644 --- a/lib/fast_haml/filter_compilers/plain.rb +++ b/lib/fast_haml/filter_compilers/plain.rb @@ -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 diff --git a/lib/fast_haml/filter_compilers/preserve.rb b/lib/fast_haml/filter_compilers/preserve.rb index 92533c0..39351f4 100644 --- a/lib/fast_haml/filter_compilers/preserve.rb +++ b/lib/fast_haml/filter_compilers/preserve.rb @@ -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, diff --git a/lib/fast_haml/filter_compilers/ruby.rb b/lib/fast_haml/filter_compilers/ruby.rb index a818922..ad4bcf3 100644 --- a/lib/fast_haml/filter_compilers/ruby.rb +++ b/lib/fast_haml/filter_compilers/ruby.rb @@ -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 diff --git a/spec/render/filters/plain_spec.rb b/spec/render/filters/plain_spec.rb index daaece0..e03fad2 100644 --- a/spec/render/filters/plain_spec.rb +++ b/spec/render/filters/plain_spec.rb @@ -2,7 +2,7 @@ RSpec.describe 'Plain filter rendering', type: :render do it 'renders plain filter' do - expect(render_string(<\nhello\nworld\n\n") + expect(render_string(<\nhello\n\nworld\n\n") %span :plain he#{'llo'} @@ -11,7 +11,7 @@ end it 'strips last empty lines' do - expect(render_string(<\nhello\n\nabc\nworld\n\n") + expect(render_string(<\nhello\n\nabc\n\n\nworld\n\n") %span :plain he#{'llo'}