summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-04-19 19:50:32 +0200
committerSamy Pessé <samypesse@gmail.com>2014-04-19 19:50:32 +0200
commit0ac04e15844d83c7fa0f805a256449a7eb88e822 (patch)
tree5e3b6ae351e1e9551d1a1f7dfa1f9bd60714abe4 /lib
parentfb14caf5b3cfc321910ccb3ff57082dd7a43e51a (diff)
downloadgitbook-0ac04e15844d83c7fa0f805a256449a7eb88e822.zip
gitbook-0ac04e15844d83c7fa0f805a256449a7eb88e822.tar.gz
gitbook-0ac04e15844d83c7fa0f805a256449a7eb88e822.tar.bz2
Add call for hooks "init" and "finish"
Diffstat (limited to 'lib')
-rw-r--r--lib/generate/generator.js12
-rw-r--r--lib/generate/index.js3
-rw-r--r--lib/generate/plugin.js22
3 files changed, 34 insertions, 3 deletions
diff --git a/lib/generate/generator.js b/lib/generate/generator.js
index 9d50c92..e303345 100644
--- a/lib/generate/generator.js
+++ b/lib/generate/generator.js
@@ -12,11 +12,19 @@ var BaseGenerator = function(options) {
this.plugins = [];
};
+BaseGenerator.prototype.callHook = function(name) {
+ return this.plugins.hook(name, this);
+};
+
BaseGenerator.prototype.loadPlugins = function() {
+ var that = this;
+
return Plugin.fromList(this.options.plugins)
.then(function(_plugins) {
- this.plugins = _plugins;
- }.bind(this));
+ that.plugins = _plugins;
+
+ return that.callHook("init");
+ });
};
BaseGenerator.prototype.convertFile = function(content, input) {
diff --git a/lib/generate/index.js b/lib/generate/index.js
index b82a8ca..c3803cf 100644
--- a/lib/generate/index.js
+++ b/lib/generate/index.js
@@ -165,6 +165,9 @@ var generate = function(options) {
// Finish gneration
.then(function() {
return generator.finish();
+ })
+ .then(function() {
+ return generator.callHook("finish");
});
}
})
diff --git a/lib/generate/plugin.js b/lib/generate/plugin.js
index f753834..b51d1ff 100644
--- a/lib/generate/plugin.js
+++ b/lib/generate/plugin.js
@@ -67,6 +67,19 @@ Plugin.prototype.resolveFile = function(filename) {
return path.resolve(path.dirname(require.resolve(this.name)), filename);
};
+// Resolve file path
+Plugin.prototype.callHook = function(name, args) {
+ var hookFunc = this.infos.hooks? this.infos.hooks[name] : null;
+ args = _.isArray(args) ? args : [args];
+
+ if (!hookFunc) return Q();
+
+ return Q()
+ .then(function() {
+ return hookFunc.apply(null, args);
+ });
+};
+
// Extract data from a list of plugin
Plugin.fromList = function(names) {
var failed = [];
@@ -98,7 +111,14 @@ Plugin.fromList = function(names) {
return Q({
'list': plugins,
- 'resources': resources
+ 'resources': resources,
+ 'hook': function(name, args) {
+ return _.reduce(plugins, function(prev, plugin) {
+ return prev.then(function() {
+ return plugin.callHook(name, args);
+ })
+ }, Q());
+ }
});
};