UNPKG

omljs

Version:

Template engine built on top of the Oli language

98 lines 2.66 kB
var fs, oml, echo, version, program, options, init, parse, stdin, getSource, output, outputError; fs = require('fs'); oml = require('./oml'); echo = require('./helpers').echo; version = require('../package.json').version; program = require('commander'); options = { indent: 2 }; exports.parse = function(it){ program.parse( it); return init(); }; program.version(version).usage('[options] <path/to/file.oli>').option('-o, --output <file>', 'write output into a file instead of stdout').option('-i, --in-line', 'parse in-line argument as string').option('-p, --pretty', 'generate well-indented pretty output').option('-d, --indent <size>', 'Output indent size. Default to 2 spaces').option('-t, --tabs', 'use tabs instead of spaces to indent').option('-s, --stdin', 'read source from stdin'); program.on('--help', function(){ echo(' Usage examples:'); echo(); echo(' $ oml file.oli > file.html'); echo(' $ oml file.oli -o file.html'); echo(' $ oml -i "div: p: Hello"'); echo(' $ oml -s < file.oli'); echo(' $ oml --indent 4 file.oli'); echo(' $ oml --tabs file.oli'); return echo(); }); program.on('in-line', function(){ return options.inLine = true; }).on('output', function(file){ return options.output = file; }).on('stdin', function(){ return options.stdin = true; }).on('tabs', function(){ return options.pretty = options.tabs = true; }).on('pretty', function(){ return options.pretty = true; }).on('indent', function(size){ options.indent = size === null ? 2 : parseInt(size, 10); return options.pretty = true; }); init = function(){ if (options.stdin) { return stdin(); } else { return output( parse( getSource())); } }; parse = function(it){ return oml.render(it, options); }; stdin = function(){ var buf; buf = ''; process.stdin.setEncoding('utf8'); process.stdin.on('data', function(it){ return buf += it; }); return process.stdin.on('end', function(){ if (buf) { process.stdout.write(parse(buf)); return echo(); } }).resume(); }; getSource = function(){ var source; if (options.inLine) { source = program.args.join(' '); } else { if (program.args) { source = program.args.map(function(it){ return fs.readFileSync(it); }).join(' '); } } return source; }; output = function(it){ if (options.output) { return fs.writeFileSync(options.output, it); } else { if (it) { return echo( it); } } }; outputError = function(it){ echo(it.name + ": " + it.message); if (it.errorLines) { echo('\n' + it.errorLines.join('\n')); } return process.exit(1); };