summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/plugin.js70
-rw-r--r--test/plugins.js15
-rw-r--r--test/plugins/filters/index.js7
-rw-r--r--test/plugins/filters/package.json9
4 files changed, 93 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) {
diff --git a/test/plugins.js b/test/plugins.js
index f75af34..cd46246 100644
--- a/test/plugins.js
+++ b/test/plugins.js
@@ -35,4 +35,19 @@ describe('Plugins', function () {
done);
});
});
+
+ describe('filters', function() {
+ var plugin = new Plugin(books[0], "filters");
+ plugin.load("./filters", PLUGINS_ROOT);
+
+ it('should valid a plugin', function() {
+ assert(plugin.isValid());
+ });
+
+ it('should return a map of filters', function() {
+ var filters = plugin.getFilters();
+ assert.equal(_.size(filters), 1);
+ assert(filters["hello"]);
+ });
+ });
});
diff --git a/test/plugins/filters/index.js b/test/plugins/filters/index.js
new file mode 100644
index 0000000..0087ba1
--- /dev/null
+++ b/test/plugins/filters/index.js
@@ -0,0 +1,7 @@
+module.exports = {
+ filters: {
+ hello: function(text) {
+ return "Hello "+text;
+ }
+ }
+}; \ No newline at end of file
diff --git a/test/plugins/filters/package.json b/test/plugins/filters/package.json
new file mode 100644
index 0000000..f1d4e45
--- /dev/null
+++ b/test/plugins/filters/package.json
@@ -0,0 +1,9 @@
+{
+ "name": "gitbook-plugin-filters",
+ "description": "Test filters",
+ "main": "index.js",
+ "version": "0.0.1",
+ "engines": {
+ "gitbook": "*"
+ }
+} \ No newline at end of file