diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-22 14:50:55 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-22 14:50:55 +0100 |
commit | 642ba72a1d0680107357083a79560c2ccc931457 (patch) | |
tree | 34be30a7b84da734703093391483a727d4f19964 /lib/plugins | |
parent | 71144db09c150f6499d977863dbccf12ce05638b (diff) | |
download | gitbook-642ba72a1d0680107357083a79560c2ccc931457.zip gitbook-642ba72a1d0680107357083a79560c2ccc931457.tar.gz gitbook-642ba72a1d0680107357083a79560c2ccc931457.tar.bz2 |
Load all filters and blocks from plugins in output's template
Diffstat (limited to 'lib/plugins')
-rw-r--r-- | lib/plugins/context.js | 3 | ||||
-rw-r--r-- | lib/plugins/index.js | 18 | ||||
-rw-r--r-- | lib/plugins/plugin.js | 31 |
3 files changed, 49 insertions, 3 deletions
diff --git a/lib/plugins/context.js b/lib/plugins/context.js index 51649aa..a192b7b 100644 --- a/lib/plugins/context.js +++ b/lib/plugins/context.js @@ -18,6 +18,9 @@ function pluginCtx(plugin) { // Deprecation error.deprecateField(ctx, 'options', book.config.dump(), '"options" property is deprecated, use config.get(key) instead'); + // Loop for template filters/blocks + error.deprecateField(ctx, 'book', ctx, '"book" property is deprecated, this directly instead'); + return ctx; } diff --git a/lib/plugins/index.js b/lib/plugins/index.js index bed4488..2ebbcc8 100644 --- a/lib/plugins/index.js +++ b/lib/plugins/index.js @@ -69,7 +69,7 @@ PluginsManager.prototype.load = function(name) { PluginsManager.prototype.loadAll = function() { var plugins = _.pluck(this.book.config.get('plugins'), 'name'); - this.log.info.ln('installing', plugins.length, 'plugins'); + this.log.info.ln('loading', plugins.length, 'plugins'); return this.load(plugins); }; @@ -135,4 +135,20 @@ PluginsManager.prototype.copyResources = function(namespace, outputRoot) { }); }; +// Get all filters and blocks +PluginsManager.prototype.getFilters = function() { + return _.reduce(this.plugins, function(out, plugin) { + var filters = plugin.getFilters(); + + return _.extend(out, filters); + }, {}); +}; +PluginsManager.prototype.getBlocks = function() { + return _.reduce(this.plugins, function(out, plugin) { + var blocks = plugin.getBlocks(); + + return _.extend(out, blocks); + }, {}); +}; + module.exports = PluginsManager; diff --git a/lib/plugins/plugin.js b/lib/plugins/plugin.js index c262887..0fbbab1 100644 --- a/lib/plugins/plugin.js +++ b/lib/plugins/plugin.js @@ -266,12 +266,39 @@ BookPlugin.prototype.getResources = function(base) { // Normalize filters and return them BookPlugin.prototype.getFilters = function() { - return this.content.filters || {}; + var that = this; + + return _.mapValues(this.content.filters || {}, function(fn, filter) { + return function() { + var ctx = _.extend( + this, + pluginCtx(that) + ); + + return fn.apply(ctx, arguments); + }; + }); }; // Normalize blocks and return them BookPlugin.prototype.getBlocks = function() { - return this.content.blocks || {}; + var that = this; + + return _.mapValues(this.content.blocks || {}, function(block, blockName) { + block = _.isFunction(block)? { exec: block } : block; + + var fn = block.exec; + block.exec = function() { + var ctx = _.extend( + this, + pluginCtx(that) + ); + + return fn.apply(ctx, arguments); + }; + + return block; + }); }; module.exports = BookPlugin; |