blob: 5720cb570e897f58f270ee6bac29d3457bc9165d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
|