diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cli/index.js | 6 | ||||
-rw-r--r-- | lib/config/index.js | 11 | ||||
-rw-r--r-- | lib/config/plugins.js | 71 | ||||
-rw-r--r-- | lib/output/base.js | 2 | ||||
-rw-r--r-- | lib/plugins/index.js | 16 | ||||
-rw-r--r-- | lib/plugins/plugin.js | 16 |
6 files changed, 103 insertions, 19 deletions
diff --git a/lib/cli/index.js b/lib/cli/index.js index d67b78a..4bb926b 100644 --- a/lib/cli/index.js +++ b/lib/cli/index.js @@ -1,4 +1,5 @@ var Output = require('../output/base'); +var PluginsManager = require('../plugins'); var helper = require('./helper'); module.exports = { @@ -34,8 +35,9 @@ module.exports = { options: [ helper.options.log ], - exec: helper.outputCmd(Output, function(output, args) { - + exec: helper.bookCmd(function(book, args) { + var plugins = new PluginsManager(book); + return plugins.install(); }) } diff --git a/lib/config/index.js b/lib/config/index.js index 801dca1..ede45ba 100644 --- a/lib/config/index.js +++ b/lib/config/index.js @@ -2,8 +2,9 @@ var _ = require('lodash'); var semver = require('semver'); var gitbook = require('../gitbook'); -var configDefault = require('./default'); var Promise = require('../utils/promise'); +var configDefault = require('./default'); +var plugins = require('./plugins'); // Config files to tested (sorted) var CONFIG_FILES = [ @@ -56,10 +57,10 @@ Config.prototype.load = function() { } that.options.output = that.options.output || that.book.resolve('_book'); - //that.options.plugins = normalizePluginsList(that.options.plugins); - //that.options.defaultsPlugins = normalizePluginsList(that.options.defaultsPlugins || '', false); - //that.options.plugins = _.union(that.options.plugins, that.options.defaultsPlugins); - //that.options.plugins = _.uniq(that.options.plugins, 'name'); + that.options.plugins = plugins.toList(that.options.plugins); + that.options.defaultsPlugins = plugins.toList(that.options.defaultsPlugins || '', false); + that.options.plugins = _.union(that.options.plugins, that.options.defaultsPlugins); + that.options.plugins = _.uniq(that.options.plugins, 'name'); // Default value for text direction (from language) /*if (!that.options.direction) { diff --git a/lib/config/plugins.js b/lib/config/plugins.js new file mode 100644 index 0000000..43eafdf --- /dev/null +++ b/lib/config/plugins.js @@ -0,0 +1,71 @@ +var _ = require('lodash'); + +// Default plugins added to each books +var DEFAULT_PLUGINS = ['highlight', 'search', 'sharing', 'fontsettings']; + +// Return true if a plugin is a default plugin +function isDefaultPlugin(name, version) { + return _.contains(DEFAULT_PLUGINS, name); +} + +// Normalize a list of plugins to use +function normalizePluginsList(plugins, addDefaults) { + // Normalize list to an array + plugins = _.isString(plugins) ? plugins.split(',') : (plugins || []); + + // Remove empty parts + plugins = _.compact(plugins); + + // Divide as {name, version} to handle format like 'myplugin@1.0.0' + plugins = _.map(plugins, function(plugin) { + if (plugin.name) return plugin; + + var parts = plugin.split('@'); + var name = parts[0]; + var version = parts[1]; + return { + 'name': name, + 'version': version, // optional + 'isDefault': isDefaultPlugin(name, version) + }; + }); + + // List plugins to remove + var toremove = _.chain(plugins) + .filter(function(plugin) { + return plugin.name.length > 0 && plugin.name[0] == '-'; + }) + .map(function(plugin) { + return plugin.name.slice(1); + }) + .value(); + + // Merge with defaults + if (addDefaults !== false) { + _.each(DEFAULT_PLUGINS, function(plugin) { + if (_.find(plugins, { name: plugin })) { + return; + } + + plugins.push({ + 'name': plugin, + 'isDefault': true + }); + }); + } + + // Remove plugin that start with '-' + plugins = _.filter(plugins, function(plugin) { + return !_.contains(toremove, plugin.name) && !(plugin.name.length > 0 && plugin.name[0] == '-'); + }); + + // Remove duplicates + plugins = _.uniq(plugins, 'name'); + + return plugins; +} + +module.exports = { + toList: normalizePluginsList +}; + diff --git a/lib/output/base.js b/lib/output/base.js index 193f636..2b9cdc7 100644 --- a/lib/output/base.js +++ b/lib/output/base.js @@ -20,7 +20,7 @@ function Output(book) { this.log = this.book.log; // Create plugins manager - this.plugins = new PluginsManager(this); + this.plugins = new PluginsManager(this.book); // Create template engine this.template = new TemplateEngine(this); diff --git a/lib/plugins/index.js b/lib/plugins/index.js index 14a323a..f26c63d 100644 --- a/lib/plugins/index.js +++ b/lib/plugins/index.js @@ -10,13 +10,19 @@ PluginsManager is an interface to work with multiple plugins at once: - Call hooks for all plugins, etc */ -function PluginsManager(output) { - this.output = output; - this.book = output.book; +function PluginsManager(book) { + this.book = book; this.log = this.book.log; this.plugins = []; + + _.bindAll(this); } +// Return count of plugins loaded +PluginsManager.prototype.count = function() { + return _.size(this.plugins); +}; + // Returns a plugin by its name PluginsManager.prototype.get = function(name) { return _.find(this.plugins, { @@ -60,12 +66,12 @@ PluginsManager.prototype.load = function(name) { // Setup a plugin // Register its filter, blocks, etc PluginsManager.prototype._setup = function(plugin) { - + this.plugins.push(plugin); }; // Install all plugins for the book PluginsManager.prototype.install = function() { - + this.log.info.ln('installing 0 plugins'); }; module.exports = PluginsManager; diff --git a/lib/plugins/plugin.js b/lib/plugins/plugin.js index 7ce29e4..4ed619c 100644 --- a/lib/plugins/plugin.js +++ b/lib/plugins/plugin.js @@ -1,3 +1,4 @@ +var _ = require('lodash'); var path = require('path'); var resolve = require('resolve'); @@ -15,6 +16,8 @@ function BookPlugin(book, pluginId) { this.packageInfos = undefined; this.content = undefined; + + _.bindAll(this); } // Return true if plugin has been loaded correctly @@ -31,18 +34,19 @@ BookPlugin.prototype.load = function() { } // Try loading plugins from different location - var promise = Promise.some([ + var p = Promise.some([ this.book.resolve('node_modules'), __dirname ], function(baseDir) { // Locate plugin and load pacjage.json try { - var res = resolve.sync(name + '/package.json', { basedir: baseDir }); + var res = resolve.sync(that.npmId + '/package.json', { basedir: baseDir }); that.baseDir = path.dirname(res); that.packageInfos = require(res); } catch (err) { - if (err.code != 'MODULE_NOT_FOUND') throw err; + // Wait on https://github.com/substack/node-resolve/pull/81 to be merged + if (err.message.indexOf('Cannot find module') < 0) throw err; that.packageInfos = undefined; that.content = undefined; @@ -51,14 +55,14 @@ BookPlugin.prototype.load = function() { } // Load plugin JS content - that.content = require(resolve.sync(name, { basedir: baseDir })); + that.content = require(resolve.sync(that.npmId, { basedir: baseDir })); return true; }) .then(that.validate); - this.log.info.log('Loading plugin "' + this.id + '" ...'); - return this.log.info.promise('', promise); + this.log.info('Loading plugin "' + this.id + '" ...'); + return this.log.info.promise(p); }; |