diff options
Diffstat (limited to 'lib/pluginslist.js')
-rw-r--r-- | lib/pluginslist.js | 79 |
1 files changed, 47 insertions, 32 deletions
diff --git a/lib/pluginslist.js b/lib/pluginslist.js index b3e1ccc..10e21ac 100644 --- a/lib/pluginslist.js +++ b/lib/pluginslist.js @@ -13,14 +13,24 @@ var PluginsList = function(book, plugins) { // List of names of failed plugins this.failed = []; - // List of plugins resources - this.resources = {}; - _.each(Plugin.RESOURCES, function(resourceType) { - this.resources[resourceType] = []; - }, this); - - // Map of html snippets - this.htmlSnippets = {}; + // Namespaces + this.namespaces = _.chain(["website", "ebook"]) + .map(function(namespace) { + return [ + namespace, + { + html: {}, + resources: _.chain(Plugin.RESOURCES) + .map(function(type) { + return [type, []]; + }) + .object() + .value() + } + ]; + }) + .object() + .value(); // Bind methods _.bindAll(this); @@ -36,9 +46,6 @@ PluginsList.prototype.count = function() { // Add and load a plugin PluginsList.prototype.load = function(plugin, options) { var that = this; - options = _.defaults(options || {}, { - assetsBase: "book" - }); if (_.isArray(plugin)) { return _.reduce(plugin, function(prev, p) { @@ -72,26 +79,28 @@ PluginsList.prototype.load = function(plugin, options) { that.book.template.addBlock(blockName, block); }); - return Q() - .then(function() { - return plugin.getResources(options.assetsBase); - }) - - .then(function(plResources) { - // Extract js and css - _.each(Plugin.RESOURCES, function(resourceType) { - that.resources[resourceType] = that.resources[resourceType].concat(plResources[resourceType] || []); + return _.reduce(_.keys(that.namespaces), function(prev, namespaceName) { + return prev.then(function() { + return plugin.getResources(namespaceName) + .then(function(plResources) { + var namespace = that.namespaces[namespaceName]; + + // Extract js and css + _.each(Plugin.RESOURCES, function(resourceType) { + namespace.resources[resourceType] = (namespace.resources[resourceType] || []).concat(plResources[resourceType] || []); + }); + + // Map of html resources by name added by each plugin + _.each(plResources.html || {}, function(value, tag) { + // Turn into function if not one already + if (!_.isFunction(value)) value = _.constant(value); + + namespace.html[tag] = namespace.html[tag] || []; + namespace.html[tag].push(value); + }); + }) }); - - // Map of html resources by name added by each plugin - _.each(plResources.html || {}, function(value, tag) { - // Turn into function if not one already - if (!_.isFunction(value)) value = _.constant(value); - - that.htmlSnippets[tag] = that.htmlSnippets[tag] || []; - that.htmlSnippets[tag].push(value); - }); - }); + }, Q()); }; // Call a hook @@ -117,10 +126,16 @@ PluginsList.prototype.template = function(name) { }; // Return an html snippet -PluginsList.prototype.html = function(tag, context, options) { - return _.map(this.htmlSnippets[tag] || [], function(code) { +PluginsList.prototype.html = function(namespace, tag, context, options) { + var htmlSnippets = this.namespaces[namespace].html[tag]; + return _.map(htmlSnippets || [], function(code) { return code.call(context, options); }).join("\n"); }; +// Return a resources map for a namespace +PluginsList.prototype.resources = function(namespace) { + return this.namespaces[namespace].resources; +}; + module.exports = PluginsList; |