vendor/plugins/haml/lib/sass/css.rb in radiant-0.7.2 vs vendor/plugins/haml/lib/sass/css.rb in radiant-0.8.0

- old
+ new

@@ -16,11 +16,11 @@ result end end class ValueNode - def to_sass(tabs) + def to_sass(tabs, opts = {}) "#{value}\n" end end class RuleNode @@ -38,10 +38,16 @@ class AttrNode def to_sass(tabs, opts = {}) "#{' ' * tabs}#{opts[:alternate] ? '' : ':'}#{name}#{opts[:alternate] ? ':' : ''} #{value}\n" end end + + class DirectiveNode + def to_sass(tabs, opts = {}) + "#{' ' * tabs}#{value}#{children.map {|c| c.to_sass(tabs + 1, opts)}}\n" + end + end end # This class is based on the Ruby 1.9 ordered hashes. # It keeps the semantics and most of the efficiency of normal hashes # while also keeping track of the order in which elements were set. @@ -116,11 +122,11 @@ # Processes the document and returns the result as a string # containing the CSS template. def render begin - build_tree.to_sass(@options).lstrip + build_tree.to_sass(@options).strip + "\n" rescue Exception => err line = @template.string[0...@template.pos].split("\n").size err.backtrace.unshift "(css):#{line}" raise err @@ -130,51 +136,46 @@ private def build_tree root = Tree::Node.new(nil) whitespace - directives root rules root expand_commas root parent_ref_rules root remove_parent_refs root flatten_rules root fold_commas root root end - def directives(root) - while @template.scan(/@/) - name = @template.scan /[^\s;]+/ + def rules(root) + while r = rule + root << r whitespace - value = @template.scan /[^;]+/ - assert_match /;/ - whitespace - - if name == "import" && value =~ /^(url\()?"?([^\s\(\)\"]+)\.css"?\)?$/ - value = $2 - end - - root << Tree::ValueNode.new("@#{name} #{value};", nil) end end - def rules(root) - rules = [] - while @template.scan(/[^\{\s]+/) - rules << @template[0] - whitespace + def rule + return unless rule = @template.scan(/[^\{\};]+/) + rule.strip! + directive = rule[0] == ?@ - if @template.scan(/\{/) - result = Tree::RuleNode.new(rules.join(' '), nil) - root << result - rules = [] + if directive + node = Tree::DirectiveNode.new(rule, nil) + return node if @template.scan(/;/) - whitespace - attributes(result) - end + assert_match /\{/ + whitespace + + rules(node) + return node end + + assert_match /\{/ + node = Tree::RuleNode.new(rule, nil) + attributes(node) + return node end def attributes(rule) while @template.scan(/[^:\}\s]+/) name = @template[0] @@ -234,11 +235,11 @@ # but it's necessary to get nesting to work properly. def expand_commas(root) root.children.map! do |child| next child unless Tree::RuleNode === child && child.rule.include?(',') child.rule.split(',').map do |rule| - node = Tree::RuleNode.new(rule, nil) + node = Tree::RuleNode.new(rule.strip, {}) node.children = child.children node end end root.children.flatten! @@ -308,11 +309,11 @@ # color: blue # def remove_parent_refs(root) root.children.each do |child| if child.is_a?(Tree::RuleNode) - child.rule.gsub! /^& /, '' + child.rule.gsub! /^& +/, '' remove_parent_refs child end end end @@ -374,10 +375,10 @@ # color: blue # def fold_commas(root) prev_rule = nil root.children.map! do |child| - next child unless Tree::RuleNode === child + next child unless child.is_a?(Tree::RuleNode) if prev_rule && prev_rule.children == child.children prev_rule.rule << ", #{child.rule}" next nil end