Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions lib/modules/kss-splitter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'
'use strict';

var gonzales = require('gonzales-pe'),
gonzo = require('gonzales-ast');
Expand All @@ -7,10 +7,8 @@ module.exports = {

/* Split string source into array of code and comment blocks */
pureSplitter: function(source, syntax) {

syntax = syntax || 'scss';
var out = {},
ast = gonzales.srcToAST({
var ast = gonzales.srcToAST({
src: source,
syntax: syntax
}),
Expand All @@ -20,7 +18,7 @@ module.exports = {

gonzo.traverse(ast, [{
// Visitor for SASS and SCSS syntaxes
test: function(name, nodes) {
test: function(name) {
return name !== 'stylesheet';
},
process: function(nodes) {
Expand Down Expand Up @@ -68,24 +66,23 @@ module.exports = {
}]);

// push last block
blocks.push(block);
if (block) {
blocks.push(block);
}
return blocks;

},

getBlocks: function(source, syntax) {

var blocks = this.pureSplitter(source, syntax),
pair = {
kss: '',
code: []
},
firstBlock = true,
pairs = [],
isKssMarkupBlock = /Styleguide [0-9]+/
isKssMarkupBlock = /Styleguide [0-9]+/;

blocks.forEach(function(block) {

if (block.type == 'comment') {
// Check if KSS
if (isKssMarkupBlock.test(block.content)) {
Expand Down Expand Up @@ -118,4 +115,4 @@ module.exports = {
return pairs;
}

}
};
116 changes: 57 additions & 59 deletions lib/modules/kss.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,77 +48,75 @@ function sanitize(string) {
}

function processBlock(block, options, json) {
var blockPromise = Q.defer();

kss.parse(block.kss, options, function(err, styleguide) {
var section,
blockStyles;
if (err) {
new PluginError(PLUGIN_NAME, 'Error parsing', err);
blockPromise.resolve();
return false;
} else {
section = jsonSections(styleguide.section());

if (section.length > 0) {
if (section.length > 1) {
console.warn('Warning: KSS splitter returned more than 1 KSS block. Styleguide might not be properly generated.');
return Q.Promise(function(resolve, reject) {
kss.parse(block.kss, options, function(err, styleguide) {
var section,
blockStyles;
if (err) {
console.error(' error processing kss block', err);
reject(err);
return false;
} else {
section = jsonSections(styleguide.section());

if (section.length > 0) {
if (section.length > 1) {
console.warn('Warning: KSS splitter returned more than 1 KSS block. Styleguide might not be properly generated.');
}
blockStyles = trimLinebreaks(block.code);

// Add related CSS to section
if (blockStyles && blockStyles !== '') {
section[0].css = blockStyles;
}
json.sections = json.sections.concat(section);
}
blockStyles = trimLinebreaks(block.code);

// Add related CSS to section
if (blockStyles && blockStyles !== '') {
section[0].css = blockStyles;
}
json.sections = json.sections.concat(section);
resolve();
}
blockPromise.resolve();
}
});
});
return blockPromise;
}

function processFile(contents, syntax, options, json) {
var filePromise = Q.defer(),
blockPromises = [],
splittedFile = kssSplitter.getBlocks(contents, syntax);

// Process every block in the current file
splittedFile.forEach(function(block) {
blockPromises.push(processBlock(block, options, json));
});

Q.all(blockPromises).then(function() {
// All blocks are processed, resolve file promise
filePromise.resolve();
return Q.Promise(function(resolve, reject) {
try {
var blockPromises = [],
blocks = kssSplitter.getBlocks(contents, syntax);

// Process every block in the current file
blocks.forEach(function(block) {
blockPromises.push(processBlock(block, options, json));
});
} catch (err) {
reject(err);
}
Q.all(blockPromises).then(resolve);
});

return filePromise;
}

module.exports = {
// Parse node-kss object ( {'file.path': 'file.contents.toString('utf8'}' )
parseKSS: function(files, options) {
var parsePromise = Q.defer(),
json = {
sections: []
},
filePromises = [],
fileKeys = Object.keys(files);

fileKeys.forEach(function(filePath) {
var contents = files[filePath],
syntax = path.extname(filePath).substring(1);
filePromises.push(processFile(contents, syntax, options, json));
});

Q.all(filePromises).then(function() {
// All files are processed. Sort results and call main promise
json.sections = _.sortBy(json.sections, function(section) {
return section.reference;
return Q.Promise(function(resolve, reject) {
var json = {
sections: []
},
filePromises = [],
fileKeys = Object.keys(files);

fileKeys.forEach(function(filePath) {
var contents = files[filePath],
syntax = path.extname(filePath).substring(1);
filePromises.push(processFile(contents, syntax, options, json));
});
parsePromise.resolve(json);

Q.all(filePromises).then(function() {
// All files are processed. Sort results and call main promise
json.sections = _.sortBy(json.sections, function(section) {
return section.reference;
});
resolve(json);
}).catch(reject);
});
return parsePromise.promise;
}
}
};
6 changes: 5 additions & 1 deletion lib/styleguide.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,14 @@ module.exports = function(options) {
emitCompileSuccess();
})
.catch(function(error) {
console.error('Style guide parsing failed:', error.message);
console.error(error.toString());
emitCompileError(error);
})
.finally(callback);
}).catch(function(error) {
console.error(error.toString());
emitCompileError(error);
callback();
});
}
).on('error', console.error.bind(console));
Expand Down