mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Enable extension "do"
This commit is contained in:
@@ -5,6 +5,9 @@ var TemplateEngine = Immutable.Record({
|
||||
// List of {TemplateBlock}
|
||||
blocks: Immutable.List(),
|
||||
|
||||
// List of Extension
|
||||
extensions: Immutable.Map(),
|
||||
|
||||
// Map of filters: {String} name -> {Function} fn
|
||||
filters: Immutable.Map(),
|
||||
|
||||
@@ -42,6 +45,10 @@ TemplateEngine.prototype.getContext = function() {
|
||||
return this.get('context');
|
||||
};
|
||||
|
||||
TemplateEngine.prototype.getExtensions = function() {
|
||||
return this.get('extensions');
|
||||
};
|
||||
|
||||
/**
|
||||
Return a block by its name (or undefined)
|
||||
|
||||
@@ -66,6 +73,7 @@ TemplateEngine.prototype.toNunjucks = function() {
|
||||
var blocks = this.getBlocks();
|
||||
var filters = this.getFilters();
|
||||
var globals = this.getGlobals();
|
||||
var extensions = this.getExtensions();
|
||||
|
||||
var env = new nunjucks.Environment(
|
||||
loader,
|
||||
@@ -103,6 +111,11 @@ TemplateEngine.prototype.toNunjucks = function() {
|
||||
env.addGlobal(globalName, globalValue);
|
||||
});
|
||||
|
||||
// Add other extensions
|
||||
extensions.forEach(function(ext, extName) {
|
||||
env.addExtension(extName, ext);
|
||||
});
|
||||
|
||||
return env;
|
||||
};
|
||||
|
||||
@@ -116,4 +129,21 @@ TemplateEngine.prototype.bindToContext = function(fn) {
|
||||
return fn.bind(this.getContext());
|
||||
};
|
||||
|
||||
/**
|
||||
Create a template engine
|
||||
|
||||
@param {Object} def
|
||||
@return {TemplateEngine}
|
||||
*/
|
||||
TemplateEngine.create = function(def) {
|
||||
return new TemplateEngine({
|
||||
blocks: Immutable.List(def.blocks || []),
|
||||
extensions: Immutable.Map(def.extensions || {}),
|
||||
filters: Immutable.Map(def.filters || {}),
|
||||
globals: Immutable.Map(def.globals || {}),
|
||||
context: def.context,
|
||||
loader: def.loader
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = TemplateEngine;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
var path = require('path');
|
||||
var nunjucks = require('nunjucks');
|
||||
var DoExtension = require('nunjucks-do')(nunjucks);
|
||||
|
||||
var TEMPLATES_FOLDER = require('../../constants/templatesFolder');
|
||||
|
||||
@@ -27,8 +29,11 @@ function createTemplateEngine(output) {
|
||||
|
||||
var loader = new Templating.ThemesLoader(tplSearchPaths);
|
||||
|
||||
return new TemplateEngine({
|
||||
loader: loader
|
||||
return TemplateEngine.create({
|
||||
loader: loader,
|
||||
extensions: {
|
||||
'DoExtension': new DoExtension()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user