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 | |
parent | 72ad872e90ffb8fcb0e33be5036b879df8311dfe (diff) | |
download | gitbook-c218f7d0e30d8088ebd09951691647ffed7fe91d.zip gitbook-c218f7d0e30d8088ebd09951691647ffed7fe91d.tar.gz gitbook-c218f7d0e30d8088ebd09951691647ffed7fe91d.tar.bz2 |
Correctly set template.self in all cases
-rw-r--r-- | lib/output/website/index.js | 6 | ||||
-rw-r--r-- | lib/output/website/templateEnv.js | 4 | ||||
-rw-r--r-- | lib/output/website/themeLoader.js | 44 | ||||
-rw-r--r-- | package.json | 2 |
4 files changed, 39 insertions, 17 deletions
diff --git a/lib/output/website/index.js b/lib/output/website/index.js index 9ef87b6..0a8618c 100644 --- a/lib/output/website/index.js +++ b/lib/output/website/index.js @@ -154,12 +154,6 @@ WebsiteOutput.prototype.renderAsString = function(tpl, context) { // Calcul template name var filename = this.templateName(tpl); - // Extend context - // Setup complete context - context.template = _.extend(context.template || {}, { - self: filename - }); - context = _.extend(context, { plugins: { resources: this.resources diff --git a/lib/output/website/templateEnv.js b/lib/output/website/templateEnv.js index ea2b521..b3009db 100644 --- a/lib/output/website/templateEnv.js +++ b/lib/output/website/templateEnv.js @@ -2,6 +2,8 @@ var _ = require('lodash'); var nunjucks = require('nunjucks'); var path = require('path'); var fs = require('fs'); +var DoExtension = require('nunjucks-do')(nunjucks); + var location = require('../../utils/location'); var defaultFilters = require('../../template/filters'); @@ -24,6 +26,8 @@ function setupTemplateEnv(output, context) { ); var env = new nunjucks.Environment(loader); + env.addExtension('DoExtension', new DoExtension()); + // Add context as global _.each(context, function(value, key) { env.addGlobal(key, value); 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 diff --git a/package.json b/package.json index 148f541..c9d1add 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "npm": "3.8.6", "npmi": "1.0.1", "nunjucks": "2.4.1", - "nunjucks-autoescape": "1.0.1", + "nunjucks-do": "1.0.0", "q": "1.4.1", "read-installed": "^4.0.3", "request": "2.70.0", |