diff options
Diffstat (limited to 'lib/output/website/themeLoader.js')
-rw-r--r-- | lib/output/website/themeLoader.js | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/lib/output/website/themeLoader.js b/lib/output/website/themeLoader.js index 013b81c..bcfea5a 100644 --- a/lib/output/website/themeLoader.js +++ b/lib/output/website/themeLoader.js @@ -29,13 +29,16 @@ var ThemeLoader = nunjucks.Loader.extend({ if (!fullpath) return null; fullpath = this.resolve(null, fullpath); + var templateName = this.getTemplateName(fullpath); if(!fullpath) { return null; } return { - src: fs.readFileSync(fullpath, 'utf-8'), + src: '{% do %}template = template || {}; template.stack = template.stack || []; template.stack.push(template.self); template.self = ' + JSON.stringify(templateName) + '{% enddo %}\n' + + fs.readFileSync(fullpath, 'utf-8') + + '\n{% do %}template.self = template.stack.pop();{% enddo %}', path: fullpath, noCache: true }; @@ -50,6 +53,34 @@ var ThemeLoader = nunjucks.Loader.extend({ }, /* + Get original search path containing a template + + @param {String} filepath + @return {String} searchPath + */ + getSearchPath: function(filepath) { + return _.chain(this.searchPaths) + .sortBy(function(s) { + return -s.length; + }) + .find(function(basePath) { + return (filepath && filepath.indexOf(basePath) === 0); + }) + .value(); + }, + + /* + Get template name from a filepath + + @param {String} filepath + @return {String} name + */ + getTemplateName: function(filepath) { + var originalSearchPath = this.getSearchPath(filepath); + return originalSearchPath? path.relative(originalSearchPath, filepath) : null; + }, + + /* Resolve a template from a current template @param {String|null} from @@ -65,15 +96,8 @@ var ThemeLoader = nunjucks.Loader.extend({ } // Determine in which search folder we currently are - var originalSearchPath = _.chain(this.searchPaths) - .sortBy(function(s) { - return -s.length; - }) - .find(function(basePath) { - return (from && from.indexOf(basePath) === 0); - }) - .value(); - var originalFilename = originalSearchPath? path.relative(originalSearchPath, from) : null; + var originalSearchPath = this.getSearchPath(from); + var originalFilename = this.getTemplateName(from); // If we are including same file from a different search path // Slice the search paths to avoid including from previous ones |