Class: LessReplacer

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

Constant Summary collapse

VAR_PATTERN =
/(@[a-z\-]+):\s*([^;]*);/
LENGTH_PATTERN =
/(\s+|:)([0-9\.]+(em|px|));/
COLOR_PATTERN =
/()(#[a-fA-F0-9]+);/

Instance Method Summary collapse

Constructor Details

#initializeLessReplacer

Returns a new instance of LessReplacer.



11
12
13
14
# File 'lib/lessFactor.rb', line 11

def initialize
  @vars = {}
  @literals = {}
end

Instance Method Details

#replace_vars(infile, outfile) ⇒ Object



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
# File 'lib/lessFactor.rb', line 43

def replace_vars(infile, outfile)
  result = File.open(infile, "r").readlines.each.map{|l|
    r1 = l.gsub(LENGTH_PATTERN) do |m|
      vn =$2.downcase
      if  @literals[vn]
        $1 + @literals[vn] + ';'
      else
        m
      end
    end

    r1.gsub(COLOR_PATTERN) do |m|
      vn =$2.downcase
      if  @literals[vn]
        $1 + @literals[vn] +';'
      else
        m
      end
    end
  }

  File.open(outfile, "w") do |f|
    f.puts(result.join())
  end
end

#save_vars(file) ⇒ Object



70
71
72
73
74
# File 'lib/lessFactor.rb', line 70

def save_vars(file)
  File.open(file, "w") do |f|
    @literals.sort{|a, b| a[1]<=> b[1] }.each{|value, name| f.puts "#{name}: #{value};" }
  end
end

#scan_literals(file) ⇒ Object



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

def scan_literals(file)
  File.open(file, "r").readlines.each do |l|
    l.match(LENGTH_PATTERN) do |m|
      value =$2.downcase
      unless @literals[value]
        @literals[value] = "@zz-length-" + "000#{@literals.keys.count}"[-3 .. -1]
      end
    end
    l.match(COLOR_PATTERN) do |m|
      value =$2.downcase
      unless @literals[value]
        @literals[value] = "@zz-color-" + "000#{@literals.keys.count}"[-3 .. -1]
      end
    end
  end
end

#scan_vars(file) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/lessFactor.rb', line 16

def scan_vars(file)
  File.open(file, "r").readlines.each do |l|
     l.match(VAR_PATTERN) do |m|
       value =$2.downcase
       @vars[$1] = value
       @literals[value] = $1
     end
  end
end