Use hook to allow plugins to edit page content during generation (Fixes #176)

This commit is contained in:
Samy Pessé
2014-04-28 17:21:06 +02:00
parent 4481b5f556
commit 38fea06cf9
3 changed files with 19 additions and 12 deletions
+2 -2
View File
@@ -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() {
+8 -8
View File
@@ -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) {
+9 -2
View File
@@ -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"),