Class: Less::Engine

Inherits:
String
  • Object
show all
Defined in:
lib/less/engine.rb

Constant Summary collapse

REGEXP =
{
  :path => /([#.][->#.\w]+)?( ?> ?)?@([\w\-]+)/, # #header > .title > @var
  :selector => /[-\w #.>*_:]/,                   # .cow .milk > a
  :values => /[-\w @>\/*+#%.,'"]/,               # 10px solid #fff
  :variable => /^@([\w\-]+)/                     # @milk-white
}

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ Engine

Returns a new instance of Engine.



10
11
12
13
# File 'lib/less/engine.rb', line 10

def initialize s
  super
  @tree = Tree.new self.hashify
end

Instance Method Details

#compileObject Also known as: render



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
79
80
# File 'lib/less/engine.rb', line 15

def compile     
  #
  # Parse the variables and mixins
  #
  # We use symbolic keys, such as :mixins, to store LESS-only data,
  # each branch has its own :mixins => [], and :variables => {}
  # Once a declaration has been recognised as LESS-specific, it is copied 
  # in the appropriate data structure in that branch. The declaration itself
  # can then be deleted.
  #
  @tree = @tree.traverse :leaf do |key, value, path, node|
    matched = if match = key.match( REGEXP[:variable] )
      node[:variables] ||= {}
      node[:variables][ match.captures.first ] = value
    elsif value == :mixin
      node[:mixins] ||= []          
      node[:mixins] << key
    end
    node.delete key if matched # Delete the property if it's LESS-specific
  end
  
  #
  # Evaluate mixins
  #
  @tree = @tree.traverse :branch do |path, node|
    if node.include? :mixins
      node[:mixins].each do |m|
        @tree.find( :mixin, m.delete(' ').split('>') ).each {|k, v| node[ k ] = v }
      end
    end
  end
  
  #
  # Evaluate the variables
  #
  @tree = @tree.traverse :leaf do |key, value, path, node|
    convert = lambda do |key, value, node| # We declare this as a lambda, for re-use
      if value.is_a?(String) && value.include?('@') # There's a var to evaluate        
        
        # Find its value
        var = value.delete(' ').match( REGEXP[:path] ).captures.join  
        var = unless var.include? '>'
          node.var( var ) || @tree.var( var ) # Try local first, then global
        else
          @tree.find :var, var.split('>')           # Try finding it in a specific namespace
        end
      
        if var
          node[ key ] = value.gsub REGEXP[:path], var # substitute variable with value
        else
          node.delete key # discard the declaration if the variable wasn't found
        end
      end
    end
    
    # Call `convert` on css properties, such as 'font-size: @big'
    convert.call key, value, node
    
    # Call `convert` on variables, such as '@dark: @light / 2'
    if node.vars? 
      node.vars.each do |key, value|
        convert.call key, value, node
      end
    end
  end            
end

#evaluate(s) ⇒ Object



87
88
89
# File 'lib/less/engine.rb', line 87

def evaluate s
  eval( s )
end

#hashifyObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/less/engine.rb', line 91

def hashify
#
# Parse the LESS structure into a hash
#
###
  #   less:     color: black;
  #   hashify: "color" => "black"
  #
  hash = self.gsub(/"/, "'").                                                         # ""
              gsub(/([@a-z\-]+):[ \t]*(#{ REGEXP[:values] }+);/, '"\\1" => "\\2",').  # Properties
              gsub(/\}/, "},").                                                       # Closing }
              gsub(/([ \t]*)(#{ REGEXP[:selector] }+?)[ \t\n]*\{/m, '\\1"\\2" => {'). # Selectors
              gsub(/([.#][->\w .#]+);/, '"\\1" => :mixin,').                          # Mixins
              gsub("\n\n", "\n").                                                     # New-lines
              gsub(/\/\/.*\n/, '')                                                    # Comments
  eval "{" + hash + "}"                                                               # Return {hash}
end

#to_cssObject



83
84
85
# File 'lib/less/engine.rb', line 83

def to_css
  self.compile.to_css
end