summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-04-28 17:21:06 +0200
committerSamy Pessé <samypesse@gmail.com>2014-04-28 17:21:06 +0200
commit38fea06cf96360a08a0b704715436d6136797cb6 (patch)
treeadcf9d10770e3eed48864ddbc73cf254611c8836
parent4481b5f556937ad9980b1697421fbeb2433f45a0 (diff)
downloadgitbook-38fea06cf96360a08a0b704715436d6136797cb6.zip
gitbook-38fea06cf96360a08a0b704715436d6136797cb6.tar.gz
gitbook-38fea06cf96360a08a0b704715436d6136797cb6.tar.bz2
Use hook to allow plugins to edit page content during generation (Fixes #176)
-rw-r--r--lib/generate/generator.js4
-rw-r--r--lib/generate/plugin.js16
-rw-r--r--lib/generate/site/index.js11
3 files changed, 19 insertions, 12 deletions
diff --git a/lib/generate/generator.js b/lib/generate/generator.js
index 144af23..ecd646c 100644
--- a/lib/generate/generator.js
+++ b/lib/generate/generator.js
@@ -12,8 +12,8 @@ var BaseGenerator = function(options) {
this.plugins = [];
};
-BaseGenerator.prototype.callHook = function(name) {
- return this.plugins.hook(name, this);
+BaseGenerator.prototype.callHook = function(name, data) {
+ return this.plugins.hook(name, this, data);
};
BaseGenerator.prototype.loadPlugins = function() {
diff --git a/lib/generate/plugin.js b/lib/generate/plugin.js
index d92b918..6b30073 100644
--- a/lib/generate/plugin.js
+++ b/lib/generate/plugin.js
@@ -74,15 +74,15 @@ Plugin.prototype.resolveFile = function(filename) {
};
// Resolve file path
-Plugin.prototype.callHook = function(name, args) {
+Plugin.prototype.callHook = function(name, context, data) {
var hookFunc = this.infos.hooks? this.infos.hooks[name] : null;
- args = _.isArray(args) ? args : [args];
+ data = data || {};
- if (!hookFunc) return Q();
+ if (!hookFunc) return Q(data);
return Q()
.then(function() {
- return hookFunc.apply(null, args);
+ return hookFunc.apply(context, [data]);
});
};
@@ -159,12 +159,12 @@ Plugin.fromList = function(names, root) {
return Q({
'list': plugins,
'resources': resources,
- 'hook': function(name, args) {
+ 'hook': function(name, context, data) {
return _.reduce(plugins, function(prev, plugin) {
- return prev.then(function() {
- return plugin.callHook(name, args);
+ return prev.then(function(ret) {
+ return plugin.callHook(name, context, ret);
})
- }, Q());
+ }, Q(data));
},
'template': function(name) {
var withTpl = _.find(plugins, function(plugin) {
diff --git a/lib/generate/site/index.js b/lib/generate/site/index.js
index f8986df..f0ed784 100644
--- a/lib/generate/site/index.js
+++ b/lib/generate/site/index.js
@@ -124,13 +124,20 @@ Generator.prototype.convertFile = function(content, _input) {
});
})
.then(function(sections) {
+ // Use plugin hook
+ return that.callHook("page", {
+ sections: sections,
+ progress: progress
+ })
+ })
+ .then(function(_page) {
that.manifest.add("CACHE", _output);
return that._writeTemplate(that.template, {
- progress: progress,
+ progress: _page.progress,
_input: _input,
- content: sections,
+ content: _page.sections,
basePath: basePath,
staticBase: path.join(basePath, "gitbook"),