diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-26 21:42:30 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-26 21:42:30 +0100 |
commit | 4250f559b0a2047fb172248dca0bbd0ab3b9c6fb (patch) | |
tree | 8bb5ec8e51886836f4637aedb1b0677299575b4c /lib/plugin.js | |
parent | 3293c394508ef40a65068a19f04deda467431864 (diff) | |
download | gitbook-4250f559b0a2047fb172248dca0bbd0ab3b9c6fb.zip gitbook-4250f559b0a2047fb172248dca0bbd0ab3b9c6fb.tar.gz gitbook-4250f559b0a2047fb172248dca0bbd0ab3b9c6fb.tar.bz2 |
Extract filters from plugins
Diffstat (limited to 'lib/plugin.js')
-rw-r--r-- | lib/plugin.js | 70 |
1 files changed, 62 insertions, 8 deletions
diff --git a/lib/plugin.js b/lib/plugin.js index bcc861d..99e8402 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -101,6 +101,37 @@ Plugin.prototype.getResources = function(base) { }); }; +// Normalize filters and return them +Plugin.prototype.getFilters = function() { + var that = this; + + return _.chain(this.infos.filters || {}) + .map(function(func, key) { + if (!_.isString(key)) { + that.book.log.warn.ln("Invalid filter '"+key+"' in plugin '"+that.name+"'"); + return null; + } + + return [ + key, + function() { + var ctx = this; + var args = Array.prototype.slice.apply(arguments); + var callback = _.last(args); + + Q() + .then(function() { + return func.apply(ctx, args.slice(0, -1)); + }) + .nodeify(callback); + } + ]; + }) + .compact() + .object() + .value(); +}; + // Test if it's a valid plugin Plugin.prototype.isValid = function() { var that = this; @@ -175,6 +206,12 @@ Plugin.normalize = function(plugins, generator, options) { // The raw resources extracted from each plugin var pluginResources; + // Map of resources + var resources = {}; + + // Map of all filters + var filters = {}; + // Get resources of plugins return Q.all(_.map(plugins, function(plugin) { return plugin.getResources(options.assetsBase); @@ -182,17 +219,20 @@ Plugin.normalize = function(plugins, generator, options) { // Extract resources out // css, js, etc ... - .then(function(resources) { - pluginResources = resources; + .then(function(_resources) { + pluginResources = _resources; + }) + .then(function() { // Group by resource types - return _.chain(Plugin.RESOURCES) + resources = _.chain(Plugin.RESOURCES) .map(function(resourceType) { // Get resources from all the plugins for this current type return [ // Key resourceType, + // Value - _.chain(resources) + _.chain(pluginResources) .pluck(resourceType) .compact() .flatten() @@ -202,8 +242,9 @@ Plugin.normalize = function(plugins, generator, options) { .object() .value(); }) + // Extract html snippets - .then(function(resources) { + .then(function() { // Map of html resources by name added by each plugin resources.html = pluginResources.reduce(function(accu, resource) { var html = (resource && resource.html) || {}; @@ -216,14 +257,27 @@ Plugin.normalize = function(plugins, generator, options) { return accu; }, {}); + }) - return resources; + // Extract filters + .then(function() { + _.each(plugins, function(plugin) { + _.each(plugin.getFilters(), function(filterFunc, filterName) { + if (filters[filterName]) { + plugin.book.log.warn.ln("Conflict in filters, '"+filterName+"' is already set"); + } else { + filters[filterName] = filterFunc; + } + }); + }); }) - // Return big multi-plugin object - .then(function(resources) { + + // Return big multi-plugins object + .then(function() { return { 'list': plugins, 'resources': resources, + 'filters': filters, 'hook': function(name, data) { return _.reduce(plugins, function(prev, plugin) { return prev.then(function(ret) { |