summaryrefslogtreecommitdiffstats
path: root/lib/template.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-29 14:09:27 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-29 14:09:27 +0100
commit2bec7c5e10274b260faea3e007b056c19760cc6e (patch)
tree7c16616115430b151093e634afe3e66b8341ffe1 /lib/template.js
parent907ce6dbcdbebd59f4ce5a43073b0dd68056d6c2 (diff)
downloadgitbook-2bec7c5e10274b260faea3e007b056c19760cc6e.zip
gitbook-2bec7c5e10274b260faea3e007b056c19760cc6e.tar.gz
gitbook-2bec7c5e10274b260faea3e007b056c19760cc6e.tar.bz2
Add option to block to not parse output
Diffstat (limited to 'lib/template.js')
-rw-r--r--lib/template.js77
1 files changed, 72 insertions, 5 deletions
diff --git a/lib/template.js b/lib/template.js
index a34f79b..433104c 100644
--- a/lib/template.js
+++ b/lib/template.js
@@ -69,8 +69,56 @@ var TemplateEngine = function(book) {
// List of tags shortcuts
this.shortcuts = [];
+
+ // Map of blocks
+ this.blocks = {};
+
+ // Bind methods
+ _.bindAll(this);
+};
+
+// Process a block in a context
+TemplateEngine.prototype.processBlock = function(blk) {
+ if (_.isString(blk)) blk = { body: blk };
+ blk = _.defaults(blk, {
+ parse: false
+ });
+ blk.id = _.uniqueId("blk");
+
+ // Add to global map
+ this.blocks[blk.id] = blk;
+
+ // If don't parse id, return it as a macro
+ if (!blk.parse) return "%+%"+blk.id+"%+%";
+
+ return blk.body;
+};
+
+// Replace blocks by body after processing
+// This is done to avoid that markdown processer parse the block content
+TemplateEngine.prototype.replaceBlocks = function(content) {
+ var that = this;
+
+ return content.replace(/\%\+\%([\s\S]+?)\%\+\%/g, function(match, key) {
+ var blk = that.blocks[key];
+ if (!blk || blk.parse) return match;
+ return blk.body;
+ });
};
+// Bind a function to a context
+TemplateEngine.prototype.bindContext = function(func) {
+ var that = this;
+
+ return function() {
+ var ctx = {
+ ctx: this.ctx,
+ book: that.book
+ };
+
+ return func.apply(ctx, arguments);
+ };
+};
// Add filter
TemplateEngine.prototype.addFilter = function(filterName, func) {
@@ -81,7 +129,17 @@ TemplateEngine.prototype.addFilter = function(filterName, func) {
} catch(e) {}
this.log.debug.ln("add filter '"+filterName+"'");
- this.env.addFilter(filterName, func, true);
+ this.env.addFilter(filterName, this.bindContext(function() {
+ var ctx = this;
+ var args = Array.prototype.slice.apply(arguments);
+ var callback = _.last(args);
+
+ Q()
+ .then(function() {
+ return func.apply(ctx, args.slice(0, -1));
+ })
+ .nodeify(callback);
+ }), true);
return true;
};
@@ -180,18 +238,21 @@ TemplateEngine.prototype.addBlock = function(name, block) {
var body = args.pop();
var kwargs = args.pop() || {};
+
+ var func = that.bindContext(block.process);
+
Q()
.then(function() {
- return block.process.call({
- ctx: context.ctx,
- book: that.book
- }, {
+ return func.call(context, {
body: body(),
args: args,
kwargs: kwargs,
blocks: _blocks
});
})
+
+ // process the block returned
+ .then(that.processBlock)
.nodeify(callback)
};
};
@@ -284,4 +345,10 @@ TemplateEngine.prototype.renderPage = function(page) {
});
};
+// Post process content
+TemplateEngine.prototype.postProcess = function(content) {
+ return Q(content)
+ .then(this.replaceBlocks);
+};
+
module.exports = TemplateEngine;