summaryrefslogtreecommitdiffstats
path: root/lib/models
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-04-27 16:22:17 +0200
committerSamy Pessé <samypesse@gmail.com>2016-04-27 16:22:17 +0200
commite50422bce588ec5a0b5581bf3963b0995138de4c (patch)
tree5128b5759c2bed3d9cac21fa8de5ccc1b3ead7bb /lib/models
parent999882e72327e06dd2fd346ca13eccb1c7e8781f (diff)
downloadgitbook-e50422bce588ec5a0b5581bf3963b0995138de4c.zip
gitbook-e50422bce588ec5a0b5581bf3963b0995138de4c.tar.gz
gitbook-e50422bce588ec5a0b5581bf3963b0995138de4c.tar.bz2
Use code TemplateBlock to highlight code
Diffstat (limited to 'lib/models')
-rw-r--r--lib/models/plugin.js6
-rw-r--r--lib/models/templateBlock.js9
-rw-r--r--lib/models/templateEngine.js22
3 files changed, 15 insertions, 22 deletions
diff --git a/lib/models/plugin.js b/lib/models/plugin.js
index 909ca0b..dd7bc90 100644
--- a/lib/models/plugin.js
+++ b/lib/models/plugin.js
@@ -100,7 +100,7 @@ Plugin.prototype.getFilters = function() {
/**
Return map of blocks
- @return {List<TemplateBlock>}
+ @return {Map<String:TemplateBlock>}
*/
Plugin.prototype.getBlocks = function() {
var blocks = this.getContent().get('blocks');
@@ -109,9 +109,7 @@ Plugin.prototype.getBlocks = function() {
return blocks
.map(function(block, blockName) {
return TemplateBlock.create(blockName, block);
- })
- .valueSeq()
- .toList();
+ });
};
/**
diff --git a/lib/models/templateBlock.js b/lib/models/templateBlock.js
index 4094b89..92a0bf3 100644
--- a/lib/models/templateBlock.js
+++ b/lib/models/templateBlock.js
@@ -1,4 +1,5 @@
var is = require('is');
+var extend = require('extend');
var Immutable = require('immutable');
var Promise = require('../utils/promise');
@@ -72,7 +73,7 @@ TemplateBlock.prototype.getExtensionName = function() {
@return {Nunjucks.Extension}
*/
-TemplateBlock.prototype.toNunjucksExt = function() {
+TemplateBlock.prototype.toNunjucksExt = function(mainContext) {
var that = this;
var name = this.getName();
var endTag = this.getEndTag();
@@ -175,7 +176,11 @@ TemplateBlock.prototype.toNunjucksExt = function() {
Promise()
.then(function() {
- return that.applyBlock(mainBlock, context);
+ var ctx = extend({
+ ctx: context
+ }, mainContext || {});
+
+ return that.applyBlock(mainBlock, ctx);
})
.then(function(result) {
return that.blockResultToHtml(result);
diff --git a/lib/models/templateEngine.js b/lib/models/templateEngine.js
index 114aa73..0d0a015 100644
--- a/lib/models/templateEngine.js
+++ b/lib/models/templateEngine.js
@@ -2,10 +2,10 @@ var nunjucks = require('nunjucks');
var Immutable = require('immutable');
var TemplateEngine = Immutable.Record({
- // List of {TemplateBlock}
- blocks: Immutable.List(),
+ // Map of {TemplateBlock}
+ blocks: Immutable.Map(),
- // List of Extension
+ // Map of Extension
extensions: Immutable.Map(),
// Map of filters: {String} name -> {Function} fn
@@ -68,12 +68,12 @@ TemplateEngine.prototype.getBlock = function(name) {
@return {Nunjucks.Environment}
*/
TemplateEngine.prototype.toNunjucks = function() {
- var that = this;
var loader = this.getLoader();
var blocks = this.getBlocks();
var filters = this.getFilters();
var globals = this.getGlobals();
var extensions = this.getExtensions();
+ var context = this.getContext();
var env = new nunjucks.Environment(
loader,
@@ -95,13 +95,13 @@ TemplateEngine.prototype.toNunjucks = function() {
// Add filters
filters.forEach(function(filterFn, filterName) {
- env.addFilter(filterName, that.bindToContext(filterFn));
+ env.addFilter(filterName, filterFn.bind(context));
});
// Add blocks
blocks.forEach(function(block) {
var extName = block.getExtensionName();
- var Ext = block.toNunjucksExt();
+ var Ext = block.toNunjucksExt(context);
env.addExtension(extName, new Ext());
});
@@ -120,16 +120,6 @@ TemplateEngine.prototype.toNunjucks = function() {
};
/**
- Bind a function to the context
-
- @param {Function} fn
- @return {Function}
-*/
-TemplateEngine.prototype.bindToContext = function(fn) {
- return fn.bind(this.getContext());
-};
-
-/**
Create a template engine
@param {Object} def