Comparing version 0.9.2 to 0.9.3
195
index.js
@@ -11,2 +11,3 @@ /*! | ||
var path = require('path'); | ||
var util = require('util'); | ||
var glob = require('globby'); | ||
@@ -16,14 +17,5 @@ var chalk = require('chalk'); | ||
var typeOf = require('kind-of'); | ||
var layouts = require('layouts'); | ||
var routes = require('en-route'); | ||
var Cache = require('config-cache'); | ||
var Delims = require('delimiters'); | ||
var Helpers = require('helper-cache'); | ||
var Engines = require('engine-cache'); | ||
var Loaders = require('loader-cache'); | ||
var pickFrom = require('pick-from'); | ||
var cloneDeep = require('clone-deep'); | ||
var arrayify = require('arrayify-compact'); | ||
var engineLodash = require('engine-lodash'); | ||
var parser = require('parser-front-matter'); | ||
var extend = require('extend-shallow'); | ||
@@ -35,10 +27,23 @@ var reduce = require('object.reduce'); | ||
/** | ||
* Extend Template | ||
*/ | ||
var layouts = require('layouts'); | ||
var routes = require('en-route'); | ||
var Delims = require('delimiters'); | ||
var Config = require('config-cache'); | ||
var Helpers = require('helper-cache'); | ||
var Engines = require('engine-cache'); | ||
var Loaders = require('loader-cache'); | ||
var Router = routes.Router; | ||
var Route = routes.Route; | ||
/** | ||
* Local modules | ||
*/ | ||
var init = require('./lib/middleware/init'); | ||
var defaultLoader = require('./lib/loaders'); | ||
var camelize = require('./lib/camelize'); | ||
var hasOwn = require('./lib/has-own'); | ||
var debug = require('./lib/debug'); | ||
var utils = require('./lib'); | ||
var Router = routes.Router; | ||
var Route = routes.Route; | ||
@@ -61,3 +66,3 @@ /** | ||
var Template = module.exports = Cache.extend({ | ||
var Template = module.exports = Config.extend({ | ||
constructor: function(options) { | ||
@@ -74,5 +79,5 @@ Template.__super__.constructor.call(this, options); | ||
Template.extend = Cache.extend; | ||
Template.Router = Router; | ||
Template.Route = Route; | ||
Template.extend = Config.extend; | ||
Template.Router = routes.Router; | ||
Template.Route = routes.Route; | ||
@@ -88,3 +93,2 @@ /** | ||
this.transforms = {}; | ||
this.history = []; | ||
@@ -122,3 +126,2 @@ // Engine properties | ||
this.defaultOptions(); | ||
this.defaultRoutes(); | ||
this.defaultTransforms(); | ||
@@ -128,2 +131,3 @@ this.defaultDelimiters(); | ||
this.defaultEngines(); | ||
this.defaultRoutes(); | ||
}; | ||
@@ -156,24 +160,34 @@ | ||
Template.prototype.defaultOptions = function() { | ||
this.disable('preferLocals'); | ||
// routes | ||
this.enable('default routes'); | ||
this.option('router methods', []); | ||
// engines | ||
this.disable('debugEngine'); | ||
this.disable('preferLocals'); | ||
this.enable('default engines'); | ||
this.option('viewEngine', '*'); | ||
this.option('delims', ['<%', '%>']); | ||
// helpers | ||
this.enable('default helpers'); | ||
this.enable('default routes'); | ||
this.enable('track history'); | ||
this.option('cwd', process.cwd()); | ||
// file extensions | ||
this.option('ext', '*'); | ||
this.option('destExt', '.html'); | ||
this.option('defaultExts', ['md', 'html', 'hbs', 'lodash']); | ||
this.option('delims', ['<%', '%>']); | ||
this.option('viewEngine', '*'); | ||
this.enable('mergePartials'); | ||
this.option('defaultExts', ['md']); | ||
this.option('cwd', process.cwd()); | ||
// layouts | ||
this.enable('mergeLayouts'); | ||
this.option('defaultLayout', null); | ||
this.option('layoutDelims', ['{%', '%}']); | ||
this.option('layoutTag', 'body'); | ||
this.option('defaultLayout', null); | ||
this.option('layoutExt', null); | ||
this.option('layout', null); | ||
this.option('router methods', []); | ||
// partials | ||
this.enable('mergePartials'); | ||
// Custom function for naming partial keys | ||
@@ -189,3 +203,3 @@ this.option('partialsKey', function (fp) { | ||
// Custom function for matchLoader | ||
// Custom function for getting a loader | ||
this.option('matchLoader', function () { | ||
@@ -205,12 +219,2 @@ return 'default'; | ||
/** | ||
* Register default loader methods | ||
*/ | ||
Template.prototype.track = function(msg, key, value) { | ||
if (this.enabled('track history')) { | ||
this.history.push({msg: msg, key: key, value: value}); | ||
} | ||
}; | ||
/** | ||
* Mixin methods from [loader-cache] for loading templates. | ||
@@ -238,3 +242,3 @@ */ | ||
Template.prototype.defaultLoaders = function() { | ||
this.loader('default', defaultLoader(this)); | ||
this.loader('default', require('./lib/loaders')(this)); | ||
}; | ||
@@ -251,8 +255,4 @@ | ||
if (this.enabled('default routes')) { | ||
this.route(/\.*/).onLoad(function route(file, next) { | ||
parser.parse(file, function(err) { | ||
if (err) return next(err); | ||
next(); | ||
}); | ||
}); | ||
var re = utils.extensionRe(Object.keys(this.engines)); | ||
this.onLoad(re, require('./lib/middleware/matter')); | ||
} | ||
@@ -277,5 +277,5 @@ }; | ||
if (this.enabled('default engines')) { | ||
this.engine(['*', 'md'], engineLodash, { | ||
layoutDelims: ['{%', '%}'], | ||
destExt: '.html' | ||
this.engine(['*', 'md'], require('engine-lodash'), { | ||
layoutDelims: this.option('layoutDelims'), | ||
destExt: this.option('destExt') | ||
}); | ||
@@ -358,2 +358,3 @@ } | ||
}); | ||
// initialization middleware. currently a noop | ||
this.router.use(init(this)); | ||
@@ -575,6 +576,3 @@ } | ||
if (ext) { | ||
if (ext[0] !== '.') { | ||
ext = '.' + ext; | ||
} | ||
layout = layout + ext; | ||
layout = layout + utils.formatExt(ext); | ||
} | ||
@@ -630,5 +628,4 @@ | ||
debug.delims('adding delims [ext]: %s', ext, delims); | ||
ext = utils.formatExt(ext); | ||
if (ext[0] !== '.') { ext = '.' + ext; } | ||
if (typeOf(layoutDelims) === 'object') { | ||
@@ -660,5 +657,5 @@ settings = layoutDelims; | ||
ext = ext || this.currentDelims || this.option('ext'); | ||
ext = utils.formatExt(ext); | ||
if (ext && ext[0] !== '.') { ext = '.' + ext; } | ||
if(hasOwn(this.delims, ext)) { | ||
if(utils.hasOwn(this.delims, ext)) { | ||
return this.delims[ext]; | ||
@@ -685,5 +682,4 @@ } | ||
debug.delims('using delims: %s', ext); | ||
ext = utils.formatExt(ext); | ||
if (ext && ext[0] !== '.') { ext = '.' + ext; } | ||
Object.defineProperty(this, 'currentDelims', { | ||
@@ -698,3 +694,2 @@ configurable: true, | ||
}); | ||
return ext; | ||
@@ -758,6 +753,5 @@ }; | ||
var opts = extend({}, options); | ||
ext = utils.formatExt(ext); | ||
if (ext[0] !== '.') { ext = '.' + ext; } | ||
this._.engines.setEngine(ext, fn, opts); | ||
if (opts.delims) { | ||
@@ -1130,7 +1124,3 @@ this.addDelims(ext, opts.delims); | ||
/** | ||
* Default method used to handle sync loading | ||
* when done | ||
*/ | ||
// Default method used to handle sync loading when done | ||
var cb = function (err, template) { | ||
@@ -1141,6 +1131,3 @@ if (err) throw new Error(err); | ||
/** | ||
* Normalize args to pass to loader stack | ||
*/ | ||
// Normalize args to pass to loader stack | ||
args = args.filter(function (arg, i) { | ||
@@ -1170,3 +1157,2 @@ if (i !== 0 && typeOf(arg) === 'array') { | ||
*/ | ||
function done(err, template) { | ||
@@ -1184,6 +1170,3 @@ if (err) return cb(err); | ||
/** | ||
* Choose loaders based on loader type | ||
*/ | ||
// Choose loaders based on loader type | ||
switch (type) { | ||
@@ -1233,7 +1216,7 @@ case 'async': | ||
if (!hasOwn(value, 'path')) { | ||
if (!utils.hasOwn(value, 'path')) { | ||
debug.err('template `value` must have a `path` property.'); | ||
} | ||
if (!hasOwn(value, 'content')) { | ||
if (!utils.hasOwn(value, 'content')) { | ||
debug.err('template `value` must have a `content` property.'); | ||
@@ -1264,4 +1247,5 @@ } | ||
var opts = extend({}, options); | ||
var self = this; | ||
var opts = extend({}, options); | ||
forOwn(template, function (value, key) { | ||
@@ -1278,3 +1262,3 @@ value.locals = value.locals || {}; | ||
// preference in the the `getExt()` method. | ||
if (hasOwn(opts, 'engine')) { | ||
if (utils.hasOwn(opts, 'engine')) { | ||
var ext = opts.engine; | ||
@@ -1286,3 +1270,3 @@ if (ext[0] !== '.') { | ||
} | ||
if (hasOwn(opts, 'delims')) { | ||
if (utils.hasOwn(opts, 'delims')) { | ||
value.options.delims = opts.delims; | ||
@@ -1312,3 +1296,3 @@ } | ||
Template.prototype.view = function(collection, name) { | ||
if (this.views.hasOwnProperty(collection)) { | ||
if (utils.hasOwn(this.views, collection)) { | ||
var obj = this.views[collection]; | ||
@@ -1374,3 +1358,3 @@ return name ? obj[name] : obj; | ||
if (type !== 'sync') { | ||
loader = methodName('loader', type); | ||
loader = utils.methodName('loader', type); | ||
} | ||
@@ -1431,3 +1415,3 @@ this[loader].apply(this, [].concat([subtype], stack)); | ||
for (var key in this.views[colection]) { | ||
if (this.views[colection].hasOwnProperty(key)) { | ||
if (utils.hasOwn(this.views[colection], key)) { | ||
o[key] = this.views[colection][key]; | ||
@@ -1555,3 +1539,3 @@ } | ||
var o = this.mergeType(type, subtypes); | ||
if (o && typeOf(o) === 'object' && hasOwn(o, key)) { | ||
if (o && typeOf(o) === 'object' && utils.hasOwn(o, key)) { | ||
return o[key]; | ||
@@ -1624,6 +1608,6 @@ } | ||
var views = this.views[plural]; | ||
if (hasOwn(views, name)) { | ||
if (utils.hasOwn(views, name)) { | ||
return views[name]; | ||
} | ||
if (hasOwn(views, name + (ext || '.md'))) { | ||
if (utils.hasOwn(views, name + (ext || '.md'))) { | ||
return views[name + (ext || '.md')]; | ||
@@ -1679,7 +1663,7 @@ } | ||
// Create a sync helper for this type | ||
if (!hasOwn(this._.helpers, subtype)) { | ||
if (!utils.hasOwn(this._.helpers, subtype)) { | ||
this.defaultHelper(subtype, plural); | ||
} | ||
// Create an async helper for this type | ||
if (!hasOwn(this._.asyncHelpers, subtype)) { | ||
if (!utils.hasOwn(this._.asyncHelpers, subtype)) { | ||
this.defaultAsyncHelper(subtype, plural); | ||
@@ -1709,3 +1693,3 @@ } | ||
// Add a `get` method to `Template` for `subtype` | ||
mixin(methodName('get', subtype), function (key) { | ||
mixin(utils.methodName('get', subtype), function (key) { | ||
return this.views[plural][key]; | ||
@@ -1715,3 +1699,3 @@ }); | ||
// Add a `render` method to `Template` for `subtype` | ||
mixin(methodName('render', subtype), function () { | ||
mixin(utils.methodName('render', subtype), function () { | ||
return this.renderSubtype(subtype); | ||
@@ -1734,3 +1718,3 @@ }); | ||
debug.render('compileBase: %j', arguments); | ||
if (!hasOwn(engine, 'compile')) { | ||
if (!utils.hasOwn(engine, 'compile')) { | ||
throw new Error('`.compile()` method not found on: "' + engine.name + '".'); | ||
@@ -1983,3 +1967,3 @@ } | ||
Template.prototype.renderSync = function(engine, content, options) { | ||
if (!hasOwn(engine, 'renderSync')) { | ||
if (!utils.hasOwn(engine, 'renderSync')) { | ||
throw new Error('`.renderSync()` method not found on: "' + engine.name + '".'); | ||
@@ -2008,3 +1992,3 @@ } | ||
Template.prototype.renderAsync = function(engine, content, options, cb) { | ||
if (!hasOwn(engine, 'render')) { | ||
if (!utils.hasOwn(engine, 'render')) { | ||
throw new Error('`.render()` method not found on: "' + engine.name + '".'); | ||
@@ -2085,3 +2069,3 @@ } | ||
locals = extend({options: {}}, locals); | ||
var options = locals.options; | ||
var options = locals.options || {}; | ||
@@ -2270,19 +2254,2 @@ var template = { content: str, locals: locals, options: options }; | ||
/** | ||
* Create a camel-cased method name for the given | ||
* `method` and `type`. | ||
* | ||
* 'get' + 'page' => `getPage` | ||
* | ||
* @param {String} `type` | ||
* @param {String} `name` | ||
* @return {String} | ||
*/ | ||
function methodName(method, type) { | ||
return camelize(method) | ||
+ type[0].toUpperCase() | ||
+ type.slice(1); | ||
} | ||
/** | ||
* Extend the `Template` prototype with a new method. | ||
@@ -2289,0 +2256,0 @@ * |
104
lib/index.js
@@ -22,2 +22,19 @@ 'use strict'; | ||
/** | ||
* Create a camel-cased method name for the given | ||
* `method` and `type`. | ||
* | ||
* 'get' + 'page' => `getPage` | ||
* | ||
* @param {String} `type` | ||
* @param {String} `name` | ||
* @return {String} | ||
*/ | ||
exports.methodName = function methodName(method, type) { | ||
return exports.camelcase(method) | ||
+ type.charAt(0).toUpperCase() | ||
+ type.slice(1); | ||
}; | ||
/** | ||
* Get the name of the layout to use for the given `template`. | ||
@@ -31,3 +48,3 @@ * | ||
exports.getLayout = function (template, locals) { | ||
exports.getLayout = function getLayout(template, locals) { | ||
debug.utils('[utils] getting layout: %j', template); | ||
@@ -49,3 +66,3 @@ | ||
exports.bindAll = function (target, thisArg) { | ||
exports.bindAll = function bindAll(target, thisArg) { | ||
if (Array.isArray(target)) { | ||
@@ -70,3 +87,3 @@ return target.map(function (fn) { | ||
exports.formatExt = function(ext) { | ||
exports.formatExt = function formatExt(ext) { | ||
if (ext && ext[0] !== '.') { | ||
@@ -86,3 +103,3 @@ ext = '.' + ext; | ||
exports.getExt = function(fp) { | ||
exports.getExt = function getExt(fp) { | ||
return path.extname(fp).slice(1); | ||
@@ -92,2 +109,49 @@ }; | ||
/** | ||
* Strip the dot from a file extension | ||
* | ||
* @param {String} `ext` extension | ||
* @return {String} | ||
* @api private | ||
*/ | ||
exports.stripDot = function stripDot(ext) { | ||
if (ext.charAt(0) === '.') { | ||
return ext.slice(1); | ||
} | ||
return ext; | ||
}; | ||
/** | ||
* Sanitize an array of extensions before converting | ||
* them to regex. | ||
* | ||
* This is used by the `extensionRe()` util for creating | ||
* a regex to match the given file extensions. | ||
* | ||
* @param {Array} `extensions` Array of file extensions | ||
* @return {Array} | ||
*/ | ||
exports.exts = function exts(extensions) { | ||
return extensions.map(function (ext) { | ||
if(ext === '.*') { | ||
ext = '$'; | ||
} else { | ||
ext = exports.stripDot(ext); | ||
} | ||
return ext; | ||
}); | ||
}; | ||
/** | ||
* Creates a regex to match only the file extensions of registered | ||
* engines. This is used by the default middleware to prevent | ||
* unregistered extensions from being processed. | ||
*/ | ||
exports.extensionRe = function extensionRe(str) { | ||
return new RegExp('(?:' + exports.exts(str).join('|') + ')$'); | ||
}; | ||
/** | ||
* Extend the `to` object with the method on the `from` object. | ||
@@ -102,3 +166,3 @@ * | ||
exports.mixInLoaders = function (to, from) { | ||
exports.mixInLoaders = function mixInLoaders(to, from) { | ||
return function (toProp, fromProp) { | ||
@@ -113,2 +177,15 @@ fromProp = fromProp || toProp; | ||
/** | ||
* Utility for getting an own property from an object. | ||
* | ||
* @param {Object} `o` | ||
* @param {Object} `prop` | ||
* @return {Boolean} | ||
* @api true | ||
*/ | ||
exports.hasOwn = function hasOwn(o, prop) { | ||
return {}.hasOwnProperty.call(o, prop); | ||
}; | ||
/** | ||
* Find any items in the array that match the specified types. | ||
@@ -122,3 +199,3 @@ * | ||
exports.filter = function (arr, types) { | ||
exports.filter = function filter(arr, types) { | ||
types = !Array.isArray(types) | ||
@@ -139,1 +216,16 @@ ? [types] | ||
}; | ||
/** | ||
* Camemlcase the given string. | ||
* | ||
* @param {String} str | ||
* @return {String} | ||
*/ | ||
exports.camelcase = function camelcase(str) { | ||
if (str.length === 1) { return str; } | ||
str = str.replace(/^[-_.\s]+/, '').toLowerCase(); | ||
return str.replace(/[-_.]+(\w|$)/g, function (_, ch) { | ||
return ch.toUpperCase(); | ||
}); | ||
}; |
@@ -6,2 +6,6 @@ 'use strict'; | ||
/** | ||
* Default loader for Template | ||
*/ | ||
module.exports = function (template) { | ||
@@ -8,0 +12,0 @@ return function (args, options) { |
@@ -13,5 +13,5 @@ 'use strict'; | ||
return function initEnv(template, next) { | ||
// placeholder for adding functionality to a `template` object. | ||
// placeholder | ||
next(); | ||
}; | ||
}; |
{ | ||
"name": "template", | ||
"description": "Render templates from any engine. Make custom template types, use layouts on pages, partials or any custom template type, custom delimiters, helpers, middleware, routes, loaders, and lots more. Powers Assemble v0.6.0, Verb v0.3.0 and your application.", | ||
"version": "0.9.2", | ||
"version": "0.9.3", | ||
"homepage": "https://github.com/jonschlinkert/template", | ||
@@ -44,3 +44,3 @@ "author": { | ||
"delimiters": "^1.0.1", | ||
"en-route": "^0.3.4", | ||
"en-route": "^0.4.0", | ||
"engine-cache": "^0.8.0", | ||
@@ -47,0 +47,0 @@ "engine-lodash": "^0.5.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
80507
2228
9
+ Addeden-route@0.4.0(transitive)
- Removeden-route@0.3.4(transitive)
Updateden-route@^0.4.0