diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-21 13:36:09 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-21 13:36:09 +0100 |
commit | c6e3a272849ce11a44889dd31e6a5889bdd648fe (patch) | |
tree | a208fd2d6de896633f839dc40daebab7dbfaa7ff /lib/plugin.js | |
parent | b5d7e7966f52386486998299e2f96116ca9cef27 (diff) | |
download | gitbook-c6e3a272849ce11a44889dd31e6a5889bdd648fe.zip gitbook-c6e3a272849ce11a44889dd31e6a5889bdd648fe.tar.gz gitbook-c6e3a272849ce11a44889dd31e6a5889bdd648fe.tar.bz2 |
Add normalization of plugins
Diffstat (limited to 'lib/plugin.js')
-rw-r--r-- | lib/plugin.js | 88 |
1 files changed, 87 insertions, 1 deletions
diff --git a/lib/plugin.js b/lib/plugin.js index 6767aed..f558bf8 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -90,7 +90,7 @@ Plugin.prototype.getResources = function(base) { return this._getResources(base) .then(function(resources) { - _.each(RESOURCES, function(resourceType) { + _.each(Plugin.RESOURCES, function(resourceType) { resources[resourceType] = (resources[resourceType] || []).map(that.normalizeResource); }); @@ -150,4 +150,90 @@ Plugin.prototype.copyAssets = function(out, options) { }, _.constant(false)); }; + +// Extract data from a list of plugin +Plugin.normalize = function(plugins, generator, options) { + options = _.defaults(options || {}, { + assetsBase: "book" + }); + + // The raw resources extracted from each plugin + var pluginResources; + + // Get resources of plugins + return Q.all(_.map(plugins, function(plugin) { + return plugin.getResources(options.assetsBase); + })) + + // Extract resources out + // css, js, etc ... + .then(function(resources) { + pluginResources = resources; + // Group by resource types + return _.chain(Plugin.RESOURCES) + .map(function(resourceType) { + // Get resources from all the plugins for this current type + return [ + // Key + resourceType, + // Value + _.chain(resources) + .pluck(resourceType) + .compact() + .flatten() + .value() + ]; + }) + .object() + .value(); + }) + // Extract html snippets + .then(function(resources) { + // Map of html resources by name added by each plugin + resources.html = pluginResources.reduce(function(accu, resource) { + var html = (resource && resource.html) || {}; + _.each(html, function(code, key) { + // Turn into function if not one already + if (!_.isFunction(code)) code = _.constant(code); + // Append + accu[key] = (accu[key] || []).concat([code]); + }); + + return accu; + }, {}); + + return resources; + }) + // Return big multi-plugin object + .then(function(resources) { + return { + 'list': plugins, + 'resources': resources, + 'hook': function(name, data) { + return _.reduce(plugins, function(prev, plugin) { + return prev.then(function(ret) { + return plugin.callHook(name, ret); + }); + }, Q(data)); + }, + 'template': function(name) { + var withTpl = _.find(plugins, function(plugin) { + return ( + plugin.infos.templates && + plugin.infos.templates[name] + ); + }); + + if (!withTpl) return null; + return withTpl.resolveFile(withTpl.infos.templates[name]); + }, + 'html': function(tag, context, options) { + return _.map(resources.html[tag] || [], function(code) { + return code.call(context, options); + }).join("\n"); + } + }; + }); +}; + module.exports = Plugin; |