1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

[Sass] Add basic support for a #to_scss function.

This commit is contained in:
Nathan Weizenbaum 2010-01-27 17:09:37 -08:00
parent f400448075
commit e6f28cb4f0
4 changed files with 53 additions and 5 deletions

View file

@ -193,6 +193,15 @@ module Sass
raise NotImplementedError.new("All static-node subclasses of Sass::Tree::Node must override #to_sass.")
end
# Converts a node to SCSS code that will generate it.
#
# @param tabs [Fixnum] The amount of tabulation to use for the SCSS code
# @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize})
# @return [String] The Sass code corresponding to the node
def to_scss(tabs = 0, opts = {})
raise NotImplementedError.new("All static-node subclasses of Sass::Tree::Node must override #to_scss.")
end
protected
# Computes the CSS corresponding to this particular Sass node.
@ -319,11 +328,16 @@ module Sass
# @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize})
# @return [String] The Sass code corresponding to the children
def children_to_sass(tabs, opts)
str = ""
children.each do |child|
str << "#{child.to_sass(tabs + 1, opts)}"
end
str
children.map {|c| c.to_sass(tabs + 1, opts)}.join
end
# Converts the children of this node to an SCSS string.
#
# @param tabs [Fixnum] The amount of tabulation to use for the SCSS code
# @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize})
# @return [String] The SCSS code corresponding to the children
def children_to_scss(tabs, opts)
children.map {|c| c.to_scss(tabs + 1, opts)}.join
end
end
end

View file

@ -103,6 +103,26 @@ module Sass::Tree
children_to_sass(tabs, opts)
end
# @see Node#to_scss
def to_scss(tabs, opts = {})
name = self.name.map {|n| n.is_a?(String) ? n : "\#{#{n.to_sass}}"}.join
if self.value.size == 1 && self.value.first.is_a?(Sass::Script::Node)
mid = '='
value = self.value.first.to_sass
else
mid = ':'
value = self.value.map do |n|
next n if n.is_a?(String)
"\#{#{n.to_sass}}"
end.join
end
value.gsub!(/\n[ \t]*/, "\n#{' ' * (tabs + 1)}")
"#{' ' * tabs}#{name}#{mid} #{value};\n" +
children_to_scss(tabs, opts)
end
protected
# Computes the CSS for the property.

View file

@ -52,6 +52,14 @@ module Sass
children.map {|child| child.to_sass(0, opts) + "\n"}.join.rstrip + "\n"
end
# Converts a node to SCSS code that will generate it.
#
# @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\n"}.join.rstrip + "\n"
end
protected
# Destructively converts this static Sass node into a static CSS node,

View file

@ -119,6 +119,12 @@ module Sass::Tree
"#{name.gsub(/^/, ' ' * tabs)}\n" + children_to_sass(tabs, opts)
end
def to_scss(tabs, opts = {})
name = rule.map {|r| r.is_a?(String) ? r : "\#{#{r.to_sass}}"}.
join.gsub(/^[ \t]*/, ' ' * tabs)
"#{name} {\n#{children_to_scss(tabs, opts).rstrip} }\n"
end
protected
# Computes the CSS for the rule.