beautiful-css 0.0.1 → 0.0.3
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/lib/beautiful-css/engine.rb +34 -13
- data/lib/beautiful-css/rule.rb +5 -17
- data/lib/beautiful-css/rule_parser.rb +38 -0
- data/lib/beautiful-css/version.rb +1 -1
- data/spec/convertions_spec.rb +39 -1
- metadata +3 -2
data/lib/beautiful-css/engine.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require "beautiful-css/
|
1
|
+
require "beautiful-css/rule_parser"
|
2
2
|
require "sass"
|
3
3
|
|
4
4
|
module BeautifulCss
|
@@ -18,35 +18,56 @@ module BeautifulCss
|
|
18
18
|
|
19
19
|
def render
|
20
20
|
return nil if @input.nil?
|
21
|
+
rules = build_rules
|
22
|
+
cleaned = remove_unset rules
|
23
|
+
groups = build_groups(cleaned)
|
24
|
+
return format( groups )
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
private
|
21
29
|
|
30
|
+
def build_rules
|
22
31
|
text = scss_to_css @input
|
23
32
|
text = text.gsub( /[\n\r\t]/m, " " )
|
24
33
|
text = text.gsub( / +/m, " " )
|
25
34
|
text = text.gsub( /\/\*[^*]*\*\//m, " " )
|
26
35
|
rules = text.split('}')
|
36
|
+
rules = rules.map{|r| RuleParser.new(r).to_rules }.flatten
|
37
|
+
end
|
27
38
|
|
28
|
-
|
29
|
-
|
39
|
+
def remove_unset rules
|
30
40
|
hash = {}
|
31
|
-
|
32
|
-
##BUILD
|
33
41
|
rules.each do |r|
|
34
|
-
r.
|
35
|
-
|
36
|
-
|
42
|
+
hash[r.prop] = {} if !( hash.has_key? r.prop)
|
43
|
+
hash[r.prop][r.selector] = r.value
|
44
|
+
end
|
45
|
+
hash
|
46
|
+
end
|
47
|
+
|
48
|
+
def build_groups rules
|
49
|
+
groups = {}
|
50
|
+
rules.keys.each do |prop|
|
51
|
+
rules[prop].keys.each do |selector|
|
52
|
+
val = rules[prop][selector]
|
53
|
+
prop_val = "{ #{prop}:#{val} }"
|
54
|
+
groups[prop_val] = [] if !( rules.has_key? prop_val)
|
55
|
+
groups[prop_val] << selector
|
37
56
|
end
|
38
57
|
end
|
58
|
+
groups
|
59
|
+
end
|
39
60
|
|
40
|
-
|
41
|
-
output =
|
42
|
-
|
61
|
+
def format groups
|
62
|
+
output = ''
|
63
|
+
groups.keys.sort.each do |key|
|
43
64
|
output += "\n"
|
44
|
-
output +=
|
65
|
+
output += groups[key].uniq.join(",\n") + "\n"
|
45
66
|
output += key + "\n"
|
46
67
|
end
|
47
|
-
|
48
68
|
output.gsub( /: +/, ':' )
|
49
69
|
end
|
50
70
|
|
71
|
+
|
51
72
|
end
|
52
73
|
end
|
data/lib/beautiful-css/rule.rb
CHANGED
@@ -1,26 +1,14 @@
|
|
1
|
-
|
2
1
|
module BeautifulCss
|
3
|
-
|
4
2
|
class Rule
|
5
|
-
def initialize(str)
|
6
|
-
@body = str
|
7
|
-
end
|
8
3
|
|
9
|
-
|
10
|
-
@body.match(/[^{]*/).to_s.split(/,/).map{|s| s.strip}.select{|s| !s.empty? }
|
11
|
-
end
|
4
|
+
attr_accessor :selector, :prop, :value
|
12
5
|
|
13
|
-
def
|
14
|
-
|
15
|
-
p = @body.match(/\{([^}]*)/)[1].split(';').select{|s| !s.strip.empty? }
|
16
|
-
p.map{|s| "{ " + s.strip + " }"}
|
17
|
-
rescue
|
18
|
-
[]
|
19
|
-
end
|
6
|
+
def initialize(selector, prop, value)
|
7
|
+
@selector, @prop, @value = selector.strip, prop.strip, value.strip
|
20
8
|
end
|
21
9
|
|
22
|
-
def
|
23
|
-
@
|
10
|
+
def to_s
|
11
|
+
@selector + "\n" + "{ #{prop}:#{value} }"
|
24
12
|
end
|
25
13
|
|
26
14
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "beautiful-css/rule"
|
2
|
+
|
3
|
+
module BeautifulCss
|
4
|
+
class RuleParser
|
5
|
+
|
6
|
+
def initialize(str)
|
7
|
+
@body = str
|
8
|
+
end
|
9
|
+
|
10
|
+
def selectors
|
11
|
+
@body.match(/[^{]*/).to_s.split(/,/).map{|s| s.strip}.select{|s| !s.empty? }
|
12
|
+
end
|
13
|
+
|
14
|
+
def props
|
15
|
+
begin
|
16
|
+
p = @body.match(/\{([^}]*)/)[1].split(';').select{|s| !s.strip.empty? }
|
17
|
+
p.map{|s| s.strip.split(':') }
|
18
|
+
rescue
|
19
|
+
[]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_rules
|
24
|
+
selectors.map do |selector|
|
25
|
+
props.map do |prop|
|
26
|
+
BeautifulCss::Rule.new(selector, prop[0], prop[1])
|
27
|
+
end
|
28
|
+
end.flatten
|
29
|
+
end
|
30
|
+
|
31
|
+
def body
|
32
|
+
@body
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/spec/convertions_spec.rb
CHANGED
@@ -20,18 +20,26 @@ D
|
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
-
it '
|
23
|
+
it 'should work with three styles' do
|
24
24
|
dirty = <<DIRTY
|
25
25
|
a
|
26
26
|
{
|
27
27
|
color:red;
|
28
28
|
background-color:blue;
|
29
29
|
}
|
30
|
+
|
31
|
+
.classy
|
32
|
+
{
|
33
|
+
color:green;
|
34
|
+
}
|
30
35
|
DIRTY
|
31
36
|
clean = <<CLEAN
|
32
37
|
a
|
33
38
|
{ background-color:blue }
|
34
39
|
|
40
|
+
.classy
|
41
|
+
{ color:green }
|
42
|
+
|
35
43
|
a
|
36
44
|
{ color:red }
|
37
45
|
CLEAN
|
@@ -81,6 +89,36 @@ CLEAN
|
|
81
89
|
|
82
90
|
|
83
91
|
|
92
|
+
it 'should remove dups' do
|
93
|
+
dirty = <<DIRTY
|
94
|
+
a { color:red }
|
95
|
+
a { color:red }
|
96
|
+
DIRTY
|
97
|
+
clean = <<CLEAN
|
98
|
+
a
|
99
|
+
{ color:red }
|
100
|
+
CLEAN
|
101
|
+
assert_renders dirty, clean
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
it 'should removed unset values' do
|
108
|
+
dirty = <<DIRTY
|
109
|
+
a { color:green }
|
110
|
+
a { color:red }
|
111
|
+
DIRTY
|
112
|
+
clean = <<CLEAN
|
113
|
+
a
|
114
|
+
{ color:red }
|
115
|
+
CLEAN
|
116
|
+
assert_renders dirty, clean
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
84
122
|
|
85
123
|
|
86
124
|
def assert_renders(dirty,clean)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: beautiful-css
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
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: 2012-10-
|
12
|
+
date: 2012-10-02 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A tool to help cleanup css
|
15
15
|
email:
|
@@ -28,6 +28,7 @@ files:
|
|
28
28
|
- lib/beautiful-css/engine.rb
|
29
29
|
- lib/beautiful-css/exec.rb
|
30
30
|
- lib/beautiful-css/rule.rb
|
31
|
+
- lib/beautiful-css/rule_parser.rb
|
31
32
|
- lib/beautiful-css/version.rb
|
32
33
|
- spec/beautiful_css_spec.rb
|
33
34
|
- spec/convertions_spec.rb
|