diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-04-18 16:56:50 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-04-18 16:56:50 +0200 |
commit | c218f7d0e30d8088ebd09951691647ffed7fe91d (patch) | |
tree | e6bbea6f78429311f790a0649dd5971a0b731b19 /lib/output/website/themeLoader.js | |
parent | 72ad872e90ffb8fcb0e33be5036b879df8311dfe (diff) | |
download | gitbook-c218f7d0e30d8088ebd09951691647ffed7fe91d.zip gitbook-c218f7d0e30d8088ebd09951691647ffed7fe91d.tar.gz gitbook-c218f7d0e30d8088ebd09951691647ffed7fe91d.tar.bz2 |
Correctly set template.self in all cases
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 |