consolidate
Advanced tools
Comparing version 0.4.0 to 0.5.0
0.5.0 / 2012-10-29 | ||
================== | ||
* add `mote` support | ||
* add support to `dust` partials | ||
* add support for `ECT` | ||
* add support for rendering without file | ||
* add support for `JUST` | ||
* improve Haml-Coffee caching. | ||
0.4.0 / 2012-07-30 | ||
@@ -3,0 +13,0 @@ ================== |
@@ -1,2 +0,1 @@ | ||
/*! | ||
@@ -6,3 +5,3 @@ * consolidate | ||
* MIT Licensed | ||
* | ||
* | ||
* Engines which do not support caching of their file contents | ||
@@ -22,4 +21,10 @@ * should use the `read()` function defined in consolidate.js | ||
var fs = require('fs'); | ||
var fs = require('fs') | ||
, path = require('path') | ||
, join = path.join | ||
, extname = path.extname | ||
, dirname = path.dirname; | ||
var readCache = {}; | ||
/** | ||
@@ -29,3 +34,3 @@ * Require cache. | ||
var cache = {}; | ||
var cacheStore = {}; | ||
@@ -45,6 +50,26 @@ /** | ||
exports.clearCache = function(){ | ||
cache = {}; | ||
cacheStore = {}; | ||
}; | ||
/** | ||
* Conditionally cache `compiled` template based | ||
* on the `options` filename and `.cache` boolean. | ||
* | ||
* @param {Object} options | ||
* @param {Function} compiled | ||
* @return {Function} | ||
* @api private | ||
*/ | ||
function cache(options, compiled) { | ||
if (compiled && options.filename && options.cache) { | ||
delete readCache[options.filename]; | ||
cacheStore[options.filename] = compiled; | ||
} else if (options.filename && options.cache) { | ||
return cacheStore[options.filename]; | ||
} | ||
return compiled; | ||
} | ||
/** | ||
* Read `path` with `options` with | ||
@@ -60,3 +85,3 @@ * callback `(err, str)`. When `options.cache` | ||
function read(path, options, fn) { | ||
var str = cache[path]; | ||
var str = readCache[path]; | ||
@@ -69,3 +94,3 @@ // cached (only if cached is a string and not a compiled template function) | ||
if (err) return fn(err); | ||
if (options.cache) cache[path] = str; | ||
if (options.cache) readCache[path] = str; | ||
fn(null, str); | ||
@@ -76,2 +101,20 @@ }); | ||
/** | ||
* fromStringRenderer | ||
*/ | ||
function fromStringRenderer(name) { | ||
return function(path, options, fn){ | ||
options.filename = path; | ||
if (cache(options)) { | ||
exports[name].render('', options, fn); | ||
} else { | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
exports[name].render(str, options, fn); | ||
}); | ||
} | ||
}; | ||
} | ||
/** | ||
* Jade support. | ||
@@ -86,38 +129,49 @@ */ | ||
/** | ||
* Jade string support. | ||
*/ | ||
exports.jade.render = function(str, options, fn){ | ||
var engine = requires.jade || (requires.jade = require('jade')); | ||
engine.render(str, options, fn); | ||
}; | ||
/** | ||
* Dust support. | ||
*/ | ||
exports.dust = function(path, options, fn){ | ||
exports.dust = fromStringRenderer('dust'); | ||
/** | ||
* Dust string support. | ||
*/ | ||
exports.dust.render = function(str, options, fn){ | ||
var engine = requires.dust; | ||
if (!engine) { | ||
try { | ||
requires.dust = require('dust'); | ||
engine = requires.dust = require('dust'); | ||
} catch (err) { | ||
requires.dust = require('dustjs-linkedin'); | ||
engine = requires.dust = require('dustjs-linkedin'); | ||
} | ||
engine = requires.dust; | ||
engine.onLoad = function(path, callback) { read(path, options, callback); } | ||
} | ||
var tmpl = cache[path]; | ||
var ext = 'dust' | ||
, views = '.'; | ||
// try cache (only if cached is a compiled template function and not a string) | ||
if (options.cache && tmpl && 'function' == typeof tmpl) { | ||
if (options) { | ||
if (options.ext) ext = options.ext; | ||
if (options.views) views = options.views; | ||
if (options.settings && options.settings.views) views = options.settings.views; | ||
} | ||
engine.onLoad = function(path, callback){ | ||
if ('' == extname(path)) path += '.' + ext; | ||
if ('/' !== path[0]) path = views + '/' + path; | ||
read(path, options, callback); | ||
}; | ||
try { | ||
var tmpl = cache(options) || cache(options, engine.compileFn(str)); | ||
tmpl(options, fn); | ||
} else { | ||
read(path, options, function(err, str) { | ||
if (err) return fn(err); | ||
try { | ||
options.filename = path; | ||
tmpl = engine.compileFn(str); | ||
if (options.cache) cache[path] = tmpl; | ||
else engine.cache = {}; | ||
tmpl(options, fn); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
@@ -130,14 +184,16 @@ }; | ||
exports.swig = function(path, options, fn){ | ||
exports.swig = fromStringRenderer('swig'); | ||
/** | ||
* Swig string support. | ||
*/ | ||
exports.swig.render = function(str, options, fn){ | ||
var engine = requires.swig || (requires.swig = require('swig')); | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
try { | ||
options.filename = path; | ||
var tmpl = engine.compile(str, options); | ||
fn(null, tmpl(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
try { | ||
var tmpl = cache(options) || cache(options, engine.compile(str, options)); | ||
fn(null, tmpl(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
@@ -149,14 +205,16 @@ | ||
exports.liquor = function(path, options, fn){ | ||
exports.liquor = fromStringRenderer('liquor'); | ||
/** | ||
* Liquor string support. | ||
*/ | ||
exports.liquor.render = function(str, options, fn){ | ||
var engine = requires.liquor || (requires.liquor = require('liquor')); | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
try { | ||
options.filename = path; | ||
var tmpl = engine.compile(str, options); | ||
fn(null, tmpl(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
try { | ||
var tmpl = cache(options) || cache(options, engine.compile(str, options)); | ||
fn(null, tmpl(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
@@ -168,16 +226,19 @@ | ||
exports.ejs = function(path, options, fn){ | ||
exports.ejs = fromStringRenderer('ejs'); | ||
/** | ||
* EJS string support. | ||
*/ | ||
exports.ejs.render = function(str, options, fn){ | ||
var engine = requires.ejs || (requires.ejs = require('ejs')); | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
try { | ||
options.filename = path; | ||
var tmpl = engine.compile(str, options); | ||
fn(null, tmpl(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
try { | ||
var tmpl = cache(options) || cache(options, engine.compile(str, options)); | ||
fn(null, tmpl(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
/** | ||
@@ -187,13 +248,15 @@ * Eco support. | ||
exports.eco = function(path, options, fn){ | ||
exports.eco = fromStringRenderer('eco'); | ||
/** | ||
* Eco string support. | ||
*/ | ||
exports.eco.render = function(str, options, fn){ | ||
var engine = requires.eco || (requires.eco = require('eco')); | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
try { | ||
options.filename = path; | ||
fn(null, engine.render(str, options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
try { | ||
fn(null, engine.render(str, options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
@@ -205,16 +268,18 @@ | ||
exports.jazz = function(path, options, fn){ | ||
exports.jazz = fromStringRenderer('jazz'); | ||
/** | ||
* Jazz string support. | ||
*/ | ||
exports.jazz.render = function(str, options, fn){ | ||
var engine = requires.jazz || (requires.jazz = require('jazz')); | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
try { | ||
options.filename = path; | ||
var tmpl = engine.compile(str, options); | ||
tmpl.eval(options, function(str){ | ||
fn(null, str); | ||
}); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
try { | ||
var tmpl = cache(options) || cache(options, engine.compile(str, options)); | ||
tmpl.eval(options, function(str){ | ||
fn(null, str); | ||
}); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
@@ -226,14 +291,16 @@ | ||
exports.jqtpl = function(path, options, fn){ | ||
exports.jqtpl = fromStringRenderer('jqtpl'); | ||
/** | ||
* JQTPL string support. | ||
*/ | ||
exports.jqtpl.render = function(str, options, fn){ | ||
var engine = requires.jqtpl || (requires.jqtpl = require('jqtpl')); | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
try { | ||
options.filename = path; | ||
engine.template(path, str); | ||
fn(null, engine.tmpl(path, options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
try { | ||
engine.template(str, str); | ||
fn(null, engine.tmpl(str, options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
@@ -245,14 +312,16 @@ | ||
exports.haml = function(path, options, fn){ | ||
exports.haml = fromStringRenderer('haml'); | ||
/** | ||
* Haml string support. | ||
*/ | ||
exports.haml.render = function(str, options, fn){ | ||
var engine = requires.hamljs || (requires.hamljs = require('hamljs')); | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
try { | ||
options.filename = path; | ||
options.locals = options; | ||
fn(null, engine.render(str, options).trimLeft()); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
try { | ||
options.locals = options; | ||
fn(null, engine.render(str, options).trimLeft()); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
@@ -270,16 +339,32 @@ | ||
/** | ||
* Whiskers string support. | ||
*/ | ||
exports.whiskers.render = function(str, options, fn){ | ||
var engine = requires.whiskers || (requires.whiskers = require('whiskers')); | ||
try { | ||
fn(null, engine.render(str, options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
/** | ||
* Coffee-HAML support. | ||
*/ | ||
exports['haml-coffee'] = function(path, options, fn){ | ||
exports['haml-coffee'] = fromStringRenderer('haml-coffee'); | ||
/** | ||
* Coffee-HAML string support. | ||
*/ | ||
exports['haml-coffee'].render = function(str, options, fn){ | ||
var engine = requires.HAMLCoffee || (requires.HAMLCoffee = require('haml-coffee')); | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
try { | ||
var tmpl = engine.compile(str, options); | ||
fn(null, tmpl(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
try { | ||
var tmpl = cache(options) || cache(options, engine.compile(str, options)); | ||
fn(null, tmpl(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
@@ -291,13 +376,16 @@ | ||
exports.hogan = function(path, options, fn){ | ||
exports.hogan = fromStringRenderer('hogan'); | ||
/** | ||
* Hogan string support. | ||
*/ | ||
exports.hogan.render = function(str, options, fn){ | ||
var engine = requires.hogan || (requires.hogan = require('hogan.js')); | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
try { | ||
var tmpl = engine.compile(str, options); | ||
fn(null, tmpl.render(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
try { | ||
var tmpl = cache(options) || cache(options, engine.compile(str, options)); | ||
fn(null, tmpl.render(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
@@ -309,14 +397,16 @@ | ||
exports.handlebars = function(path, options, fn) { | ||
exports.handlebars = fromStringRenderer('handlebars'); | ||
/** | ||
* Handlebars string support. | ||
*/ | ||
exports.handlebars.render = function(str, options, fn) { | ||
var engine = requires.handlebars || (requires.handlebars = require('handlebars')); | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
try { | ||
options.filename = path; | ||
var tmpl = engine.compile(str, options); | ||
fn(null, tmpl(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
try { | ||
var tmpl = cache(options) || cache(options, engine.compile(str, options)); | ||
fn(null, tmpl(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
} | ||
@@ -328,15 +418,17 @@ | ||
exports.underscore = function(path, options, fn) { | ||
exports.underscore = fromStringRenderer('underscore'); | ||
/** | ||
* Underscore string support. | ||
*/ | ||
exports.underscore.render = function(str, options, fn) { | ||
var engine = requires.underscore || (requires.underscore = require('underscore')); | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
try { | ||
options.filename = path; | ||
var tmpl = engine.template(str, null, options); | ||
fn(null, tmpl(options).replace(/\n$/, '')); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
} | ||
try { | ||
var tmpl = cache(options) || cache(options, engine.template(str, null, options)); | ||
fn(null, tmpl(options).replace(/\n$/, '')); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
@@ -361,3 +453,20 @@ | ||
/** | ||
* QEJS string support. | ||
*/ | ||
exports.qejs.render = function (str, options, fn) { | ||
try { | ||
var engine = requires.qejs || (requires.qejs = require('qejs')); | ||
engine.render(str, options).then(function (result) { | ||
fn(null, result); | ||
}, function (err) { | ||
fn(err); | ||
}).end(); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
/** | ||
@@ -367,19 +476,15 @@ * Walrus support. | ||
exports.walrus = function (path, options, fn) { | ||
exports.walrus = fromStringRenderer('walrus'); | ||
/** | ||
* Walrus string support. | ||
*/ | ||
exports.walrus.render = function (str, options, fn) { | ||
var engine = requires.walrus || (requires.walrus = require('walrus')); | ||
var tmpl = cache[path]; | ||
// try cache (only if cached is a compiled template function and not a string) | ||
if (options.cache && tmpl && 'function' == typeof tmpl) { | ||
tmpl(options, fn); | ||
} else { | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
try { | ||
var tmpl = engine.parse(str); | ||
fn(null, tmpl.compile(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
try { | ||
var tmpl = cache(options) || cache(options, engine.parse(str)); | ||
fn(null, tmpl.compile(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
@@ -392,13 +497,15 @@ }; | ||
exports.mustache = function(path, options, fn) { | ||
exports.mustache = fromStringRenderer('mustache'); | ||
/** | ||
* Mustache string support. | ||
*/ | ||
exports.mustache.render = function(str, options, fn) { | ||
var engine = requires.mustache || (requires.mustache = require('mustache')); | ||
read(path, options, function(err, str){ | ||
if (err) return fn(err); | ||
try { | ||
options.filename = path; | ||
fn(null, engine.to_html(str, options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
try { | ||
fn(null, engine.to_html(str, options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
@@ -410,26 +517,84 @@ | ||
exports.dot = function(path, options, fn) { | ||
exports.dot = fromStringRenderer('dot'); | ||
/** | ||
* doT string support. | ||
*/ | ||
exports.dot.render = function(str, options, fn) { | ||
var engine = requires.dot || (requires.dot = require('dot')); | ||
var tmpl = cache[path]; | ||
try { | ||
var tmpl = cache(options) || cache(options, engine.template(str)); | ||
fn(null, tmpl(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
// try cache (only if cached is a compiled template function and not a string) | ||
if (options.cache && tmpl && 'function' == typeof tmpl) { | ||
try { | ||
var html = tmpl(options); | ||
fn(null, html); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
} else { | ||
read(path, options, function(err, str) { | ||
if (err) return fn(err); | ||
try { | ||
var tmpl = engine.template(str); | ||
cache[path] = tmpl; | ||
fn(null, tmpl(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}); | ||
/** | ||
* Just support. | ||
*/ | ||
exports.just = function(path, options, fn){ | ||
var engine = requires.just; | ||
if (!engine) { | ||
var JUST = require('just'); | ||
engine = requires.just = new JUST(); | ||
} | ||
engine.configure({ useCache: options.cache }); | ||
engine.render(path, options, fn); | ||
}; | ||
/** | ||
* Just string support. | ||
*/ | ||
exports.just.render = function(str, options, fn){ | ||
var JUST = require('just'); | ||
var engine = new JUST({ root: { page: str }}); | ||
engine.render('page', options, fn); | ||
}; | ||
/** | ||
* ECT support. | ||
*/ | ||
exports.ect = function(path, options, fn){ | ||
var engine = requires.ect; | ||
if (!engine) { | ||
var ECT = require('ect'); | ||
engine = requires.ect = new ECT(); | ||
} | ||
engine.configure({ cache: options.cache }); | ||
engine.render(path, options, fn); | ||
}; | ||
/** | ||
* ECT string support. | ||
*/ | ||
exports.ect.render = function(str, options, fn){ | ||
var ECT = require('ect'); | ||
var engine = new ECT({ root: { page: str }}); | ||
engine.render('page', options, fn); | ||
}; | ||
/** | ||
* mote support. | ||
*/ | ||
exports.mote = fromStringRenderer('mote'); | ||
/** | ||
* mote string support. | ||
*/ | ||
exports.mote.render = function(str, options, fn){ | ||
var engine = requires.mote || (requires.mote = require('mote')); | ||
try { | ||
var tmpl = cache(options) || cache(options, engine.compile(str)); | ||
fn(null, tmpl(options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; |
{ | ||
"name": "consolidate", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Template engine consolidation library", | ||
@@ -23,3 +23,3 @@ "keywords": [ | ||
"whiskers": "0.2.2", | ||
"haml-coffee": "0.6.3", | ||
"haml-coffee": "1.4.0", | ||
"hogan.js": "2.0.0", | ||
@@ -33,3 +33,6 @@ "dust": "0.3.0", | ||
"mustache": "0.4.0", | ||
"dot": "0.2.6" | ||
"dot": "0.2.6", | ||
"just": "0.1.8", | ||
"ect": "0.2.10", | ||
"mote": "0.2.0" | ||
}, | ||
@@ -36,0 +39,0 @@ "main": "index", |
@@ -13,2 +13,3 @@ # Consolidate.js | ||
- [eco](https://github.com/sstephenson/eco) | ||
- [ect](https://github.com/baryshev/ect) [(website)](http://ectjs.com/) | ||
- [ejs](https://github.com/visionmedia/ejs) | ||
@@ -22,2 +23,3 @@ - [haml](https://github.com/visionmedia/haml.js) [(website)](http://haml-lang.com/) | ||
- [jqtpl](https://github.com/kof/node-jqtpl) [(website)](http://api.jquery.com/category/plugins/templates/) | ||
- [JUST](https://github.com/baryshev/just) | ||
- [liquor](https://github.com/chjj/liquor) | ||
@@ -24,0 +26,0 @@ - [mustache](https://github.com/janl/mustache.js) |
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
18720
469
151
0
24