Skip to content

Commit

Permalink
Merge pull request #50 from dwightjack/master
Browse files Browse the repository at this point in the history
Multi-file and meta improvements
  • Loading branch information
Masaaki Morishita committed Jul 5, 2017
2 parents e5d2007 + 4338229 commit 736904c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var path = require('path');
var postcss = require('postcss');

var analyzer = require('./lib/analyzer');
Expand All @@ -18,7 +19,7 @@ module.exports = postcss.plugin('postcss-style-guide', function (opts) {
} catch (err) {
throw err;
}
var maps = analyzer.analyze(root);
var maps = analyzer.analyze(root, opts);
var palette = colorPalette.parse(root.toString());
var promise = syntaxHighlighter.execute({
src: params.src,
Expand All @@ -34,7 +35,7 @@ module.exports = postcss.plugin('postcss-style-guide', function (opts) {
fileWriter.write(params.dest, html);

if (!opts.silent) {
console.log('Successfully created style guide!');
console.log('Successfully created style guide at ' + path.relative(process.cwd(), params.dest) + '!');
}

return root;
Expand Down
8 changes: 6 additions & 2 deletions lib/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports.setModules = function (syntaxHighlighter, markdownParser) {
this.markdownParser = markdownParser;
}

exports.analyze = function (root) {
exports.analyze = function (root, opts) {
var list = [];
var linkId = 0;
root.walkComments(function (comment) {
Expand All @@ -26,12 +26,16 @@ exports.analyze = function (root) {
}
var joined = rules.join('\n\n');
var md = comment.text.replace(/(@document|@doc|@docs|@styleguide)\s*\n/, '');
md = md.replace(new RegExp('@(' + Object.keys(meta).join('|') + ')\\s.*\\n', 'g'), '');

md = md.replace(/@title\s.*\n/, '');

list.push({
meta: meta,
rule: this.syntaxHighlighter.highlight(joined),
html: this.markdownParser(md),
link: {
id: 'psg-link-' + linkId,
id: (meta.id || 'psg-link-' + linkId),
title: meta.title || null
}
});
Expand Down
3 changes: 2 additions & 1 deletion lib/params.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var fs = require('fs');
var path = require('path');
var result = require('./utils').result;

module.exports = function (root, opts, pluginOpts) {
var params = {};
Expand All @@ -13,7 +14,7 @@ module.exports = function (root, opts, pluginOpts) {
}

if (opts.dest) {
params.dest = path.resolve(cwd, opts.dest);
params.dest = path.resolve(cwd, result(opts.dest, opts, pluginOpts));
} else {
var from = (pluginOpts || {}).from; // for the gulp and grunt
var output = from ? path.basename(from, '.css') : 'index.html'
Expand Down
15 changes: 15 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var ObjProto = Object.prototype;
var toString = ObjProto.toString;

function isFunction(value) {
return toString.call(value) === '[object Function]';
}
module.exports.isFunction = isFunction;

function result(obj) {
if (isFunction(obj)) {
return obj.apply(null, [].slice.call(arguments, 1));
}
return obj;
}
module.exports.result = result;
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ test('analyzer: analyze root node', function (t) {
var actual = analyzer.analyze(root);

var expected = [{
meta: { styleguide: true, title: 'input sample', mymeta: 'test' },
rule: '<span class="hljs-class">.class</span> <span class="hljs-rules">{\n <span class="hljs-rule"><span class="hljs-attribute">color</span>:<span class="hljs-value"> blue</span></span>;\n}</span>',
html: '<h1 id="h1">h1</h1>',
link: {
Expand All @@ -122,6 +123,7 @@ test('analyzer: analyze root node', function (t) {
}
},
{
meta: { doc: true },
rule: '<span class="hljs-class">.class</span> <span class="hljs-rules">{\n <span class="hljs-rule"><span class="hljs-attribute">color</span>:<span class="hljs-value"> red</span></span>;\n}</span>',
html: '<h2 id="h2">h2</h2>',
link: {
Expand Down
1 change: 1 addition & 0 deletions test/input.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@styleguide
@title input sample
@mymeta test
# h1
*/
Expand Down

0 comments on commit 736904c

Please sign in to comment.