summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-02-22 12:31:02 +0100
committerSamy Pessé <samypesse@gmail.com>2016-02-22 12:31:02 +0100
commit395bd62663614c79181e3e27049345231186339b (patch)
tree638ffc52ad38918280d481d8af1264c5c3606ac9
parentd8fd6430ed5e15d0dee33f730fb6e8e8346c866a (diff)
downloadgitbook-395bd62663614c79181e3e27049345231186339b.zip
gitbook-395bd62663614c79181e3e27049345231186339b.tar.gz
gitbook-395bd62663614c79181e3e27049345231186339b.tar.bz2
Provide "plugins" context to theme templates
-rw-r--r--lib/output/website.js19
-rw-r--r--lib/plugins/index.js11
-rw-r--r--lib/plugins/plugin.js47
3 files changed, 59 insertions, 18 deletions
diff --git a/lib/output/website.js b/lib/output/website.js
index 30e0e8c..c1b0a6c 100644
--- a/lib/output/website.js
+++ b/lib/output/website.js
@@ -32,6 +32,9 @@ function _WebsiteOutput() {
// Plugin instance for the default theme
this.defaultTheme;
+ // Resources loaded from plugins
+ this.resources;
+
// i18n for themes
this.i18n = new I18n();
}
@@ -120,7 +123,7 @@ WebsiteOutput.prototype.prepare = function() {
});
})
- // Copy assets before copyign files from book
+ // Copy assets from themes before copying files from book
.then(function() {
return Promise.serie([
// Assets from the book are already copied
@@ -150,6 +153,14 @@ WebsiteOutput.prototype.prepare = function() {
}
);
});
+ })
+
+ // Load resources for plugins
+ .then(function() {
+ return that.plugins.getResources(that.name)
+ .then(function(resources) {
+ that.resources = resources;
+ });
});
};
@@ -185,7 +196,7 @@ WebsiteOutput.prototype.finish = function() {
// Copy assets from plugins
.then(function() {
- return that.plugins.copyResources('website', that.resolve('gitbook'));
+ return that.plugins.copyResources(that.name, that.resolve('gitbook'));
});
};
@@ -203,6 +214,10 @@ WebsiteOutput.prototype.render = function(tpl, context) {
// Same template but in the theme
theme: path.resolve(templatesPath(this.theme.root), filename)
+ },
+
+ plugins: {
+ resources: this.resources
}
});
diff --git a/lib/plugins/index.js b/lib/plugins/index.js
index ed3aa0a..bed4488 100644
--- a/lib/plugins/index.js
+++ b/lib/plugins/index.js
@@ -107,8 +107,17 @@ PluginsManager.prototype.hook = function(name, input) {
};
// Extract all resources for a namespace
-PluginsManager.prototype.resources = function(namespace) {
+PluginsManager.prototype.getResources = function(namespace) {
+ return Promise.reduce(this.plugins, function(out, plugin) {
+ return plugin.getResources(namespace)
+ .then(function(pluginResources) {
+ _.each(BookPlugin.RESOURCES, function(resourceType) {
+ out[resourceType] = (out[resourceType] || []).concat(pluginResources[resourceType] || []);
+ });
+ return out;
+ });
+ }, {});
};
// Copy all resources for a plugin
diff --git a/lib/plugins/plugin.js b/lib/plugins/plugin.js
index 094c82d..7fb44b2 100644
--- a/lib/plugins/plugin.js
+++ b/lib/plugins/plugin.js
@@ -34,6 +34,9 @@ function BookPlugin(book, pluginId) {
this.packageInfos = undefined;
this.content = undefined;
+ // Cache for resources
+ this._resources = {};
+
_.bindAll(this);
}
@@ -191,25 +194,37 @@ BookPlugin.prototype.hook = function(name, input) {
// Return resources without normalization
BookPlugin.prototype._getResources = function(base) {
- base = base;
- var book = this.content[base];
+ var that = this;
+
+ return Promise()
+ .then(function() {
+ if (that._resources[base]) return that._resources[base];
- // Compatibility with version 1.x.x
- if (base == 'website') book = book || this.content.book;
+ base = base;
+ var book = that.content[base];
- // Nothing specified, fallback to default
- if (!book) {
- return Promise({});
- }
+ // Compatibility with version 1.x.x
+ if (base == 'website') book = book || that.content.book;
- // Dynamic function
- if(typeof book === 'function') {
- // Call giving it the context of our book
- return Promise().then(book.bind(this.book));
- }
+ // Nothing specified, fallback to default
+ if (!book) {
+ return Promise({});
+ }
- // Plain data object
- return Promise(_.cloneDeep(book));
+ // Dynamic function
+ if(typeof book === 'function') {
+ // Call giving it the context of our book
+ return book.call(that.book);
+ }
+
+ // Plain data object
+ return book;
+ })
+
+ .then(function(resources) {
+ that._resources[base] = resources;
+ return _.cloneDeep(resources);
+ });
};
// Normalize a specific resource
@@ -256,3 +271,5 @@ BookPlugin.prototype.getBlocks = function() {
};
module.exports = BookPlugin;
+module.exports.RESOURCES = RESOURCES;
+