Class: SCSSLint::AutoCorrect::Correctors::ColorKeyword

Inherits:
Base
  • Object
show all
Defined in:
lib/scss_lint/auto_correct/correctors/color_keyword.rb

Instance Attribute Summary

Attributes inherited from Base

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#enabled?, #initialize, linter_name, short_name

Constructor Details

This class inherits a constructor from SCSSLint::AutoCorrect::Correctors::Base

Class Method Details

.dataObject



30
31
32
33
34
# File 'lib/scss_lint/auto_correct/correctors/color_keyword.rb', line 30

def self.data
  return @data if @data
  colors_file = File.expand_path(File.join(File.dirname(__FILE__), "color-keywords.csv"))
  @data = Hash[File.read(colors_file).lines.map { |l| l.gsub("\n", '').split(',') }]
end

.descriptionObject



36
37
38
# File 'lib/scss_lint/auto_correct/correctors/color_keyword.rb', line 36

def self.description
  "Replace named colors with their hex values"
end

.priorityObject



40
41
42
# File 'lib/scss_lint/auto_correct/correctors/color_keyword.rb', line 40

def self.priority
  5
end

Instance Method Details

#call(contents) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/scss_lint/auto_correct/correctors/color_keyword.rb', line 3

def call(contents)
  result = []
  contents.lines.each do |line|
    if line =~ /\s*\/\// # skip comments
      result << line
      next
    end
    mm = line.match(/(.+:)([\s\S]*)$/)
    if mm
      result << mm[1] + correct_values(mm[2])
    else
      result << line
    end
  end
  result.join
end

#correct_values(str) ⇒ Object



20
21
22
23
24
# File 'lib/scss_lint/auto_correct/correctors/color_keyword.rb', line 20

def correct_values(str)
  str.gsub(regexp) do |color|
    self.class.data[color]
  end
end

#regexpObject



26
27
28
# File 'lib/scss_lint/auto_correct/correctors/color_keyword.rb', line 26

def regexp
  @regexp ||= Regexp.new("(?<![$_-])\\b(#{self.class.data.keys.join('|')})\\b")
end