mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
[Sass] [SCSS] Print to-SCSS and to-Sass comments as adjacent to their rules/mixins if they were in the source.
This commit is contained in:
parent
7974a92aae
commit
740884a240
6 changed files with 62 additions and 4 deletions
|
@ -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]
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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.
|
||||
#
|
||||
|
|
|
@ -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"))
|
||||
|
|
|
@ -370,6 +370,36 @@ foo
|
|||
SASS
|
||||
end
|
||||
|
||||
def test_immediately_preceding_comments
|
||||
assert_renders <<SASS, <<SCSS
|
||||
/* Foo
|
||||
Bar
|
||||
Baz
|
||||
.foo#bar
|
||||
a: b
|
||||
SASS
|
||||
/* Foo
|
||||
* Bar
|
||||
* Baz */
|
||||
.foo#bar {
|
||||
a: b; }
|
||||
SCSS
|
||||
|
||||
assert_renders <<SASS, <<SCSS
|
||||
// Foo
|
||||
Bar
|
||||
Baz
|
||||
=foo
|
||||
a: b
|
||||
SASS
|
||||
// Foo
|
||||
// Bar
|
||||
// Baz
|
||||
@mixin foo {
|
||||
a: b; }
|
||||
SCSS
|
||||
end
|
||||
|
||||
def test_debug
|
||||
assert_renders <<SASS, <<SCSS
|
||||
foo
|
||||
|
|
Loading…
Reference in a new issue