Class: Smurfville::ColorVariableParser
- Inherits:
-
Object
- Object
- Smurfville::ColorVariableParser
- Defined in:
- lib/smurfville/color_variable_parser.rb
Instance Attribute Summary collapse
-
#colors ⇒ Object
Returns the value of attribute colors.
-
#sass_directory ⇒ Object
Returns the value of attribute sass_directory.
-
#sass_env ⇒ Object
Returns the value of attribute sass_env.
-
#variable_mappings ⇒ Object
Returns the value of attribute variable_mappings.
-
#variable_usage ⇒ Object
Returns the value of attribute variable_usage.
Instance Method Summary collapse
-
#add_color(node, key = nil) ⇒ Object
add found color to @colors.
-
#initialize(sass_directory = Smurfville.sass_directory) ⇒ ColorVariableParser
constructor
A new instance of ColorVariableParser.
- #parse_sass_directory(directory = sass_directory) ⇒ Object (also: #parse)
-
#parse_sass_file(file, options = {}) ⇒ Object
parses Sass file and returns hash with colors and variable_mappings (or false).
-
#parse_variable_usage(file) ⇒ Object
count the usages of all the Sass variables.
- #print_variable_usage_count_for(color) ⇒ Object
Constructor Details
#initialize(sass_directory = Smurfville.sass_directory) ⇒ ColorVariableParser
Returns a new instance of ColorVariableParser.
8 9 10 11 12 13 14 |
# File 'lib/smurfville/color_variable_parser.rb', line 8 def initialize(sass_directory = Smurfville.sass_directory) @colors = {} @variable_mappings = {} # e.g. { '$white' => { '$light-color', '$light-background-color' }, $black => {...} } @variable_usage = {} # e.g. { 'white' => { :variables => ['light-color', 'text-color'] } :alternate_values => ['#fff', '#ffffff']} @sass_directory = sass_directory @sass_env = Sass::Environment.new end |
Instance Attribute Details
#colors ⇒ Object
Returns the value of attribute colors.
6 7 8 |
# File 'lib/smurfville/color_variable_parser.rb', line 6 def colors @colors end |
#sass_directory ⇒ Object
Returns the value of attribute sass_directory.
6 7 8 |
# File 'lib/smurfville/color_variable_parser.rb', line 6 def sass_directory @sass_directory end |
#sass_env ⇒ Object
Returns the value of attribute sass_env.
6 7 8 |
# File 'lib/smurfville/color_variable_parser.rb', line 6 def sass_env @sass_env end |
#variable_mappings ⇒ Object
Returns the value of attribute variable_mappings.
6 7 8 |
# File 'lib/smurfville/color_variable_parser.rb', line 6 def variable_mappings @variable_mappings end |
#variable_usage ⇒ Object
Returns the value of attribute variable_usage.
6 7 8 |
# File 'lib/smurfville/color_variable_parser.rb', line 6 def variable_usage @variable_usage end |
Instance Method Details
#add_color(node, key = nil) ⇒ Object
add found color to @colors
49 50 51 52 53 54 55 56 |
# File 'lib/smurfville/color_variable_parser.rb', line 49 def add_color(node, key = nil) key ||= node.expr.to_s self.colors[key] ||= { :variables => [], :alternate_values => [] } self.colors[key][:variables] << node.name self.colors[key][:alternate_values] |= ([node.expr.to_sass, node.expr.inspect] - [key]) end |
#parse_sass_directory(directory = sass_directory) ⇒ Object Also known as: parse
16 17 18 19 20 21 22 23 |
# File 'lib/smurfville/color_variable_parser.rb', line 16 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)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/smurfville/color_variable_parser.rb', line 27 def parse_sass_file(file, = {}) Sass::Engine.for_file(file, ).to_tree.children.each do |node| if node.kind_of? Sass::Tree::VariableNode if node.expr.is_a? Sass::Script::Color add_color(node) elsif node.expr.is_a? Sass::Script::Funcall # if function call (e.g. lighten(#000, 20%)), try to resolve to color resolved = node.expr.perform(sass_env) rescue false if resolved.is_a? Sass::Script::Color add_color(node, resolved.to_s) end elsif node.expr.is_a? Sass::Script::Variable (self.variable_mappings[node.expr.name] ||= []) << node.name # add to variable mapping end end end end |
#parse_variable_usage(file) ⇒ Object
count the usages of all the Sass variables
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/smurfville/color_variable_parser.rb', line 59 def parse_variable_usage(file) grep_output = `grep -iG "\\$" #{file}` grep_output.each_line do |line| matches = line.scan(/\$[\w-]*/) matches.each do |match| match.gsub!('$', '') self.variable_usage[match] ||= 0 self.variable_usage[match] += 1 end end end |
#print_variable_usage_count_for(color) ⇒ Object
71 72 73 |
# File 'lib/smurfville/color_variable_parser.rb', line 71 def print_variable_usage_count_for(color) self.variable_usage[color] - 1 rescue 0 end |