engine-base
Advanced tools
Comparing version 0.1.3 to 1.0.0
324
index.js
'use strict'; | ||
/** | ||
* Module dependencies. | ||
*/ | ||
module.exports = instance => { | ||
if (instance === void 0) throw new Error('expected an instance of "engine"'); | ||
var fs = require('fs'); | ||
var util = require('engine-utils'); | ||
var Emitter = require('component-emitter'); | ||
var utils = require('./utils'); | ||
var engine = util.fromStringRenderer('base'); | ||
Emitter(engine); | ||
const engine = { | ||
name: 'base', | ||
instance, | ||
async compile(file, options) { | ||
if (typeof file === 'string') file = { contents: file }; | ||
let opts = { ...options }; | ||
opts.imports = Object.assign({}, this.helpers, opts.helpers, opts.imports); | ||
if (typeof file.fn !== 'function' || opts.recompile === true) { | ||
file.fn = instance.compile(file.contents.toString(), opts); | ||
} | ||
return file; | ||
}, | ||
async render(file, locals, options) { | ||
if (typeof file === 'string') file = { contents: file }; | ||
/** | ||
* Expose the Engine constructor | ||
*/ | ||
if (typeof file.fn !== 'function') { | ||
await engine.compile.call(this, file, { ...locals, ...options }); | ||
} | ||
engine.Engine = utils.Engine; | ||
let res = await file.fn(locals); | ||
let str = this.resolveIds ? await this.resolveIds(res) : res; | ||
file.contents = Buffer.from(str); | ||
return file; | ||
}, | ||
compileSync(file, options) { | ||
if (typeof file === 'string') file = { contents: file }; | ||
let opts = { ...options }; | ||
opts.imports = Object.assign({}, this.helpers, opts.helpers, opts.imports); | ||
if (typeof file.fn !== 'function' || opts.recompile === true) { | ||
file.fn = instance.compile(file.contents.toString(), opts); | ||
} | ||
return file; | ||
}, | ||
renderSync(file, locals, options) { | ||
if (typeof file === 'string') file = { contents: file }; | ||
if (typeof file.fn !== 'function') { | ||
engine.compileSync.call(this, file, { ...locals, ...options }); | ||
} | ||
file.contents = Buffer.from(file.fn(locals)); | ||
return file; | ||
} | ||
}; | ||
/** | ||
* expose engine `defaults` | ||
*/ | ||
engine.options = { | ||
name: 'base', | ||
dest: {ext: '.html'} | ||
return engine; | ||
}; | ||
/** | ||
* Return a compiled function from the given template | ||
* `string` and `options`. | ||
* | ||
* ```js | ||
* var engine = require('engine-base'); | ||
* var fn = engine.compileSync('<%= name %>'); | ||
* console.log(fn({name: 'Halle'})); //=> 'Halle' | ||
* ``` | ||
* @param {String} `str` Template string to compile. | ||
* @param {Object} `options` Options or settings to pass to base | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
function compileSync(str, locals) { | ||
try { | ||
locals = locals || {}; | ||
locals.settings = locals.settings || {}; | ||
var settings = {}; | ||
var picked = utils.merge(pick(locals), pick(locals.settings)); | ||
var delims = picked.delims; | ||
var opts = picked.opts; | ||
var fns = picked.fns; | ||
settings.imports = utils.merge({}, fns.helpers, fns.imports); | ||
settings = utils.merge({}, settings, delims); | ||
if (locals.debugEngine === true) { | ||
inspectHelpers(settings, opts); | ||
} | ||
delete settings.imports.with; | ||
delete settings.imports.if; | ||
var base = new utils.Engine(settings); | ||
return base.compile(str, settings); | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
/** | ||
* Return a compiled function from the given template | ||
* `string` and `options` can `callback` | ||
* | ||
* ```js | ||
* var engine = require('engine-base'); | ||
* engine.compile('<%= name %>', function (err, fn) { | ||
* console.log(fn({name: 'Halle'})); //=> 'Halle' | ||
* }); | ||
* ``` | ||
* @param {String} `str` Template string to compile. | ||
* @param {Object} `options` Options or settings to pass to engine. | ||
* @param {Function} `cb` Callback function | ||
* @api public | ||
*/ | ||
function compile(str, options, cb) { | ||
if (typeof options === 'function') { | ||
return compile(str, {}, options); | ||
} | ||
if (typeof cb !== 'function') { | ||
return compileSync(str, options); | ||
} | ||
try { | ||
cb(null, compileSync(str, options)); | ||
} catch (err) { | ||
cb(err); | ||
} | ||
} | ||
/** | ||
* Render templates synchronously. | ||
* | ||
* ```js | ||
* var engine = require('engine-base'); | ||
* engine.renderSync('<%= name %>', {name: 'Halle'}); | ||
* //=> 'Halle' | ||
* ``` | ||
* | ||
* @param {Object} `str` The string to render. | ||
* @param {Object} `options` Object of options. | ||
* @option {Object} `settings` Settings to pass to Lo-Dash. | ||
* @option {Arrary} `delims` Template delimiters, generated by [delimiter-regex] | ||
* @option {Object} `imports` Template helpers to pass to Lo-Dash. | ||
* @return {String} Rendered string. | ||
* @api public | ||
*/ | ||
function renderSync(str, locals) { | ||
locals = locals || {}; | ||
locals.settings = locals.settings || {}; | ||
var settings = {}; | ||
var picked = utils.merge(pick(locals), pick(locals.settings)); | ||
var delims = picked.delims; | ||
var opts = picked.opts; | ||
var fns = picked.fns; | ||
settings.imports = utils.merge({}, fns.helpers, fns.imports); | ||
settings = utils.merge({}, settings, delims); | ||
if (locals.debugEngine === true) { | ||
inspectHelpers(settings, opts); | ||
} | ||
if (typeof str === 'function') { | ||
var ctx = utils.omit(locals, ['helpers', 'imports']); | ||
return str(ctx); | ||
} | ||
try { | ||
var base = new utils.Engine(settings); | ||
return base.render(str, locals); | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
/** | ||
* String support. Render the given `str` | ||
* and invoke the callback `callback(err, str)`. | ||
* | ||
* ```js | ||
* var engine = require('engine-base'); | ||
* engine.render('<%= name %>', {name: 'Jon'}, function (err, content) { | ||
* console.log(content); //=> 'Jon' | ||
* }); | ||
* ``` | ||
* | ||
* @param {String} `str` | ||
* @param {Object|Function} `locals` or callback. | ||
* @property {Object} `cache` enable template caching | ||
* @property {String} `filename` filename required for caching | ||
* @param {Function} `callback` | ||
* @api public | ||
*/ | ||
function render(str, locals, cb) { | ||
if (typeof locals === 'function') { | ||
return render(str, {}, locals); | ||
} | ||
if (typeof cb !== 'function') { | ||
return renderSync(str, locals); | ||
} | ||
try { | ||
cb(null, renderSync(str, locals)); | ||
} catch (err) { | ||
return cb(err); | ||
} | ||
} | ||
/** | ||
* File support. Render a file at the given `filepath` | ||
* and callback `callback(err, str)`. | ||
* | ||
* ```js | ||
* var engine = require('engine-base'); | ||
* engine.renderFile('foo/bar/baz.tmpl', {name: 'Halle'}); | ||
* //=> 'Halle' | ||
* ``` | ||
* | ||
* @param {String} `path` | ||
* @param {Object|Function} `options` or callback function. | ||
* @param {Function} `callback` | ||
* @api public | ||
*/ | ||
function renderFile(fp, opts, cb) { | ||
if (typeof opts === 'function') { | ||
return renderFile(fp, {}, opts); | ||
} | ||
var str = fs.readFileSync(fp, 'utf8'); | ||
render(str, opts, cb); | ||
} | ||
/** | ||
* Handle custom delimiters | ||
*/ | ||
function delimsObject(delims) { | ||
var a = delims[0], b = delims[1]; | ||
var res = {}; | ||
res.interpolate = utils.delims(a + '=', b); | ||
res.evaluate = utils.delims(a, b); | ||
res.escape = utils.delims(a + '-', b); | ||
return res; | ||
} | ||
/** | ||
* Inspect helpers if `debugEngine` is enabled | ||
*/ | ||
function inspectHelpers(settings, opts) { | ||
var helpers = Object.keys(settings.imports); | ||
for (var key in opts) { | ||
if (helpers.indexOf(key) !== -1) { | ||
var msg = conflictMessage(settings, opts, key); | ||
var err = new Error(msg); | ||
err.id = 'helper-conflict'; | ||
err.engine = 'engine-base'; | ||
engine.emit('error', err); | ||
} | ||
} | ||
} | ||
/** | ||
* Conflict report displayed when the same key exists as both | ||
* a helper name and the key of a (data) property on the context. | ||
*/ | ||
function conflictMessage(settings, options, key) { | ||
var type1 = typeof settings.imports[key]; | ||
var type2 = typeof options[key]; | ||
return 'Property "' + key + '" is defined on ' | ||
+ 'more than one object: \n' | ||
+ ' - `settings.imports` as ' + article(type1) + ' ' + type1 | ||
+ '\n' | ||
+ ' - `options` as ' + article(type2) + ' ' + type2; | ||
} | ||
function article(word) { | ||
var n = /^[aeiou]/.test(word); | ||
return n ? 'an' : 'a'; | ||
} | ||
function pick(obj) { | ||
var res = {}; | ||
res.delims = utils.pick(obj, ['interpolate', 'evaluate', 'escape']); | ||
res.opts = utils.omit(obj, ['helpers', 'imports']); | ||
res.fns = utils.pick(obj, ['helpers', 'imports']); | ||
if (Array.isArray(obj.delims)) { | ||
res.delims = utils.merge({}, delimsObject(obj.delims), res.delims); | ||
} | ||
return res; | ||
} | ||
/** | ||
* Express support. | ||
*/ | ||
engine.__express = engine.renderFile; | ||
/** | ||
* Expose `engine` | ||
*/ | ||
module.exports = engine; | ||
/** | ||
* Expose `engine` methods | ||
*/ | ||
module.exports.render = render; | ||
module.exports.renderFile = renderFile; | ||
module.exports.renderSync = renderSync; | ||
module.exports.compile = compile; | ||
module.exports.compileSync = compileSync; |
{ | ||
"name": "engine-base", | ||
"description": "Default engine for Template.", | ||
"version": "0.1.3", | ||
"description": "Default engine for jonschlinkert/templates.", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/jonschlinkert/engine-base", | ||
@@ -13,8 +13,7 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"files": [ | ||
"index.js", | ||
"utils.js" | ||
"index.js" | ||
], | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=8" | ||
}, | ||
@@ -24,19 +23,6 @@ "scripts": { | ||
}, | ||
"dependencies": { | ||
"component-emitter": "^1.2.1", | ||
"delimiter-regex": "^2.0.0", | ||
"engine": "^0.1.12", | ||
"engine-utils": "^0.1.1", | ||
"lazy-cache": "^2.0.2", | ||
"mixin-deep": "^1.1.3", | ||
"object.omit": "^2.0.1", | ||
"object.pick": "^1.2.0" | ||
}, | ||
"devDependencies": { | ||
"gulp": "^3.9.1", | ||
"gulp-istanbul": "^1.1.1", | ||
"gulp-jshint": "^2.0.4", | ||
"gulp-mocha": "^3.0.1", | ||
"jshint-stylish": "^2.2.1", | ||
"mocha": "^3.2.0" | ||
"engine": "^1.0.0", | ||
"gulp-format-md": "^1.0.0", | ||
"mocha": "^5.2.0" | ||
}, | ||
@@ -48,14 +34,17 @@ "keywords": [ | ||
"verb": { | ||
"toc": false, | ||
"layout": "default", | ||
"tasks": [ | ||
"readme" | ||
], | ||
"plugins": [ | ||
"gulp-format-md" | ||
], | ||
"related": { | ||
"list": [ | ||
"engine-cache", | ||
"engine-handlebars", | ||
"engine-less", | ||
"engine-lodash", | ||
"helper-cache", | ||
"template", | ||
"template-helpers" | ||
] | ||
"list": ["engine", "templates"] | ||
}, | ||
"lint": { | ||
"reflinks": true | ||
} | ||
} | ||
} |
152
README.md
@@ -1,11 +0,13 @@ | ||
# engine-base [![NPM version](https://badge.fury.io/js/engine-base.svg)](http://badge.fury.io/js/engine-base) | ||
# engine-base [![NPM version](https://img.shields.io/npm/v/engine-base.svg?style=flat)](https://www.npmjs.com/package/engine-base) [![NPM monthly downloads](https://img.shields.io/npm/dm/engine-base.svg?style=flat)](https://npmjs.org/package/engine-base) [![NPM total downloads](https://img.shields.io/npm/dt/engine-base.svg?style=flat)](https://npmjs.org/package/engine-base) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/engine-base.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/engine-base) | ||
> Default engine for Template. | ||
> Default engine for jonschlinkert/templates. | ||
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. | ||
## Install | ||
Install with [npm](https://www.npmjs.com/) | ||
Install with [npm](https://www.npmjs.com/): | ||
```sh | ||
$ npm i engine-base --save | ||
$ npm install --save engine-base | ||
``` | ||
@@ -16,135 +18,67 @@ | ||
```js | ||
var engine = require('engine-base'); | ||
``` | ||
const engine = require('engine-base')(require('engine')); | ||
const Templates = require('templates'); | ||
const app = new Templates(); | ||
## API | ||
### [compileSync](index.js#L44) | ||
Return a compiled function from the given template `string` and `options`. | ||
**Params** | ||
* `str` **{String}**: Template string to compile. | ||
* `options` **{Object}**: Options or settings to pass to base | ||
* `returns` **{Function}** | ||
**Example** | ||
```js | ||
var engine = require('engine-base'); | ||
var fn = engine.compileSync('<%= name %>'); | ||
console.log(fn({name: 'Halle'})); //=> 'Halle' | ||
// register with file extension(s) to associate with the engine | ||
app.engine('.md', engine); | ||
app.engine(['.md', '.html'], engine); | ||
app.engine('*', engine); | ||
``` | ||
### [compile](index.js#L84) | ||
## About | ||
Return a compiled function from the given template `string` and `options` can `callback` | ||
<details> | ||
<summary><strong>Contributing</strong></summary> | ||
**Params** | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
* `str` **{String}**: Template string to compile. | ||
* `options` **{Object}**: Options or settings to pass to engine. | ||
* `cb` **{Function}**: Callback function | ||
</details> | ||
**Example** | ||
<details> | ||
<summary><strong>Running Tests</strong></summary> | ||
```js | ||
var engine = require('engine-base'); | ||
engine.compile('<%= name %>', function (err, fn) { | ||
console.log(fn({name: 'Halle'})); //=> 'Halle' | ||
}); | ||
``` | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
### [renderSync](index.js#L113) | ||
Render templates synchronously. | ||
**Params** | ||
* `str` **{Object}**: The string to render. | ||
* `options` **{Object}**: Object of options. | ||
* `returns` **{String}**: Rendered string. | ||
**Example** | ||
```js | ||
var engine = require('engine-base'); | ||
engine.renderSync('<%= name %>', {name: 'Halle'}); | ||
//=> 'Halle' | ||
```sh | ||
$ npm install && npm test | ||
``` | ||
### [render](index.js#L162) | ||
</details> | ||
String support. Render the given `str` and invoke the callback `callback(err, str)`. | ||
<details> | ||
<summary><strong>Building docs</strong></summary> | ||
**Params** | ||
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ | ||
* `str` **{String}** | ||
* `locals` **{Object|Function}**: or callback. | ||
* `callback` **{Function}** | ||
To generate the readme, run the following command: | ||
**Example** | ||
```js | ||
var engine = require('engine-base'); | ||
engine.render('<%= name %>', {name: 'Jon'}, function (err, content) { | ||
console.log(content); //=> 'Jon' | ||
}); | ||
```sh | ||
$ npm install -g verbose/verb#dev verb-generate-readme && verb | ||
``` | ||
### [renderFile](index.js#L190) | ||
</details> | ||
File support. Render a file at the given `filepath` and callback `callback(err, str)`. | ||
### Related projects | ||
**Params** | ||
You might also be interested in these projects: | ||
* `path` **{String}** | ||
* `options` **{Object|Function}**: or callback function. | ||
* `callback` **{Function}** | ||
* [engine](https://www.npmjs.com/package/engine): Template engine based on Lo-Dash template, but adds features like the ability to register helpers… [more](https://github.com/jonschlinkert/engine) | [homepage](https://github.com/jonschlinkert/engine "Template engine based on Lo-Dash template, but adds features like the ability to register helpers and more easily set data to be used as context in templates.") | ||
* [templates](https://www.npmjs.com/package/templates): System for creating and managing template collections, and rendering templates with any node.js template engine… [more](https://github.com/jonschlinkert/templates) | [homepage](https://github.com/jonschlinkert/templates "System for creating and managing template collections, and rendering templates with any node.js template engine. Can be used as the basis for creating a static site generator or blog framework.") | ||
**Example** | ||
### Author | ||
```js | ||
var engine = require('engine-base'); | ||
engine.renderFile('foo/bar/baz.tmpl', {name: 'Halle'}); | ||
//=> 'Halle' | ||
``` | ||
## Related projects | ||
* [engine-cache](https://www.npmjs.com/package/engine-cache): express.js inspired template-engine manager. | [homepage](https://github.com/jonschlinkert/engine-cache) | ||
* [engine-handlebars](https://www.npmjs.com/package/engine-handlebars): Handlebars engine, consolidate.js style but with enhancements. This works with Assemble, express.js, engine-cache or any… [more](https://www.npmjs.com/package/engine-handlebars) | [homepage](https://github.com/jonschlinkert/engine-handlebars) | ||
* [engine-less](https://www.npmjs.com/package/engine-less): Consolidate-style engine for rendering .less files. | [homepage](https://github.com/jonschlinkert/engine-less) | ||
* [engine-lodash](https://www.npmjs.com/package/engine-lodash): Lo-Dash engine, consolidate.js style but with enhancements. Works with Assemble, express.js, engine-cache or any application… [more](https://www.npmjs.com/package/engine-lodash) | [homepage](https://github.com/jonschlinkert/engine-lodash) | ||
* [helper-cache](https://www.npmjs.com/package/helper-cache): Easily register and get helper functions to be passed to any template engine or node.js… [more](https://www.npmjs.com/package/helper-cache) | [homepage](https://github.com/jonschlinkert/helper-cache) | ||
* [template](https://www.npmjs.com/package/template): Render templates using any engine. Supports, layouts, pages, partials and custom template types. Use template… [more](https://www.npmjs.com/package/template) | [homepage](https://github.com/jonschlinkert/template) | ||
* [template-helpers](https://www.npmjs.com/package/template-helpers): Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or… [more](https://www.npmjs.com/package/template-helpers) | [homepage](https://github.com/jonschlinkert/template-helpers) | ||
## Running tests | ||
Install dev dependencies: | ||
```sh | ||
$ npm i -d && npm test | ||
``` | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/engine-base/issues/new). | ||
## Author | ||
**Jon Schlinkert** | ||
+ [github/jonschlinkert](https://github.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
* [GitHub Profile](https://github.com/jonschlinkert) | ||
* [Twitter Profile](https://twitter.com/jonschlinkert) | ||
* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) | ||
## License | ||
### License | ||
Copyright © 2015 Jon Schlinkert | ||
Released under the MIT license. | ||
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT License](LICENSE). | ||
*** | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 08, 2015._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on November 11, 2018._ |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
3
0
1
7181
4
45
83
1
- Removedcomponent-emitter@^1.2.1
- Removeddelimiter-regex@^2.0.0
- Removedengine@^0.1.12
- Removedengine-utils@^0.1.1
- Removedlazy-cache@^2.0.2
- Removedmixin-deep@^1.1.3
- Removedobject.omit@^2.0.1
- Removedobject.pick@^1.2.0
- Removedarr-flatten@1.1.0(transitive)
- Removedassign-deep@0.4.8(transitive)
- Removedassign-symbols@0.1.1(transitive)
- Removedcollection-visit@0.2.3(transitive)
- Removedcomponent-emitter@1.3.1(transitive)
- Removeddelimiter-regex@2.0.0(transitive)
- Removedengine@0.1.12(transitive)
- Removedengine-utils@0.1.1(transitive)
- Removedextend-shallow@1.1.4(transitive)
- Removedfor-in@1.0.2(transitive)
- Removedfor-own@0.1.5(transitive)
- Removedget-value@1.3.1(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-extendable@0.1.11.0.1(transitive)
- Removedis-plain-object@2.0.4(transitive)
- Removedis-primitive@2.0.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisobject@1.0.22.1.03.0.1(transitive)
- Removedkind-of@1.1.02.0.13.2.25.1.0(transitive)
- Removedlazy-cache@0.2.72.0.2(transitive)
- Removedmap-visit@0.1.5(transitive)
- Removedmixin-deep@1.3.2(transitive)
- Removednoncharacters@1.1.0(transitive)
- Removedobject-visit@0.3.4(transitive)
- Removedobject.omit@2.0.1(transitive)
- Removedobject.pick@1.3.0(transitive)
- Removedset-getter@0.1.1(transitive)
- Removedset-value@0.2.0(transitive)
- Removedto-object-path@0.3.0(transitive)