Skip to content

Commit

Permalink
Merge pull request #188 from hannu/parse-related-vars
Browse files Browse the repository at this point in the history
Parse section related variables to styleguide.json
  • Loading branch information
Juuso Backman committed Nov 14, 2014
2 parents a7ac229 + bdc9bd3 commit ac93e18
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 108 deletions.
39 changes: 39 additions & 0 deletions lib/modules/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,45 @@ module.exports.parseVariables = function(string, syntax) {
return out;
};

// Parse Style variables to object
module.exports.findVariables = function(string, syntax) {
syntax = syntax || 'scss';

var out = [],
ast = gonzales.srcToAST({
src: string,
syntax: syntax
});

gonzo.traverse(ast, [{
// Visitor for SASS and SCSS syntaxes
test: function(name, nodes) {
return name === 'value';
},
process: function(nodes) {
nodes.forEach(function(element) {
if (element[0] === 'variable' && element[1][0] === 'ident') {
out.push(element[1][1]);
}
});
}
}, {
// Visitor for LESS syntax
test: function(name, nodes) {
return name === 'value';
},
process: function(nodes) {
nodes.forEach(function(element) {
if (element[0] === 'atkeyword' && element[1][0] === 'ident') {
out.push(element[1][1]);
}
});
}
}]);

return out;
};

// Modifies string so that variables passed in object are updated
module.exports.setVariables = function(string, syntax, variables) {
var sorted = [], lines = [],
Expand Down
13 changes: 11 additions & 2 deletions lib/styleguide.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,17 @@ module.exports = function(opt) {

// If settings file is found, generate settings object
if (opt.styleVariables) {
var syntax = path.extname(opt.styleVariables).substring(1);
var syntax = path.extname(opt.styleVariables).substring(1),
variables;
// Parse variables from the defined file
styleguide.config.settings = parser.parseVariables(fs.readFileSync(opt.styleVariables, 'utf-8'), syntax);
// Go trough every styleguide style block and find used variables
styleguide.sections.forEach(function(section) {
if (section.css) {
section.variables = parser.findVariables(section.css, syntax);
}
return section;
});
}

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

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

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"yargs": "^1.3.2"
},
"devDependencies": {
"async": "^0.9.0",
"chai": "^1.9.2",
"conventional-changelog": "git://github.com/sc5/conventional-changelog.git#features/sc-styleguide",
"exec-sync": "^0.1.6",
Expand All @@ -78,6 +79,7 @@
"karma-sinon-chai": "^0.2.0",
"main-bower-files": "^2.1.0",
"mocha": "^2.0.1",
"mocha-shared": "^0.2.0",
"multiline": "^1.0.1",
"sinon": "^1.11.1",
"sinon-chai": "^2.6.0",
Expand Down
1 change: 0 additions & 1 deletion test/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ describe('Markdown', function() {

it('getRenderer if formed correctly', function() {
var renderer = markdown.getRenderer();
console.log(renderer);
expect(renderer).to.be.an('object');
expect(renderer.heading).to.be.an('function');
expect(renderer.paragraph).to.be.an('function');
Expand Down
72 changes: 72 additions & 0 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,78 @@ var gulp = require('gulp'),
parser = require('../lib/modules/parser');

describe('Parser', function() {
describe('variable finding', function() {
describe('SCSS syntax', function() {
it('should return all used variables', function() {
var str = multiline(function() {
/*
color: $mycolor1;
.testStyle {
border: 1px solid $mycolor2;
}
.testStyle2 {
background-color: $mycolor3;
}
*/
}),
result = [
'mycolor1', 'mycolor2', 'mycolor3'
]
expect(parser.findVariables(str)).eql(result);
});

it('should not return new variable definitions', function() {
var str = multiline(function() {
/*
$mycolor: #00ff00;
.testStyle {
color: $mycolor2;
}
*/
}),
result = [
'mycolor2'
]
expect(parser.findVariables(str)).eql(result);
});
});

describe('LESS syntax', function() {
it('should return all used variables', function() {
var str = multiline(function() {
/*
color: @mycolor1;
.testStyle {
border: 1px solid @mycolor2;
}
.testStyle2 {
background-color: @mycolor3;
}
*/
}),
result = [
'mycolor1', 'mycolor2', 'mycolor3'
]
expect(parser.findVariables(str, 'less')).eql(result);
});

it('should not return new variable definitions', function() {
var str = multiline(function() {
/*
@mycolor: #00ff00;
.testStyle {
color: @mycolor2;
}
*/
}),
result = [
'mycolor2'
]
expect(parser.findVariables(str, 'less')).eql(result);
});
});
});

describe('variable parser', function() {
describe('SCSS syntax', function() {
it('should parse basic variables', function() {
Expand Down
19 changes: 19 additions & 0 deletions test/projects/less-project/source/styles/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,22 @@
// Styleguide 3.0

.test-css {color: purple;}

// Section with multiple variables
//
// Markup:
// <div class="section">Section markup</div>
//
// Styleguide 4.0

.section1 {
color: @color-red;
}

.section2 {
background-color: @color-green;
}

.section3 {
border: 1px solid @color-blue;
}
21 changes: 20 additions & 1 deletion test/projects/scss-project/source/styles/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,23 @@
//
// Styleguide 3.0

.test-css {color: purple;}
.test-css {color: purple;}

// Section with multiple variables
//
// Markup:
// <div class="section">Section markup</div>
//
// Styleguide 4.0

.section1 {
color: $color-red;
}

.section2 {
background-color: $color-green;
}

.section3 {
border: 1px solid $color-blue;
}

0 comments on commit ac93e18

Please sign in to comment.