Skip to content

Instantly share code, notes, and snippets.

@SjB
Last active August 29, 2015 14:04
Show Gist options
  • Save SjB/e1b5c47dfad9f610a8ba to your computer and use it in GitHub Desktop.
Save SjB/e1b5c47dfad9f610a8ba to your computer and use it in GitHub Desktop.
var mu = require('mu2'),
fs = require('fs'),
beautify_html = require('js-beautify').html
path = require('path');
var config = {
tpldir: 'templates',
outdir: 'web',
beautify_opts: { 'indent_inner_html': true, 'wrap_line_length': 120 }
}
var templates = [];
var data = {};
var importjsonfile = function(filename) {
var json = fs.readFileSync(filename).toString();
return eval('(' + json +')');
}
var loadjson = function(dst, filename) {
src = importjsonfile(filename);
for (var prop in src)
dst[prop] = src[prop];
return dst;
}
config = loadjson(config, path.join(__dirname, 'config.json'));
var args = process.argv.slice(2);
args.forEach(function(val, index, array) {
switch (path.extname(val || '')) {
case '':
templates.push(val);
jsonfile = path.join(config.tpldir, val) + '.json'
if (fs.existsSync(jsonfile)) {
data = loadjson(data, jsonfile);
}
break;
case '.json':
data = loadjson(data, val);
break;
}
});
mu.root = config.tpldir;
templates.forEach(function(tmpl) {
var buffer = '';
try {
tmplfile = tmpl + '.html';
mu.compileAndRender(tmplfile, data)
.on('data', function(data) {
buffer += data.toString();
})
.on('end', function() {
var outfile = path.join(config.outdir, tmplfile);
fs.writeFileSync(outfile, beautify_html(buffer, config.beautify_opts));
});
} catch (err) {
console.log(e);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment