diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-03-11 10:43:37 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-03-11 10:43:37 +0100 |
commit | 514883f0bd7e44ac9f40482a6f4a44bbcd2e6f53 (patch) | |
tree | 7fde330d94f81ab62c63199c017dce1601cc2044 /lib/template | |
parent | 27681aad98c0a891a8cbf1a0bc09ea6d292000c5 (diff) | |
download | gitbook-514883f0bd7e44ac9f40482a6f4a44bbcd2e6f53.zip gitbook-514883f0bd7e44ac9f40482a6f4a44bbcd2e6f53.tar.gz gitbook-514883f0bd7e44ac9f40482a6f4a44bbcd2e6f53.tar.bz2 |
Create new loader for nunjucks to avoid infinite loop
Diffstat (limited to 'lib/template')
-rw-r--r-- | lib/template/fs-loader.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/template/fs-loader.js b/lib/template/fs-loader.js new file mode 100644 index 0000000..5720cb5 --- /dev/null +++ b/lib/template/fs-loader.js @@ -0,0 +1,44 @@ +var _ = require('lodash'); +var fs = require('fs'); +var path = require('path'); +var nunjucks = require('nunjucks'); + +/* + Nunjucks loader similar to FileSystemLoader, but avoid infinite looping +*/ + +var Loader = nunjucks.Loader.extend({ + init: function(searchPaths) { + this.searchPaths = searchPaths.map(path.normalize); + }, + + getSource: function(fullpath) { + fullpath = this.resolve(null, fullpath); + + if(!fullpath) { + return null; + } + + return { + src: fs.readFileSync(fullpath, 'utf-8'), + path: fullpath, + noCache: true + }; + }, + + resolve: function(from, to) { + var resultFolder = _.find(this.searchPaths, function(basePath) { + var p = path.resolve(basePath, to); + + return ( + p.indexOf(basePath) === 0 + && p != from + && fs.existsSync(p) + ); + }); + + return path.resolve(resultFolder, to); + } +}); + +module.exports = Loader; |