Skip to content

Commit

Permalink
Fix #191: Sort styleguide sections by reference number
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannu Pelkonen committed Nov 17, 2014
1 parent fe22333 commit 3d90162
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/modules/kss-splitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = {
type = 'comment';
}
// Single breaklines between singleline comments are comments
else if (nodes[0] === 's' && nodes[1].split('\n').length <= 2 && prevNode[0] === 'commentSL') {
else if (nodes[0] === 's' && nodes[1].split('\n').length <= 2 && prevNode && prevNode[0] === 'commentSL') {
type = 'comment';
}
else {
Expand Down
24 changes: 15 additions & 9 deletions lib/modules/kss.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var kss = require('kss'),
path = require('path'),
Q = require('q'),
_ = require('lodash'),
kssSplitter = require('./kss-splitter'),
sanitizeHtml = require('sanitize-html');

Expand Down Expand Up @@ -35,7 +36,7 @@ function jsonModifiers(modifiers) {
}

function trimLinebreaks(str) {
// Remove leading and trailing whitespaces
// Remove leading and trailing linebreaks
if (!str) {
return str;
}
Expand Down Expand Up @@ -97,12 +98,13 @@ function processFile(contents, syntax, options, json) {

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

fileKeys.forEach(function(filePath) {
var contents = files[filePath],
Expand All @@ -111,8 +113,12 @@ module.exports = {
});

Q.all(filePromises).then(function() {
// All files are processed. Call success callback
success(json);
// All files are processed. Sort results and call main promise
json.sections = _.sortBy(json.sections, function(section) {
return section.reference;
});
parsePromise.resolve(json);
});
return parsePromise.promise;
}
}
14 changes: 7 additions & 7 deletions lib/styleguide.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var through = require('through2'),
path = require('path'),
sgServer = require(__dirname + '/server'),
File = require('vinyl'),
q = require('q'),
Q = require('q'),
distPath = __dirname + '/dist',
serverInstance;

Expand Down Expand Up @@ -126,7 +126,7 @@ module.exports = function(options) {
return through(throughOpts, bufferFileContents, function(callback) {
var _this = this;

parseKSS(filesBuffer, opt.kssOpt, function(styleguide) {
parseKSS(filesBuffer, opt.kssOpt).then(function(styleguide) {
function pushAllFiles() {
return through.obj(function(file, enc, cb) {
_this.push(file);
Expand All @@ -135,7 +135,7 @@ module.exports = function(options) {
}

function processOverviewMarkdown(opt) {
return q.Promise(function(resolve) {
return Q.Promise(function(resolve) {
if (!opt.overviewPath) {
resolve();
}
Expand Down Expand Up @@ -168,7 +168,7 @@ module.exports = function(options) {
overviewProcessed = processOverviewMarkdown(opt);

// Preprocess all CSS files and combile then to a single file
preProcessingDone = q.Promise(function(resolve, reject) {
preProcessingDone = Q.Promise(function(resolve, reject) {
preprocess.getStream(Object.keys(filesBuffer), opt, reject)
.pipe(through.obj(function(file, enc, cb) {

Expand All @@ -184,14 +184,14 @@ module.exports = function(options) {
});

// Copy all files (except index.html) from dist from to output stream
filesCopied = q.Promise(function(resolve, reject) {
filesCopied = Q.Promise(function(resolve, reject) {
gulp.src([distPath + '/**', '!' + distPath + '/index.html'])
.pipe(pushAllFiles())
.on('finish', resolve);
});

// Process index.html
indexHtmlProcessed = q.Promise(function(resolve, reject) {
indexHtmlProcessed = Q.Promise(function(resolve, reject) {
gulp.src([distPath + '/index.html'])
.pipe(mustache({
title: opt.title,
Expand All @@ -204,7 +204,7 @@ module.exports = function(options) {
.on('finish', resolve);
});

q.all([overviewProcessed, preProcessingDone, filesCopied, indexHtmlProcessed])
Q.all([overviewProcessed, preProcessingDone, filesCopied, indexHtmlProcessed])
.then(function() {
if (opt.server) {
startServer(opt);
Expand Down
54 changes: 54 additions & 0 deletions test/kss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var chai = require('chai'),
expect = chai.expect,
multiline = require('multiline'),
parseKSS = require('../lib/modules/kss').parseKSS;

describe('KSS parser', function() {
var files;
beforeEach(function() {
var str = multiline(function() {
/*
// Section
//
// Styleguide 1.2
// Section
//
// Styleguide 1.0
// Section
//
// Styleguide 2.0
*/
}),
str2 = multiline(function() {
/*
// Section
//
// Styleguide 1.1
*/
});
files = {'file1.scss': str, 'file2.scss': str2};
});

it('should parse sections from all files', function(done) {
parseKSS(files, {}).then(function(styleguide) {
expect(styleguide.sections.length).to.eql(4);
done();
}).catch(function(error) {
done(error);
});
});

it('should list sections in the correct order', function(done) {
parseKSS(files, {}).then(function(styleguide) {
expect(styleguide.sections[0].reference).to.eql('1');
expect(styleguide.sections[1].reference).to.eql('1.1');
expect(styleguide.sections[2].reference).to.eql('1.2');
expect(styleguide.sections[3].reference).to.eql('2');
done();
}).catch(function(error) {
done(error);
});
});
});

0 comments on commit 3d90162

Please sign in to comment.