Module: Stylish::Generate

Defined in:
lib/stylish/generate.rb

Overview

The Generate module is a general namespace for the stylesheet generation DSL components. It contains various modules and classes which together are used to generate the intermediate data-structures of selector trees, and ultimately CSS code.

Defined Under Namespace

Modules: ElementMethods Classes: Description, Variable

Class Method Summary collapse

Class Method Details

.parse_declarations(declarations) ⇒ Object

The parse_declarations method deals with three things: turning the hashes passed to the Description#rule method into Declarations objects; renaming property names to use dashes rather than underscores (since the former are correct CSS but are invalid Ruby, at least when quotation marks are not used to delimit the symbol); and handling special cases.

There are currently two main special cases: colours and backgrounds. Each of these property types have their own Stylish classes, and thus to create a rich datastructure which takes advantage of these powerful classes the declarations passed to the stylesheet generation DSL need to be parsed with this in mind.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/stylish/generate.rb', line 52

def self.parse_declarations(declarations)
  declarations.to_a.inject(Declarations.new) do |ds, declaration|
    key, value = declaration
    key        = key.to_s.sub("_", "-").to_sym
    
    if key == :background
      if value.any? {|k,v| v.is_a? Symbol }
        declaration = Variable.new(value, Background)
      else
        declaration = Background.new(value)
      end
    elsif key == :color
      if value.is_a? Symbol
        value = Variable.new(value, Color)
      else
        value = Color.new(value)
      end
      
      declaration = Declaration.new("color", value)
    else
      value = Variable.new(value) if value.is_a? Symbol
      declaration = Declaration.new(key, value)
    end
    
    ds << declaration
  end
end