From 38fea06cf96360a08a0b704715436d6136797cb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Mon, 28 Apr 2014 17:21:06 +0200 Subject: [PATCH] Use hook to allow plugins to edit page content during generation (Fixes #176) --- lib/generate/generator.js | 4 ++-- lib/generate/plugin.js | 16 ++++++++-------- lib/generate/site/index.js | 11 +++++++++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/generate/generator.js b/lib/generate/generator.js index 144af234c..ecd646cb4 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 d92b91809..6b3007399 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 f8986dfa0..f0ed78482 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"),