Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add full functionality

  • Loading branch information...
commit 77350ff67d14292d0a448ddb444d6c05284d5dbe 1 parent 8cc0e59
@pvorb authored
Showing with 72 additions and 41 deletions.
  1. +6 −3 bin/tpl
  2. +66 −38 tpl.js
View
9 bin/tpl
@@ -12,10 +12,9 @@ var optimist = require('optimist')
});
var argv = optimist.argv;
-var tpl = require('tpl');
// Show version
if (argv.v) {
- console.log(tpl.version);
+ console.log(require('tpl').version);
}
// Show help
else if (argv.h) {
@@ -35,5 +34,9 @@ else if (argv._.length > 1) {
}
// Translate file
else {
- console.log(argv);
+ var file = require('path').resolve(process.cwd(), argv._[0]);
+
+ require('tpl').apply(file, function(err, result) {
+ console.log(result);
+ });
}
View
104 tpl.js
@@ -1,44 +1,72 @@
// tpl - a general purpose template cli
// (c) 2011 Paul Vorbach. Licensed under MIT.
;(function() {
- var version = 'v0.0.0';
-
- var fs = require('fs');
- var path = require('path');
- var confdir = require('confdir');
- var append = require('append');
-
- // default template engine
- var ejs = require('ejs');
- var tplEngine = ejs;
-
- // default
- var defaultConf = {
- parsers: {
- __default: 'markdown.js'
- },
- templates: {
- __default: 'default.ejs'
- },
- extensions: {
- __default: 'txt'
- },
- };
-
- function apply(content, cb) {
- // look for configuration directory
- confdir(process.cwd(), 'tpl', function(err, dir) {
- if (err) console.error(err);
-
- var json = fs.readFileSync(path.resolve(dir, 'conf.json'), 'utf8');
- var conf = JSON.parse(json);
- conf = append(defaultConf, conf);
-
- });
+
+module.exports.version = 'v0.0.0';
+
+var fs = require('fs');
+var path = require('path');
+var confdir = require('confdir');
+var append = require('append');
+
+// default configuration
+var defaultConf = {
+ parsers: {
+ default: 'props-markdown'
+ },
+ templates: {
+ default: {
+ file: 'default.ejs',
+ engine: 'ejs',
+ ext: '.txt'
+ }
}
+};
+
+function apply(file, cb) {
+ // look for configuration directory
+ confdir(process.cwd(), 'tpl', function(err, dir) {
+ if (err) {
+ cb(err);
+ return;
+ }
+
+ // read configuration file
+ var json = fs.readFileSync(path.resolve(dir, 'conf.json'), 'utf8');
+ var conf = JSON.parse(json);
+ // set configuration
+ conf = append(defaultConf, conf);
+
+ var ext = path.extname(file);
+ var parser;
+ // determine parser
+ if (typeof conf.parsers[ext] != 'undefined')
+ parser = conf.parser[ext];
+ else
+ parser = conf.parsers.default;
+ // require parser
+ parser = require(path.resolve(dir, 'parsers', parser + '.js'));
+
+ // parse file
+ var doc = parser(file);
+
+ var tpl;
+ // determine template and engine
+ if (typeof doc.template != 'undefined')
+ tpl = conf.templates[doc.template]
+ else
+ tpl = conf.templates.default;
+
+ // resolve template file
+ tpl.file = path.resolve(dir, 'templates', tpl.file);
+ // require template engine
+ tpl.engine = require(path.resolve(dir, 'templates', tpl.engine + '.js'));
+
+ // render
+ return cb(null, tpl.engine(tpl.file, doc));
+ });
+}
+
+module.exports.apply = apply;
- return {
- apply: apply,
- version: version
- };
}).call(this);
Please sign in to comment.
Something went wrong with that request. Please try again.