From 740884a240ea3a36128bc76dba6e8b13d974c980 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Sun, 21 Feb 2010 15:43:27 -0800 Subject: [PATCH] [Sass] [SCSS] Print to-SCSS and to-Sass comments as adjacent to their rules/mixins if they were in the source. --- lib/haml/exec.rb | 2 +- lib/haml/util.rb | 9 +++++++++ lib/sass/scss/parser.rb | 4 +++- lib/sass/tree/root_node.rb | 16 ++++++++++++++-- test/haml/util_test.rb | 5 +++++ test/sass/conversion_test.rb | 30 ++++++++++++++++++++++++++++++ 6 files changed, 62 insertions(+), 4 deletions(-) diff --git a/lib/haml/exec.rb b/lib/haml/exec.rb index 6791662d..50fd1e14 100644 --- a/lib/haml/exec.rb +++ b/lib/haml/exec.rb @@ -288,7 +288,7 @@ END input.close() if input.is_a?(File) - output.write(tree.render) + output.write(tree.to_scss) output.close() if output.is_a? File rescue ::Sass::SyntaxError => e raise e if @options[:trace] diff --git a/lib/haml/util.rb b/lib/haml/util.rb index fde01990..bdaac682 100644 --- a/lib/haml/util.rb +++ b/lib/haml/util.rb @@ -311,6 +311,15 @@ MSG ruby1_8? ? enum.enum_with_index : enum.each_with_index end + # A version of `Enumerable#enum_cons` that works in Ruby 1.8 and 1.9. + # + # @param enum [Enumerable] The enumerable to get the enumerator for + # @param n [Fixnum] The size of each cons + # @return [Enumerator] The consed enumerator + def enum_cons(enum, n) + ruby1_8? ? enum.enum_cons(n) : enum.each_cons(n) + end + ## Static Method Stuff # The context in which the ERB for \{#def\_static\_method} will be run. diff --git a/lib/sass/scss/parser.rb b/lib/sass/scss/parser.rb index 2ee08e8c..c5385e9e 100644 --- a/lib/sass/scss/parser.rb +++ b/lib/sass/scss/parser.rb @@ -75,7 +75,9 @@ module Sass reverse[/.*?\*\/(.*?)($|\Z)/, 1]. reverse.gsub(/[^\s]/, ' ') text = text.sub(/^\s*\/\//, '/*').gsub(/^\s*\/\//, ' *') + ' */' if single_line - node << Sass::Tree::CommentNode.new(pre_str + text, single_line) + comment = Sass::Tree::CommentNode.new(pre_str + text, single_line) + comment.line = @line - text.count("\n") + node << comment end DIRECTIVES = Set[:mixin, :include, :debug, :for, :while, :if, :import, :media] diff --git a/lib/sass/tree/root_node.rb b/lib/sass/tree/root_node.rb index 07d8941e..77ab61e1 100644 --- a/lib/sass/tree/root_node.rb +++ b/lib/sass/tree/root_node.rb @@ -49,7 +49,7 @@ module Sass # @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize}) # @return [String] The Sass code corresponding to the node def to_sass(opts = {}) - children.map {|child| child.to_sass(0, opts) + "\n"}.join.rstrip + "\n" + to_src(opts, :sass) end # Converts a node to SCSS code that will generate it. @@ -57,11 +57,23 @@ module Sass # @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize}) # @return [String] The SCSS code corresponding to the node def to_scss(opts = {}) - children.map {|child| child.to_scss(0, opts) + "\n"}.join.rstrip + "\n" + to_src(opts, :scss) end protected + def to_src(opts, fmt) + Haml::Util.enum_cons(children + [nil], 2).map do |child, nxt| + child.send("to_#{fmt}", 0, opts) + + if nxt && child.is_a?(CommentNode) && + child.line + child.value.count("\n") + 1 == nxt.line + "" + else + "\n" + end + end.join.rstrip + "\n" + end + # Destructively converts this static Sass node into a static CSS node, # and checks that there are no properties at root level. # diff --git a/test/haml/util_test.rb b/test/haml/util_test.rb index 7a8bd2f3..e290582d 100755 --- a/test/haml/util_test.rb +++ b/test/haml/util_test.rb @@ -95,6 +95,11 @@ class UtilTest < Test::Unit::TestCase enum_with_index(%w[foo bar baz]).map {|s, i| "#{s}#{i}"}) end + def test_enum_cons + assert_equal(%w[foobar barbaz], + enum_cons(%w[foo bar baz], 2).map {|s1, s2| "#{s1}#{s2}"}) + end + def test_caller_info assert_equal(["/tmp/foo.rb", 12, "fizzle"], caller_info("/tmp/foo.rb:12: in `fizzle'")) assert_equal(["/tmp/foo.rb", 12, nil], caller_info("/tmp/foo.rb:12")) diff --git a/test/sass/conversion_test.rb b/test/sass/conversion_test.rb index 4384cc49..12731717 100755 --- a/test/sass/conversion_test.rb +++ b/test/sass/conversion_test.rb @@ -370,6 +370,36 @@ foo SASS end + def test_immediately_preceding_comments + assert_renders <