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:
parent
f400448075
commit
e6f28cb4f0
4 changed files with 53 additions and 5 deletions
|
@ -193,6 +193,15 @@ module Sass
|
||||||
raise NotImplementedError.new("All static-node subclasses of Sass::Tree::Node must override #to_sass.")
|
raise NotImplementedError.new("All static-node subclasses of Sass::Tree::Node must override #to_sass.")
|
||||||
end
|
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
|
protected
|
||||||
|
|
||||||
# Computes the CSS corresponding to this particular Sass node.
|
# 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})
|
# @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize})
|
||||||
# @return [String] The Sass code corresponding to the children
|
# @return [String] The Sass code corresponding to the children
|
||||||
def children_to_sass(tabs, opts)
|
def children_to_sass(tabs, opts)
|
||||||
str = ""
|
children.map {|c| c.to_sass(tabs + 1, opts)}.join
|
||||||
children.each do |child|
|
end
|
||||||
str << "#{child.to_sass(tabs + 1, opts)}"
|
|
||||||
end
|
# Converts the children of this node to an SCSS string.
|
||||||
str
|
#
|
||||||
|
# @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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -103,6 +103,26 @@ module Sass::Tree
|
||||||
children_to_sass(tabs, opts)
|
children_to_sass(tabs, opts)
|
||||||
end
|
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
|
protected
|
||||||
|
|
||||||
# Computes the CSS for the property.
|
# Computes the CSS for the property.
|
||||||
|
|
|
@ -52,6 +52,14 @@ module Sass
|
||||||
children.map {|child| child.to_sass(0, opts) + "\n"}.join.rstrip + "\n"
|
children.map {|child| child.to_sass(0, opts) + "\n"}.join.rstrip + "\n"
|
||||||
end
|
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
|
protected
|
||||||
|
|
||||||
# Destructively converts this static Sass node into a static CSS node,
|
# Destructively converts this static Sass node into a static CSS node,
|
||||||
|
|
|
@ -119,6 +119,12 @@ module Sass::Tree
|
||||||
"#{name.gsub(/^/, ' ' * tabs)}\n" + children_to_sass(tabs, opts)
|
"#{name.gsub(/^/, ' ' * tabs)}\n" + children_to_sass(tabs, opts)
|
||||||
end
|
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
|
protected
|
||||||
|
|
||||||
# Computes the CSS for the rule.
|
# Computes the CSS for the rule.
|
||||||
|
|
Loading…
Add table
Reference in a new issue