Class: Smurf::ColorVariableParser

Inherits:
Object
  • Object
show all
Defined in:
lib/smurf/color_variable_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sass_directory = Smurf.sass_directory) ⇒ ColorVariableParser

Returns a new instance of ColorVariableParser.



8
9
10
11
12
13
# File 'lib/smurf/color_variable_parser.rb', line 8

def initialize(sass_directory = Smurf.sass_directory)
  @colors            = {}
  @variable_mappings = {}
  @variable_usage    = {}
  @sass_directory    = sass_directory
end

Instance Attribute Details

#colorsObject

Returns the value of attribute colors.



6
7
8
# File 'lib/smurf/color_variable_parser.rb', line 6

def colors
  @colors
end

#sass_directoryObject

Returns the value of attribute sass_directory.



6
7
8
# File 'lib/smurf/color_variable_parser.rb', line 6

def sass_directory
  @sass_directory
end

#variable_mappingsObject

Returns the value of attribute variable_mappings.



6
7
8
# File 'lib/smurf/color_variable_parser.rb', line 6

def variable_mappings
  @variable_mappings
end

#variable_usageObject

Returns the value of attribute variable_usage.



6
7
8
# File 'lib/smurf/color_variable_parser.rb', line 6

def variable_usage
  @variable_usage
end

Class Method Details

.parse_color(color) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/smurf/color_variable_parser.rb', line 59

def self.parse_color(color)
  return false  unless color.is_a? String
  
  if color.include? "#"
    Color::RGB.from_html(color) rescue false
  elsif Color::CSS[color]
    Color::CSS[color]
  else
    false
  end
end

Instance Method Details

#parse_sass_directory(directory = sass_directory) ⇒ Object Also known as: parse



15
16
17
18
19
20
21
22
# File 'lib/smurf/color_variable_parser.rb', line 15

def parse_sass_directory(directory = sass_directory)
  Dir.glob("#{directory}/**/*").each do |file|
    if file.end_with?(".sass", ".scss")
      parse_sass_file(file)
      parse_variable_usage(file)
    end
  end
end

#parse_sass_file(file, options = {}) ⇒ Object

parses Sass file and returns hash with colors and variable_mappings (or false)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/smurf/color_variable_parser.rb', line 26

def parse_sass_file(file, options = {})
  Sass::Engine.for_file(file, options).to_tree.children.each do | node |
    if node.kind_of? Sass::Tree::VariableNode
      variable_node_parts = node.to_scss.split(":")
      variable_name       = variable_node_parts[0]
      value               = variable_node_parts[1].gsub(";", "").strip

      if color = Smurf::ColorVariableParser.parse_color(value)
        (self.colors[color.html] ||= []) << variable_name

      elsif value.start_with? "$"
        (self.variable_mappings[value] ||= []) << variable_name

      end
    end
  end
end

#parse_variable_usage(file) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/smurf/color_variable_parser.rb', line 44

def parse_variable_usage(file)
  grep_output = `grep -iG "\$" #{file}`
  grep_output.each_line do |line|
    matches = line.scan(/\$[\w-]*/)
    matches.each do |match|
      self.variable_usage[match] ||= 0
      self.variable_usage[match] += 1
    end
  end
end


55
56
57
# File 'lib/smurf/color_variable_parser.rb', line 55

def print_variable_usage_count_for(color)
  self.variable_usage[color] - 1  rescue 0
end