cloudhead-less 0.8.3 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.3
1
+ 0.8.4
data/less.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{less}
5
- s.version = "0.8.3"
5
+ s.version = "0.8.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["cloudhead"]
data/lib/less/engine.rb CHANGED
@@ -52,13 +52,13 @@ module Less
52
52
  # Call `evaluate` on variables, such as '@dark: @light / 2'
53
53
  @tree = @tree.traverse :branch do |path, node|
54
54
  node.vars.each do |key, value|
55
- evaluate key, value, node.vars
55
+ evaluate key, value, path, node.vars
56
56
  end if node.vars?
57
57
  end
58
58
 
59
59
  # Call `evaluate` on css properties, such as 'font-size: @big'
60
60
  @tree = @tree.traverse :leaf do |key, value, path, node|
61
- evaluate key, value, node
61
+ evaluate key, value, path, node
62
62
  end
63
63
 
64
64
  #
@@ -95,14 +95,14 @@ module Less
95
95
  #
96
96
  # Evaluate variables
97
97
  #
98
- def evaluate key, value, node
99
- if value.is_a? String and value.include? '@' # There's a var to evaluate
100
- value.scan REGEX[:path] do |p|
101
- p = p.join.delete ' '
102
- var = if p.include? '>'
103
- @tree.find :var, p.split('>') # Try finding it in a specific namespace
104
- else
105
- node.var( p ) || @tree.var( p ) # Try local first, then global
98
+ def evaluate key, value, path, node
99
+ if value.is_a? String and value.include? '@' # There's a var to evaluate
100
+ value.scan REGEX[:path] do |var|
101
+ var = var.join.delete ' '
102
+ var = if var.include? '>'
103
+ @tree.find :var, var.split('>') # Try finding it in a specific namespace
104
+ else
105
+ node.var( var ) or @tree.nearest var, path # Try local first, then nearest scope
106
106
  end
107
107
 
108
108
  if var
data/lib/less/tree.rb CHANGED
@@ -16,6 +16,9 @@ module Less
16
16
  self[:variables] ? self[:variables] : nil
17
17
  end
18
18
 
19
+ #
20
+ # Find a variable or mixin from a specific path
21
+ #
19
22
  def find what = :var, path = []
20
23
  path.inject(self) do |branch, k|
21
24
  if what == :var && k == path.last
@@ -26,40 +29,52 @@ module Less
26
29
  end
27
30
  end
28
31
 
32
+ #
33
+ # Find the nearest variable in the hierarchy
34
+ #
35
+ def nearest var, path
36
+ values = []
37
+ path.inject(self) do |branch, k|
38
+ values << branch.var( var )
39
+ branch[ k ]
40
+ end
41
+ values.compact.last
42
+ end
43
+
29
44
  def traverse by = :leaf, path = [], &blk
30
45
  #
31
46
  # Traverse the whole tree, returning each leaf or branch (recursive)
32
47
  #
33
48
  ###
34
- # Aside from the key & value,
35
- # we yield the full path of the leaf, aswell as
36
- # the branch which contains it (self).
49
+ # Aside from the key & value, we yield the full path of the leaf,
50
+ # aswell as the branch which contains it (self).
51
+ # Note that in :branch mode, we only return 'twigs', branches which contain leaves.
37
52
  #
38
53
  self.each do |key, value| # `self` is the current node, starting with the trunk
39
54
  if value.is_a? Hash and key != :variables # If the node is a branch, we can go deeper
40
55
  path << key # Add the current branch to the path
41
56
  self[ key ] = value.to_tree. # Make sure any change is saved to the main tree
42
- traverse by, path, &blk # Recurse, with the current node becoming `self`
43
- if by == :branch
44
- yield path, self[ key ] # The node is a branch, yield it to the block
45
- path.pop
46
- end
57
+ traverse by, path, &blk # Recurse, with the current node becoming `self`
58
+ yield path, self[ key ] if by == :branch # The node is a branch, yield it to the block
59
+ path.pop # We're returning from a branch, pop the last element
47
60
  elsif by == :leaf and key.is_a? String
48
61
  yield key, value, path, self # The node is a leaf, yield it to the block
49
- path.pop
50
62
  else
51
63
  next
52
64
  end
53
65
  end
54
66
  self
55
67
  end
56
-
68
+
69
+ #
70
+ # Convert the tree to css, using full paths
71
+ #
57
72
  def to_css css = []
58
73
  self.traverse :branch do |path, node|
59
74
  properties = node.inject("") do |s, (k, v)|
60
75
  v.is_a?(String) ? (s + "#{k}: #{CGI.unescape(v)}; ") : s # Add the property to the list
61
76
  end
62
- css << path * ' > ' + " { " + properties + "}" # Add the rule-set to the CSS
77
+ css << path * ' > ' + " { " + properties + "}" # Add the rule-set to the CSS
63
78
  end
64
79
  css.join "\n"
65
80
  end
data/spec/spec.less CHANGED
@@ -46,7 +46,9 @@
46
46
  .second {
47
47
  @color: blue;
48
48
  .third {
49
- color: @main-color; // #444
49
+ background-color: @color; // blue
50
+ font-size: @size; // 8px
51
+ color: @main-color; // red
50
52
  border-color: .root > .first > .second > @color; // 'blue'
51
53
  }
52
54
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudhead-less
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - cloudhead