lessFactor 0.0.1 → 0.0.2
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/README.md +24 -6
- data/lib/lessFactor.rb +35 -16
- data/lib/lessFactor/version.rb +2 -1
- metadata +4 -4
data/README.md
CHANGED
@@ -6,10 +6,6 @@ mainly extracts variables. Variables are recognized by a pattern:
|
|
6
6
|
- length : number followed by 'px' or 'em'
|
7
7
|
- color : \#xxxxxx or \#xxx
|
8
8
|
|
9
|
-
It generates two new files:
|
10
|
-
|
11
|
-
<infile>.refactored.less - the refactored lessfile
|
12
|
-
<infile>.refactored_vars.less - the new variables file
|
13
9
|
|
14
10
|
## Installation
|
15
11
|
|
@@ -29,7 +25,13 @@ Or install it yourself as:
|
|
29
25
|
|
30
26
|
1. run
|
31
27
|
|
32
|
-
lessfactor
|
28
|
+
lessfactor varfile.less infile.less
|
29
|
+
|
30
|
+
It generates two new files:
|
31
|
+
|
32
|
+
<infile>.refactored.less - the refactored lessfile
|
33
|
+
<infile>.refactored_vars.less - the new variables file
|
34
|
+
|
33
35
|
|
34
36
|
2. identify the semantics of the variables, e.g. by comparing
|
35
37
|
'infile.refactored.less' with 'infile.less'
|
@@ -37,15 +39,31 @@ Or install it yourself as:
|
|
37
39
|
4. rerun lessfactor to get the new variable names
|
38
40
|
5. merge the refactored files back into your project
|
39
41
|
|
42
|
+
* find ambiguous variable names by "__@"
|
43
|
+
* investigate the highest occurrences first
|
44
|
+
|
40
45
|
## Known Issues
|
41
46
|
|
42
47
|
This is a very first shot. Areas of improvements:
|
43
48
|
|
44
49
|
* more robust parser
|
45
50
|
* option to ignore particular literals from being refactored - no idea how to achieve that
|
46
|
-
* do not
|
51
|
+
* do not create a new variables file but patch the existing one
|
47
52
|
* apply the same for sass
|
48
53
|
|
54
|
+
## version history
|
55
|
+
|
56
|
+
### 0.0.2
|
57
|
+
|
58
|
+
* now forward comments on the variables
|
59
|
+
* expose the lines of occurrence for extracted variables
|
60
|
+
* concatenate the variable names in case of ambiguity
|
61
|
+
* fixed usage screen
|
62
|
+
|
63
|
+
### 0.0.1
|
64
|
+
|
65
|
+
* initial version
|
66
|
+
|
49
67
|
## Contributing
|
50
68
|
|
51
69
|
1. Fork it
|
data/lib/lessFactor.rb
CHANGED
@@ -1,51 +1,61 @@
|
|
1
|
-
|
1
|
+
require "lessfactor/version"
|
2
2
|
|
3
3
|
|
4
4
|
class LessReplacer
|
5
5
|
|
6
|
-
VAR_PATTERN
|
6
|
+
VAR_PATTERN = /(@[a-z\-]+):\s*([^;]*);(.*)/
|
7
7
|
LENGTH_PATTERN = /(\s+|:)([0-9\.]+(em|px|));/
|
8
|
-
COLOR_PATTERN
|
8
|
+
COLOR_PATTERN = /()(#[a-fA-F0-9]+);/
|
9
9
|
|
10
10
|
|
11
11
|
def initialize
|
12
12
|
@vars = {}
|
13
13
|
@literals = {}
|
14
|
+
@literalcomments = {}
|
15
|
+
@occurence = {}
|
14
16
|
end
|
15
17
|
|
16
18
|
def scan_vars(file)
|
17
19
|
File.open(file, "r").readlines.each do |l|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
l.match(VAR_PATTERN) do |m|
|
21
|
+
value =$2.downcase
|
22
|
+
@vars[$1] = {v: value, c: $3}
|
23
|
+
(@literals[value] ||= []).push $1
|
24
|
+
end
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
26
28
|
def scan_literals(file)
|
29
|
+
line = 0
|
27
30
|
File.open(file, "r").readlines.each do |l|
|
31
|
+
|
32
|
+
line +=1
|
33
|
+
|
28
34
|
l.match(LENGTH_PATTERN) do |m|
|
29
35
|
value =$2.downcase
|
30
36
|
unless @literals[value]
|
31
|
-
@literals[value]
|
37
|
+
(@literals[value] ||= []).push "@zz-size-" + "000#{@literals.keys.count}"[-3 .. -1]
|
32
38
|
end
|
39
|
+
(@literalcomments[value] ||= []).push line
|
33
40
|
end
|
41
|
+
|
34
42
|
l.match(COLOR_PATTERN) do |m|
|
35
43
|
value =$2.downcase
|
36
44
|
unless @literals[value]
|
37
|
-
@literals[value]
|
45
|
+
(@literals[value] ||= []).push "@zz-color-" + "000#{@literals.keys.count}"[-3 .. -1]
|
38
46
|
end
|
47
|
+
(@literalcomments[value] ||= []).push line
|
39
48
|
end
|
40
49
|
end
|
41
50
|
end
|
42
51
|
|
43
52
|
def replace_vars(infile, outfile)
|
44
|
-
result = File.open(infile, "r").readlines.each.map{|l|
|
53
|
+
result = File.open(infile, "r").readlines.each.map { |l|
|
45
54
|
r1 = l.gsub(LENGTH_PATTERN) do |m|
|
46
55
|
vn =$2.downcase
|
47
56
|
if @literals[vn]
|
48
|
-
|
57
|
+
name = @literals[vn].join("__")
|
58
|
+
$1 + name +';'
|
49
59
|
else
|
50
60
|
m
|
51
61
|
end
|
@@ -54,7 +64,8 @@ class LessReplacer
|
|
54
64
|
r1.gsub(COLOR_PATTERN) do |m|
|
55
65
|
vn =$2.downcase
|
56
66
|
if @literals[vn]
|
57
|
-
|
67
|
+
name = @literals[vn].join("__")
|
68
|
+
$1 + name +';'
|
58
69
|
else
|
59
70
|
m
|
60
71
|
end
|
@@ -69,14 +80,22 @@ class LessReplacer
|
|
69
80
|
|
70
81
|
def save_vars(file)
|
71
82
|
File.open(file, "w") do |f|
|
72
|
-
@literals.sort{|a, b| a[1]<=> b[1] }.each{|value, name|
|
83
|
+
@literals.sort { |a, b| a[1] <=> b[1] }.each { |value, name|
|
84
|
+
the_name = name.first
|
85
|
+
occurrences = @literalcomments[value] || []
|
86
|
+
comment = @vars[the_name] || {c: " // todo: #{occurrences.count}: #{occurrences}"}
|
87
|
+
f.printf "%-25s %-15s %s\n", "#{the_name}:", " #{value};", comment[:c]
|
88
|
+
}
|
73
89
|
end
|
74
90
|
end
|
75
91
|
end
|
76
92
|
|
77
93
|
unless ARGV[1]
|
78
|
-
puts %
|
79
|
-
|
94
|
+
puts %Q{
|
95
|
+
|
96
|
+
this is lessfactor #{LessFactor::VERSION} (#{LessFactor::HOMEPAGE_URL})
|
97
|
+
|
98
|
+
usage: lessfactor <variablesfile> <infile>
|
80
99
|
|
81
100
|
creates
|
82
101
|
|
data/lib/lessFactor/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lessFactor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-08-
|
12
|
+
date: 2014-08-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -77,7 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
77
|
version: '0'
|
78
78
|
segments:
|
79
79
|
- 0
|
80
|
-
hash: -
|
80
|
+
hash: -726695259881330046
|
81
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
82
|
none: false
|
83
83
|
requirements:
|
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
86
|
version: '0'
|
87
87
|
segments:
|
88
88
|
- 0
|
89
|
-
hash: -
|
89
|
+
hash: -726695259881330046
|
90
90
|
requirements: []
|
91
91
|
rubyforge_project:
|
92
92
|
rubygems_version: 1.8.25
|