diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-03-15 12:37:52 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-03-15 12:37:52 +0100 |
commit | 0b4db49f5752fd2a6f72c3ba57cdb5c1b5b8ae4e (patch) | |
tree | 2b2dede4d212d40992a829cd33151d35a7a7d91d /lib/plugins/index.js | |
parent | c499a8a13a3059e3953727866beb7c986e46dd78 (diff) | |
parent | fbffd54aa244d8a969200b0efbed3d7dc9eb73d0 (diff) | |
download | gitbook-0b4db49f5752fd2a6f72c3ba57cdb5c1b5b8ae4e.zip gitbook-0b4db49f5752fd2a6f72c3ba57cdb5c1b5b8ae4e.tar.gz gitbook-0b4db49f5752fd2a6f72c3ba57cdb5c1b5b8ae4e.tar.bz2 |
Merge pull request #1181 from GitbookIO/feature/allplugins_tpl
All plugins can extend templates/theme
Diffstat (limited to 'lib/plugins/index.js')
-rw-r--r-- | lib/plugins/index.js | 54 |
1 files changed, 41 insertions, 13 deletions
diff --git a/lib/plugins/index.js b/lib/plugins/index.js index 8280542..f897d9c 100644 --- a/lib/plugins/index.js +++ b/lib/plugins/index.js @@ -21,6 +21,11 @@ function PluginsManager(book) { _.bindAll(this); } +// Returns the list of plugins +PluginsManager.prototype.list = function() { + return this.plugins; +}; + // Return count of plugins loaded PluginsManager.prototype.count = function() { return _.size(this.plugins); @@ -33,24 +38,21 @@ PluginsManager.prototype.get = function(name) { }); }; -// Load a plugin, or a list of plugins -PluginsManager.prototype.load = function(name) { +// Load a plugin (could be a BookPlugin or {name,path}) +PluginsManager.prototype.load = function(plugin) { var that = this; - if (_.isArray(name)) { - return Promise.serie(name, function(_name) { - return that.load(_name); - }); + if (_.isArray(plugin)) { + return Promise.serie(plugin, that.load); } return Promise() // Initiate and load the plugin .then(function() { - var plugin; - - if (!_.isString(name)) plugin = name; - else plugin = new BookPlugin(that.book, name); + if (!(plugin instanceof BookPlugin)) { + plugin = new BookPlugin(that.book, plugin.name, plugin.path); + } if (that.get(plugin.id)) { throw new Error('Plugin "'+plugin.id+'" is already loaded'); @@ -68,10 +70,36 @@ PluginsManager.prototype.load = function(name) { // Load all plugins from the book's configuration PluginsManager.prototype.loadAll = function() { - var plugins = _.pluck(this.book.config.get('plugins'), 'name'); + var that = this; + var pluginNames = _.pluck(this.book.config.get('plugins'), 'name'); + + return registry.list(this.book) + .then(function(plugins) { + // Filter out plugins not listed of first level + // (aka pre-installed plugins) + plugins = _.filter(plugins, function(plugin) { + return ( + plugin.depth > 1 || + _.contains(pluginNames, plugin.name) + ); + }); - this.log.info.ln('loading', plugins.length, 'plugins'); - return this.load(plugins); + // Log state + that.log.info.ln(_.size(plugins) + ' are installed'); + if (_.size(pluginNames) != _.size(plugins)) that.log.info.ln(_.size(pluginNames) + ' explicitly listed'); + + // Verify that all plugins are present + var notInstalled = _.filter(pluginNames, function(name) { + return !_.find(plugins, { name: name }); + }); + + if (_.size(notInstalled) > 0) { + throw new Error('Couldn\'t locate plugins "' + notInstalled.join(', ') + '", Run \'gitbook install\' to install plugins from registry.'); + } + + // Load plugins + return that.load(plugins); + }); }; // Setup a plugin |