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

249 lines
5.9 KiB
Ruby
Raw Normal View History

require File.dirname(__FILE__) + '/../sass'
require 'sass/tree/node'
require 'sass/scss/css_parser'
require 'strscan'
module Sass
2009-04-11 03:50:24 -07:00
# This class converts CSS documents into Sass templates.
# It works by parsing the CSS document into a {Sass::Tree} structure,
# and then applying various transformations to the structure
# to produce more concise and idiomatic Sass.
#
# Example usage:
#
# Sass::CSS.new("p { color: blue }").render #=> "p\n color: blue"
class CSS
# @param template [String] The CSS code
# @option options :old [Boolean] (false)
# Whether or not to output old property syntax
# (`:color blue` as opposed to `color: blue`).
# @option options :filename [String]
# The filename of the CSS file being processed.
# Used for error reporting
def initialize(template, options = {})
if template.is_a? IO
template = template.read
end
@options = options.dup
# Backwards compatibility
@options[:old] = true if @options[:alternate] == false
@template = template
end
2009-04-11 03:50:24 -07:00
# Converts the CSS template into Sass code.
#
# @return [String] The resulting Sass code
# @raise [Sass::SyntaxError] if there's an error parsing the CSS template
def render
Haml::Util.check_encoding(@template) do |msg, line|
raise Sass::SyntaxError.new(msg, :line => line)
end
build_tree.to_sass(0, @options).strip + "\n"
rescue Sass::SyntaxError => err
err.modify_backtrace(:filename => @options[:filename] || '(css)')
raise err
end
private
2009-04-11 03:50:24 -07:00
# Parses the CSS template and applies various transformations
#
# @return [Tree::Node] The root node of the parsed tree
def build_tree
root = Sass::SCSS::CssParser.new(@template).parse
2008-04-29 12:41:53 -07:00
expand_commas root
parent_ref_rules root
remove_parent_refs root
flatten_rules root
fold_commas root
root
end
# Transform
#
2009-04-11 03:50:24 -07:00
# foo, bar, baz
# color: blue
#
# into
#
2009-04-11 03:50:24 -07:00
# foo
# color: blue
# bar
# color: blue
# baz
# color: blue
#
# @param root [Tree::Node] The parent node
def expand_commas(root)
root.children.map! do |child|
next child unless Tree::RuleNode === child && child.rule.first.include?(',')
child.rule.first.split(',').map do |rule|
node = Tree::RuleNode.new([rule.strip])
node.children = child.children
node
end
end
root.children.flatten!
end
2008-04-29 12:41:53 -07:00
# Make rules use parent refs so that
#
2009-04-11 03:50:24 -07:00
# foo
# color: green
# foo.bar
# color: blue
2008-04-29 12:41:53 -07:00
#
# becomes
#
2009-04-11 03:50:24 -07:00
# foo
# color: green
# &.bar
# color: blue
2008-04-29 12:41:53 -07:00
#
# This has the side effect of nesting rules,
# so that
#
2009-04-11 03:50:24 -07:00
# foo
# color: green
# foo bar
# color: red
# foo baz
# color: blue
#
# becomes
#
2009-04-11 03:50:24 -07:00
# foo
# color: green
# & bar
# color: red
# & baz
# color: blue
2008-04-07 23:09:17 -07:00
#
# @param root [Tree::Node] The parent node
2008-04-29 12:41:53 -07:00
def parent_ref_rules(root)
current_rule = nil
root.children.select { |c| Tree::RuleNode === c }.each do |child|
root.children.delete child
first, rest = child.rule.first.scan(/^(&?(?: .|[^ ])[^.#: \[]*)([.#: \[].*)?$/).first
if current_rule.nil? || current_rule.rule.first != first
current_rule = Tree::RuleNode.new([first])
root << current_rule
end
if rest
child.rule = ["&" + rest]
current_rule << child
else
current_rule.children += child.children
end
end
root.children.each { |v| parent_ref_rules(v) }
end
2008-04-29 12:41:53 -07:00
# Remove useless parent refs so that
#
2009-04-11 03:50:24 -07:00
# foo
# & bar
# color: blue
2008-04-29 12:41:53 -07:00
#
# becomes
#
2009-04-11 03:50:24 -07:00
# foo
# bar
# color: blue
2008-04-29 12:41:53 -07:00
#
# @param root [Tree::Node] The parent node
2008-04-29 12:41:53 -07:00
def remove_parent_refs(root)
root.children.each do |child|
if child.is_a?(Tree::RuleNode)
child.rule.first.gsub! /^& +/, ''
2008-04-29 12:41:53 -07:00
remove_parent_refs child
end
end
end
# Flatten rules so that
#
2009-04-11 03:50:24 -07:00
# foo
# bar
# color: red
#
# becomes
#
2009-04-11 03:50:24 -07:00
# foo bar
# color: red
2008-04-07 23:09:17 -07:00
#
# and
#
2009-04-11 03:50:24 -07:00
# foo
# &.bar
# color: blue
#
# becomes
#
2009-04-11 03:50:24 -07:00
# foo.bar
# color: blue
#
# @param root [Tree::Node] The parent node
def flatten_rules(root)
root.children.each { |child| flatten_rule(child) if child.is_a?(Tree::RuleNode) }
end
2009-04-11 03:50:24 -07:00
# Flattens a single rule
#
# @param rule [Tree::RuleNode] The candidate for flattening
2009-04-11 03:50:24 -07:00
# @see #flatten_rules
def flatten_rule(rule)
while rule.children.size == 1 && rule.children.first.is_a?(Tree::RuleNode)
child = rule.children.first
if child.rule.first[0] == ?&
rule.rule = [child.rule.first.gsub(/^&/, rule.rule.first)]
else
rule.rule = ["#{rule.rule.first} #{child.rule.first}"]
end
rule.children = child.children
end
flatten_rules(rule)
end
# Transform
#
2009-04-11 03:50:24 -07:00
# foo
# bar
# color: blue
# baz
# color: blue
#
# into
#
2009-04-11 03:50:24 -07:00
# foo
# bar, baz
# color: blue
#
# @param rule [Tree::RuleNode] The candidate for flattening
def fold_commas(root)
prev_rule = nil
root.children.map! do |child|
next child unless child.is_a?(Tree::RuleNode)
if prev_rule && prev_rule.children == child.children
prev_rule.rule.first << ", #{child.rule.first}"
next nil
end
fold_commas(child)
prev_rule = child
child
end
root.children.compact!
end
end
end