-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/JadeParser' into develop
- Loading branch information
Showing
5 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/*! | ||
* bewbem | ||
* Copyright(c) 2015 VovanR <mail@vovanr.com> | ||
* MIT Licensed | ||
*/ | ||
|
||
var _ = require('lodash'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var exists = fs.existsSync || path.existsSync; | ||
|
||
module.exports = { | ||
/** | ||
* Jade source parser | ||
* | ||
* @param {String} source | ||
* @return {Array} | ||
*/ | ||
parseString: function (source) { | ||
var result = []; | ||
|
||
if (_.isUndefined(source)) { | ||
return result; | ||
} | ||
|
||
source = String(source); | ||
|
||
if (!/\./.test(source)) { | ||
return result; | ||
} | ||
|
||
var re = /\.([^\s\.\(]+)/gm; | ||
|
||
var parsed = source.match(re); | ||
parsed = _.map(parsed, function (item) { | ||
return item.slice(1); | ||
}); | ||
|
||
parsed = _.uniq(parsed); | ||
|
||
if (parsed.length > 0) { | ||
result = parsed; | ||
} | ||
|
||
return result; | ||
}, | ||
|
||
/** | ||
* Jade file parser | ||
* | ||
* @param {String} filePath | ||
* @return {Array} | ||
*/ | ||
parseFile: function (filePath) { | ||
var result = []; | ||
|
||
if (!exists(filePath)) { | ||
return result; | ||
} | ||
|
||
var fileText = fs.readFileSync(filePath).toString(); | ||
result = this.parseString(fileText); | ||
|
||
return result; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
- input: '' | ||
output: [] | ||
|
||
- input: block | ||
output: [] | ||
|
||
- input: .block | ||
output: | ||
- block | ||
|
||
- input: .block.block_theme_simple | ||
output: | ||
- block | ||
- block_theme_simple | ||
|
||
- input: | | ||
block | ||
.block | ||
output: | ||
- block | ||
|
||
- input: | | ||
block | ||
.block | ||
foo | ||
.bar | ||
output: | ||
- block | ||
- bar | ||
|
||
- input: | | ||
block | ||
.block | ||
foo | ||
.bar | ||
output: | ||
- block | ||
- bar | ||
|
||
- input: .foo.bar.foo.baz.foo | ||
output: | ||
- foo | ||
- bar | ||
- baz | ||
|
||
- input: | | ||
.header.header_theme_simple | ||
.header__side.header__side_align_left | ||
.header__side.header__side_align_right | ||
output: | ||
- header | ||
- header_theme_simple | ||
- header__side | ||
- header__side_align_left | ||
- header__side_align_right | ||
|
||
- input: | | ||
.progressbar__bar(style='width: 90%;') | ||
output: | ||
- progressbar__bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
var assert = require('chai').assert; | ||
var mockfs = require('mock-fs'); | ||
var parserJade = require('../lib/parser-jade'); | ||
var _ = require('lodash'); | ||
var yaml = require('js-yaml'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
var testData = yaml.safeLoad(fs.readFileSync('./test/parser-jade-test-cases.yml', 'utf8')); | ||
|
||
var TEMP_DIR = '/tmp/bemstyla'; | ||
|
||
describe('parserJade', function () { | ||
it('should be `Object`', function () { | ||
assert.isObject(parserJade); | ||
}); | ||
|
||
describe('#parseString', function () { | ||
it('should return an `Array`', function () { | ||
assert.isArray(parserJade.parseString()); | ||
}); | ||
|
||
it('should parse class names from jade text', function () { | ||
_.forEach(testData, function (data) { | ||
assert.deepEqual( | ||
parserJade.parseString(data.input), | ||
data.output | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#parseFile', function () { | ||
it('should return an empty array if file does not exists', function () { | ||
assert.isArray(parserJade.parseFile()); | ||
}); | ||
|
||
it('should parse Jade file', function () { | ||
var mockData = {}; | ||
|
||
var filePath = path.join(TEMP_DIR, 'foo.jade'); | ||
var fileText = '.foo.bar'; | ||
mockData[filePath] = fileText; | ||
mockfs(mockData); | ||
|
||
assert.deepEqual(fs.readFileSync(filePath).toString(), fileText); | ||
assert.deepEqual(parserJade.parseFile(filePath), ['foo', 'bar']); | ||
|
||
mockfs.restore(); | ||
}); | ||
}); | ||
}); |