diff --git a/README.md b/README.md index c7daac9..bd265fc 100755 --- a/README.md +++ b/README.md @@ -111,6 +111,21 @@ gulp.task('default', ['compile']); **trace**: [true|false] *enables tracing info logging (defaults to false)* +**includes**: [string|array] *used to pass inline includes to Twig. (defaults to undefined) [Read more here](https://github.com/justjohn/twig.js/wiki#inline-templates) * + +**getIncludesId**: [function] *can be used to implement a custom getter for the id (essentially the path) for inline templates passed in `includes`. (defaults to returning `filePath`) [Read more here](https://github.com/justjohn/twig.js/wiki#inline-templates)* +```javascript +// default +function(filePath) { + return filePath; +} + +// example implementation +function(filePath) { + return path.relative('./my/source/path', filePath); +} +``` + **extend**: [function (Twig)] *extends Twig with new tags types. The Twig attribute is Twig.js's internal object. [Read more here](https://github.com/justjohn/twig.js/wiki/Extending-twig.js-With-Custom-Tags)* **functions**: [array] *extends Twig with given function objects. (default to undefined)* diff --git a/index.js b/index.js index 9c6eda6..ffe9d4c 100755 --- a/index.js +++ b/index.js @@ -1,18 +1,63 @@ +var Twig = require('twig'); var map = require('map-stream'); var rext = require('replace-ext'); var gutil = require('gulp-util'); +var glob = require('glob'); +var fs = require('fs'); +var _ = { + merge: require('lodash.merge'), + isArray: require('lodash.isarray') +}; const PLUGIN_NAME = 'gulp-twig'; +var twig = Twig.twig, + includeCache = {}; + module.exports = function (options) { 'use strict'; - if (!options) { - options = {}; + + options = _.merge({ + data: {}, + includes: null, + getIncludeId: function(filePath) { + return filePath; + } + }, options || {}); + + // Register includes + // Thanks to https://github.com/backflip/gulp-twig + if (options.includes) { + if (!_.isArray(options.includes)) { + options.includes = [options.includes]; + } + + options.includes.forEach(function(pattern) { + glob.sync(pattern).forEach(function(file) { + var id = options.getIncludeId(file), + changed = fs.statSync(file).mtime, + content; + + // Skip registering if include has not changed since last time + if (includeCache[id] && includeCache[id].getTime() === changed.getTime()) { + return; + } + + includeCache[id] = changed; + + content = fs.readFileSync(file, 'utf8').toString(); + + twig({ + id: id, + data: content + }); + }); + }); } function modifyContents(file, cb) { - var data = file.data || options.data || {}; + var data = file.data || (typeof options.data === 'function' ? options.data(file) : options.data); if (file.isNull()) { return cb(null, file); @@ -21,17 +66,16 @@ module.exports = function (options) { if (file.isStream()) { return cb(new gutil.PluginError(PLUGIN_NAME, "Streaming not supported!")); } - + data._file = file; data._target = { path: rext(file.path, '.html'), relative: rext(file.relative, '.html') }; - var Twig = require('twig'), - twig = Twig.twig, - twigOpts = { - path: file.path, + var twigOpts = { + allowInlineIncludes: true, + data: file.contents.toString(), async: false }, template; diff --git a/package.json b/package.json index cc675e1..278a2ac 100644 --- a/package.json +++ b/package.json @@ -10,10 +10,13 @@ "author": "Simon de Turck (http://www.zimmen.com)", "main": "./index.js", "dependencies": { + "glob": "5.0.6", "gulp-util": "^2.2.14", + "lodash.isarray": "^3.0.4", + "lodash.merge": "^3.3.2", "map-stream": "^0.1.0", "replace-ext": "0.0.1", - "twig": "0.8.2" + "twig": "git://github.com/d-simon/twig.js.git#528dd7ce36e4bdb616c58b786b1a59e646fa24f2" }, "devDependencies": { "mocha": "*",