summaryrefslogtreecommitdiffstats
path: root/lib
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
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')
-rw-r--r--lib/book.js17
-rw-r--r--lib/plugin.js31
-rw-r--r--lib/template.js77
3 files changed, 88 insertions, 37 deletions
diff --git a/lib/book.js b/lib/book.js
index 24ab505..5099291 100644
--- a/lib/book.js
+++ b/lib/book.js
@@ -524,9 +524,22 @@ Book.prototype.parsePage = function(filename, options) {
return filetype.parser.page(page.content);
})
+ // Post process sections
+ .then(function(_page) {
+ return _.reduce(_page.sections, function(prev, section) {
+ return prev.then(function(_sections) {
+ return that.template.postProcess(section.content || "")
+ .then(function(content) {
+ section.content = content;
+ return _sections.concat([section]);
+ })
+ });
+ }, Q([]));
+ })
+
// Prepare html
- .then(function(_page) {
- return pageUtil.normalize(_page.sections, {
+ .then(function(_sections) {
+ return pageUtil.normalize(_sections, {
book: that,
convertImages: options.convertImages,
input: filename,
diff --git a/lib/plugin.js b/lib/plugin.js
index 2af81c6..995385a 100644
--- a/lib/plugin.js
+++ b/lib/plugin.js
@@ -103,36 +103,7 @@ Plugin.prototype.getResources = function(base) {
// Normalize filters and return them
Plugin.prototype.getFilters = function() {
- var that = this;
-
- return _.chain(this.infos.filters || {})
- .map(function(func, key) {
- if (!_.isString(key)) {
- that.book.log.warn.ln("Invalid filter '"+key+"' in plugin '"+that.name+"'");
- return null;
- }
-
- return [
- key,
- function() {
- var ctx = {
- ctx: this.ctx,
- book: that.book
- };
- var args = Array.prototype.slice.apply(arguments);
- var callback = _.last(args);
-
- Q()
- .then(function() {
- return func.apply(ctx, args.slice(0, -1));
- })
- .nodeify(callback);
- }
- ];
- })
- .compact()
- .object()
- .value();
+ return this.infos.filters || {};
};
// Normalize blocks and return them
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;