summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/generator.js7
-rw-r--r--lib/plugin.js88
2 files changed, 93 insertions, 2 deletions
diff --git a/lib/generator.js b/lib/generator.js
index d8fd38a..f495d8d 100644
--- a/lib/generator.js
+++ b/lib/generator.js
@@ -3,6 +3,8 @@ var path = require("path");
var Q = require("q");
var fs = require("./utils/fs");
+var Plugin = require("./plugin");
+
var BaseGenerator = function(book) {
this.book = book;
@@ -28,7 +30,10 @@ BaseGenerator.prototype.prepare = function() {
BaseGenerator.prototype.preparePlugins = function() {
var that = this;
- return Q();
+ return Plugin.normalize(that.book.plugins)
+ .then(function(_plugins) {
+ that.plugins = _plugins;
+ });
};
// Write a parsed file to the output
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;