css2less 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/ChangeLog.rdoc +6 -0
- data/lib/css2less.rb +23 -7
- data/lib/css2less/version.rb +1 -1
- data/spec/css2less_spec.rb +124 -0
- metadata +2 -2
data/ChangeLog.rdoc
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
=== 0.0.2 / 2013-02-03
|
2
|
+
|
3
|
+
* implement a new test to check if css @font-face rules are correctly handled
|
4
|
+
* fix: correctly handle css @import rules ( #10 )
|
5
|
+
* fix: correctly handle css child selectors ( #12 )
|
6
|
+
|
1
7
|
=== 0.0.1 / 2013-02-02
|
2
8
|
|
3
9
|
* Reformat the source code using Ruby conventions.
|
data/lib/css2less.rb
CHANGED
@@ -71,11 +71,23 @@ module Css2Less
|
|
71
71
|
def generate_tree
|
72
72
|
@css.split("\n").map { |l| l.strip }.join.gsub(/\/\*+[^\*]*\*+\//, '').split(/[\{\}]/).each_slice(2) do |style|
|
73
73
|
rules = style[0].strip
|
74
|
+
# handle child selector case - step1
|
75
|
+
if rules.include?('>')
|
76
|
+
rules = rules.gsub(/\s*>\s*/, ' &>')
|
77
|
+
end
|
78
|
+
if rules.include?("@import")
|
79
|
+
import_rule = rules.match(/@import.*;/)[0]
|
80
|
+
rules = rules.gsub(/@import.*;/, '')
|
81
|
+
add_rule(@tree, [], import_rule)
|
82
|
+
end
|
74
83
|
# leave multiple rules alone
|
75
84
|
if rules.include?(',')
|
76
85
|
add_rule(@tree, [rules], style[1])
|
77
86
|
else
|
78
|
-
|
87
|
+
rules_split = rules.split(/\s+/)
|
88
|
+
# handle child selector case - step2
|
89
|
+
rules_split.map! {|rule| rule.gsub('&>', '& > ')}
|
90
|
+
add_rule(@tree, rules_split, style[1])
|
79
91
|
end
|
80
92
|
end
|
81
93
|
end
|
@@ -85,13 +97,17 @@ module Css2Less
|
|
85
97
|
tree = @tree
|
86
98
|
end
|
87
99
|
tree.each do |element, children|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
@less = @less +
|
100
|
+
if element == :style
|
101
|
+
@less = @less + children.split(';').map { |s| s.strip }.reject { |s| s.empty? }.map { |s| s + ";" }.join("\n") + "\n"
|
102
|
+
else
|
103
|
+
@less = @less + ' ' * indent + element + " {\n"
|
104
|
+
style = children.delete(:style)
|
105
|
+
if style
|
106
|
+
@less = @less + style.split(';').map { |s| s.strip }.reject { |s| s.empty? }.map { |s| ' ' * (indent+4) + s + ";" }.join("\n") + "\n"
|
107
|
+
end
|
108
|
+
render_less(children, indent + 4)
|
109
|
+
@less = @less + ' ' * indent + "}\n"
|
92
110
|
end
|
93
|
-
render_less(children, indent + 4)
|
94
|
-
@less = @less + ' ' * indent + "}\n"
|
95
111
|
end
|
96
112
|
end
|
97
113
|
|
data/lib/css2less/version.rb
CHANGED
data/spec/css2less_spec.rb
CHANGED
@@ -6,3 +6,127 @@ describe Css2Less do
|
|
6
6
|
subject.const_get('VERSION').should_not be_empty
|
7
7
|
end
|
8
8
|
end
|
9
|
+
|
10
|
+
describe Css2Less::Converter do
|
11
|
+
it "should convert basic css structure into less structure" do
|
12
|
+
css = <<EOF
|
13
|
+
#hello {
|
14
|
+
color: blue;
|
15
|
+
}
|
16
|
+
|
17
|
+
#hello #buddy {
|
18
|
+
background: red;
|
19
|
+
}
|
20
|
+
EOF
|
21
|
+
less = <<EOF
|
22
|
+
#hello {
|
23
|
+
color: blue;
|
24
|
+
#buddy {
|
25
|
+
background: red;
|
26
|
+
}
|
27
|
+
}
|
28
|
+
EOF
|
29
|
+
converter = Css2Less::Converter.new(css)
|
30
|
+
converter.process_less
|
31
|
+
converter.get_less.should eq(less)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should correctly handle css child selectors" do
|
35
|
+
css = <<EOF
|
36
|
+
body .navbar .nav > li > a {
|
37
|
+
color: #333;
|
38
|
+
font-weight: bold;
|
39
|
+
}
|
40
|
+
EOF
|
41
|
+
less = <<EOF
|
42
|
+
body {
|
43
|
+
.navbar {
|
44
|
+
.nav {
|
45
|
+
& > li {
|
46
|
+
& > a {
|
47
|
+
color: #333;
|
48
|
+
font-weight: bold;
|
49
|
+
}
|
50
|
+
}
|
51
|
+
}
|
52
|
+
}
|
53
|
+
}
|
54
|
+
EOF
|
55
|
+
converter = Css2Less::Converter.new(css)
|
56
|
+
converter.process_less
|
57
|
+
converter.get_less.should eq(less)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should correctly handle css @import rules" do
|
61
|
+
css = <<EOF
|
62
|
+
@import "style1.css";
|
63
|
+
@import "style2.css";
|
64
|
+
|
65
|
+
@import "style3.css";
|
66
|
+
|
67
|
+
#hello {
|
68
|
+
color: blue;
|
69
|
+
}
|
70
|
+
|
71
|
+
@import "style4.css";
|
72
|
+
|
73
|
+
#hello #buddy {
|
74
|
+
background: red;
|
75
|
+
}
|
76
|
+
@import "style5.css";
|
77
|
+
EOF
|
78
|
+
less = <<EOF
|
79
|
+
@import "style1.css";
|
80
|
+
@import "style2.css";
|
81
|
+
@import "style3.css";
|
82
|
+
@import "style4.css";
|
83
|
+
@import "style5.css";
|
84
|
+
#hello {
|
85
|
+
color: blue;
|
86
|
+
#buddy {
|
87
|
+
background: red;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
EOF
|
91
|
+
converter = Css2Less::Converter.new(css)
|
92
|
+
converter.process_less
|
93
|
+
converter.get_less.should eq(less)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should correctly handle @font-face rules" do
|
97
|
+
css = <<EOF
|
98
|
+
@font-face {
|
99
|
+
font-family: 'MyWebFont';
|
100
|
+
src: url('webfont.eot'); /* IE9 Compat Modes */
|
101
|
+
src: url('webfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
|
102
|
+
url('webfont.woff') format('woff'), /* Modern Browsers */
|
103
|
+
url('webfont.ttf') format('truetype'), /* Safari, Android, iOS */
|
104
|
+
url('webfont.svg#svgFontName') format('svg'); /* Legacy iOS */
|
105
|
+
}
|
106
|
+
|
107
|
+
#hello {
|
108
|
+
color: blue;
|
109
|
+
}
|
110
|
+
|
111
|
+
#hello #buddy {
|
112
|
+
background: red;
|
113
|
+
}
|
114
|
+
EOF
|
115
|
+
less = <<EOF
|
116
|
+
@font-face {
|
117
|
+
font-family: 'MyWebFont';
|
118
|
+
src: url('webfont.eot');
|
119
|
+
src: url('webfont.eot?#iefix') format('embedded-opentype'), url('webfont.woff') format('woff'), url('webfont.ttf') format('truetype'), url('webfont.svg#svgFontName') format('svg');
|
120
|
+
}
|
121
|
+
#hello {
|
122
|
+
color: blue;
|
123
|
+
#buddy {
|
124
|
+
background: red;
|
125
|
+
}
|
126
|
+
}
|
127
|
+
EOF
|
128
|
+
converter = Css2Less::Converter.new(css)
|
129
|
+
converter.process_less
|
130
|
+
converter.get_less.should eq(less)
|
131
|
+
end
|
132
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: css2less
|
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:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-02-
|
13
|
+
date: 2013-02-03 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rdoc
|