Skip to content

Commit bdc9bd3

Browse files
author
Hannu Pelkonen
committed
Parse section related variables to styleguide.json
1 parent a4112e8 commit bdc9bd3

File tree

8 files changed

+317
-108
lines changed

8 files changed

+317
-108
lines changed

lib/modules/parser.js

+39
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,45 @@ module.exports.parseVariables = function(string, syntax) {
7575
return out;
7676
};
7777

78+
// Parse Style variables to object
79+
module.exports.findVariables = function(string, syntax) {
80+
syntax = syntax || 'scss';
81+
82+
var out = [],
83+
ast = gonzales.srcToAST({
84+
src: string,
85+
syntax: syntax
86+
});
87+
88+
gonzo.traverse(ast, [{
89+
// Visitor for SASS and SCSS syntaxes
90+
test: function(name, nodes) {
91+
return name === 'value';
92+
},
93+
process: function(nodes) {
94+
nodes.forEach(function(element) {
95+
if (element[0] === 'variable' && element[1][0] === 'ident') {
96+
out.push(element[1][1]);
97+
}
98+
});
99+
}
100+
}, {
101+
// Visitor for LESS syntax
102+
test: function(name, nodes) {
103+
return name === 'value';
104+
},
105+
process: function(nodes) {
106+
nodes.forEach(function(element) {
107+
if (element[0] === 'atkeyword' && element[1][0] === 'ident') {
108+
out.push(element[1][1]);
109+
}
110+
});
111+
}
112+
}]);
113+
114+
return out;
115+
};
116+
78117
// Modifies string so that variables passed in object are updated
79118
module.exports.setVariables = function(string, syntax, variables) {
80119
var sorted = [], lines = [],

lib/styleguide.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,17 @@ module.exports = function(opt) {
122122

123123
// If settings file is found, generate settings object
124124
if (opt.styleVariables) {
125-
var syntax = path.extname(opt.styleVariables).substring(1);
125+
var syntax = path.extname(opt.styleVariables).substring(1),
126+
variables;
127+
// Parse variables from the defined file
126128
styleguide.config.settings = parser.parseVariables(fs.readFileSync(opt.styleVariables, 'utf-8'), syntax);
129+
// Go trough every styleguide style block and find used variables
130+
styleguide.sections.forEach(function(section) {
131+
if (section.css) {
132+
section.variables = parser.findVariables(section.css, syntax);
133+
}
134+
return section;
135+
});
127136
}
128137

129138
// Create JSON containing KSS data
@@ -142,8 +151,8 @@ module.exports = function(opt) {
142151
.pipe(pushAllFiles())
143152
}
144153

145-
opt.onCompileError = onCompileError;
146154
// Preprocess all CSS files and combile then to a single file
155+
opt.onCompileError = onCompileError;
147156
preprocess.getStream(Object.keys(filesBuffer), opt)
148157
.pipe(through.obj(function(file, enc, cb) {
149158

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"yargs": "^1.3.2"
6161
},
6262
"devDependencies": {
63+
"async": "^0.9.0",
6364
"chai": "^1.9.2",
6465
"conventional-changelog": "git://github.com/sc5/conventional-changelog.git#features/sc-styleguide",
6566
"exec-sync": "^0.1.6",
@@ -78,6 +79,7 @@
7879
"karma-sinon-chai": "^0.2.0",
7980
"main-bower-files": "^2.1.0",
8081
"mocha": "^2.0.1",
82+
"mocha-shared": "^0.2.0",
8183
"multiline": "^1.0.1",
8284
"sinon": "^1.11.1",
8385
"sinon-chai": "^2.6.0",

test/markdown.js

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ describe('Markdown', function() {
88

99
it('getRenderer if formed correctly', function() {
1010
var renderer = markdown.getRenderer();
11-
console.log(renderer);
1211
expect(renderer).to.be.an('object');
1312
expect(renderer.heading).to.be.an('function');
1413
expect(renderer.paragraph).to.be.an('function');

test/parser.js

+72
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,78 @@ var gulp = require('gulp'),
55
parser = require('../lib/modules/parser');
66

77
describe('Parser', function() {
8+
describe('variable finding', function() {
9+
describe('SCSS syntax', function() {
10+
it('should return all used variables', function() {
11+
var str = multiline(function() {
12+
/*
13+
color: $mycolor1;
14+
.testStyle {
15+
border: 1px solid $mycolor2;
16+
}
17+
.testStyle2 {
18+
background-color: $mycolor3;
19+
}
20+
*/
21+
}),
22+
result = [
23+
'mycolor1', 'mycolor2', 'mycolor3'
24+
]
25+
expect(parser.findVariables(str)).eql(result);
26+
});
27+
28+
it('should not return new variable definitions', function() {
29+
var str = multiline(function() {
30+
/*
31+
$mycolor: #00ff00;
32+
.testStyle {
33+
color: $mycolor2;
34+
}
35+
*/
36+
}),
37+
result = [
38+
'mycolor2'
39+
]
40+
expect(parser.findVariables(str)).eql(result);
41+
});
42+
});
43+
44+
describe('LESS syntax', function() {
45+
it('should return all used variables', function() {
46+
var str = multiline(function() {
47+
/*
48+
color: @mycolor1;
49+
.testStyle {
50+
border: 1px solid @mycolor2;
51+
}
52+
.testStyle2 {
53+
background-color: @mycolor3;
54+
}
55+
*/
56+
}),
57+
result = [
58+
'mycolor1', 'mycolor2', 'mycolor3'
59+
]
60+
expect(parser.findVariables(str, 'less')).eql(result);
61+
});
62+
63+
it('should not return new variable definitions', function() {
64+
var str = multiline(function() {
65+
/*
66+
@mycolor: #00ff00;
67+
.testStyle {
68+
color: @mycolor2;
69+
}
70+
*/
71+
}),
72+
result = [
73+
'mycolor2'
74+
]
75+
expect(parser.findVariables(str, 'less')).eql(result);
76+
});
77+
});
78+
});
79+
880
describe('variable parser', function() {
981
describe('SCSS syntax', function() {
1082
it('should parse basic variables', function() {

test/projects/less-project/source/styles/style.less

+19
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,22 @@
2727
// Styleguide 3.0
2828

2929
.test-css {color: purple;}
30+
31+
// Section with multiple variables
32+
//
33+
// Markup:
34+
// <div class="section">Section markup</div>
35+
//
36+
// Styleguide 4.0
37+
38+
.section1 {
39+
color: @color-red;
40+
}
41+
42+
.section2 {
43+
background-color: @color-green;
44+
}
45+
46+
.section3 {
47+
border: 1px solid @color-blue;
48+
}

test/projects/scss-project/source/styles/style.scss

+20-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,23 @@
2626
//
2727
// Styleguide 3.0
2828

29-
.test-css {color: purple;}
29+
.test-css {color: purple;}
30+
31+
// Section with multiple variables
32+
//
33+
// Markup:
34+
// <div class="section">Section markup</div>
35+
//
36+
// Styleguide 4.0
37+
38+
.section1 {
39+
color: $color-red;
40+
}
41+
42+
.section2 {
43+
background-color: $color-green;
44+
}
45+
46+
.section3 {
47+
border: 1px solid $color-blue;
48+
}

0 commit comments

Comments
 (0)