Skip to content

Commit

Permalink
added block headers
Browse files Browse the repository at this point in the history
  • Loading branch information
amwmedia committed Nov 17, 2016
1 parent a3384f9 commit ae0ec49
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ jspm_packages

# Optional REPL history
.node_repl_history

helper-output.js
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testhelpers.js
50 changes: 50 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

module.exports = function (plop, config) {

// setup config defaults
const cfg = Object.assign({
prefix: '',
upperCaseHeaders: false,
blockStart: '/*',
blockEnd: '*/',
inlineStart: '//'
}, config || {});

plop.setDefaultInclude({ helpers: true });

plop.addHelper(`${cfg.prefix}multi-line-header`, multiLineHeader);

plop.addHelper(`${cfg.prefix}header`, function(text) {
return multiLineHeader(text.split('\n')[0]);
});

plop.addHelper(`${cfg.prefix}header-end`, function(text) {
if (cfg.upperCaseHeaders) { text = text.toUpperCase(); }
// one line only
text = text.split('\n')[0];

const border = '==';
const padding = ' ';
return `${cfg.inlineStart} ${border}${padding}END ${text}${padding}${border}`;
});

function multiLineHeader(text) {
if (cfg.upperCaseHeaders) { text = text.toUpperCase(); }
const border = '==';
const padding = ' ';
const lines = text.split('\n');
const longestLine = Math.max.apply(null, lines.map(line => line.length));
var out = `${cfg.blockStart} ${repeat('=', longestLine + (border.length * 2) + (padding.length * 2))}`;
out += `\n${repeat(' ', cfg.blockStart.length)} ${border}${padding}`;
out += lines.map(line => (line.length < longestLine ? line + repeat(' ', longestLine - line.length) : line))
.join(`${padding}${border}\n${repeat(' ', cfg.blockStart.length)} ${border}${padding}`);
out += `${padding}${border}\n`;
out += `${repeat(' ', cfg.blockStart.length)} ${repeat('=', longestLine + (border.length * 2) + (padding.length * 2))} ${cfg.blockEnd}`;

return out;
}
};

function repeat(char, amt) {
return (new Array(amt + 1)).join(char);
}
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "plop-pack-fancy-comments",
"version": "0.1.0",
"description": "A set of plop helpers that provide some fancy code commenting",
"main": "index.js",
"scripts": {
"test": "chokidar 'index.js' 'testhelpers.js' -c 'node testhelpers.js'"
},
"repository": {
"type": "git",
"url": "git+https://github.com/amwmedia/plop-pack-fancy-comments.git"
},
"keywords": [
"plop",
"plop-pack",
"helpers"
],
"author": "Andrew Worcester",
"license": "MIT",
"bugs": {
"url": "https://github.com/amwmedia/plop-pack-fancy-comments/issues"
},
"homepage": "https://github.com/amwmedia/plop-pack-fancy-comments#readme",
"devDependencies": {
"chokidar-cli": "^1.2.0"
}
}
23 changes: 23 additions & 0 deletions testhelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require('fs');
const path = require('path');
const helpers = {};
const plop = {
setDefaultInclude: function () {},
addHelper: function (name, func) {
helpers[name] = func;
}
};

require('./index')(plop);

var output = '';
Object.keys(helpers).forEach(function (name) {
const func = helpers[name];
output += (`\n// ${name}\n`);
output += func('simple test text 1');
output += '\n\n';
output += func('simple test text 1\nthis also has a longer line');
output += '\n\n';
});

fs.writeFileSync(path.resolve('./helper-output.js'), output, {encoding: 'utf8'});

0 comments on commit ae0ec49

Please sign in to comment.