summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-30 16:53:38 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-30 16:53:42 +0100
commitd7caf6686e4574472f6648bc9409476059571490 (patch)
tree39693515464b95b8cde7fffd409146d9adb8960f
parent71dad602ec68a105762eff102dba12a98a40a1b1 (diff)
downloadgitbook-d7caf6686e4574472f6648bc9409476059571490.zip
gitbook-d7caf6686e4574472f6648bc9409476059571490.tar.gz
gitbook-d7caf6686e4574472f6648bc9409476059571490.tar.bz2
Fix namespacing of plugins resources
-rw-r--r--lib/generators/ebook.js3
-rw-r--r--lib/generators/website.js15
-rw-r--r--lib/plugin.js12
-rw-r--r--lib/pluginslist.js79
-rw-r--r--test/plugins.js22
-rw-r--r--test/plugins/resources/index.js7
-rw-r--r--theme/templates/ebook/layout.html10
-rw-r--r--theme/templates/website/layout.html10
8 files changed, 99 insertions, 59 deletions
diff --git a/lib/generators/ebook.js b/lib/generators/ebook.js
index e9ca20c..eb84df8 100644
--- a/lib/generators/ebook.js
+++ b/lib/generators/ebook.js
@@ -14,6 +14,9 @@ var Generator = function(book, format) {
// eBook format
this.ebookFormat = format;
+ // Resources namespace
+ this.namespace = "ebook";
+
// Styles to use
this.styles = _.compact(["ebook", this.ebookFormat]);
diff --git a/lib/generators/website.js b/lib/generators/website.js
index 0187416..bedf1af 100644
--- a/lib/generators/website.js
+++ b/lib/generators/website.js
@@ -18,9 +18,12 @@ var pkg = require("../../package.json");
var Generator = function() {
BaseGenerator.apply(this, arguments);
- // revision
+ // Revision
this.revision = new Date();
+ // Resources namespace
+ this.namespace = "website";
+
// Style to integrates in the output
this.styles = ["website"];
@@ -236,9 +239,11 @@ Generator.prototype._writeTemplate = function(tpl, options, output, interpolate)
summary: that.book.summary,
allNavigation: that.book.navigation,
- plugins: that.book.plugins,
+ plugins: {
+ resources: that.book.plugins.resources(that.namespace)
+ },
pluginsConfig: JSON.stringify(that.options.pluginsConfig),
- htmlSnippet: _.partialRight(that.book.plugins.html, that, options),
+ htmlSnippet: _.partial(_.partialRight(that.book.plugins.html, that, options), that.namespace),
options: that.options,
@@ -271,9 +276,7 @@ Generator.prototype.copyAssets = function() {
return Q.all(
_.map(that.book.plugins.list, function(plugin) {
var pluginAssets = path.join(that.options.output, "gitbook/plugins/", plugin.name);
- return plugin.copyAssets(pluginAssets, {
- base: "book"
- });
+ return plugin.copyAssets(pluginAssets, that.namespace);
})
);
});
diff --git a/lib/plugin.js b/lib/plugin.js
index 995385a..ff35a3f 100644
--- a/lib/plugin.js
+++ b/lib/plugin.js
@@ -68,9 +68,12 @@ Plugin.prototype.normalizeResource = function(resource) {
// Return resources
Plugin.prototype._getResources = function(base) {
- base = base || "book";
+ base = base;
var book = this.infos[base];
+ // Compatibility with version 1.x.x
+ if (base == "website") book = book || this.infos["book"];
+
// Nothing specified, fallback to default
if (!book) {
return Q({});
@@ -156,13 +159,10 @@ Plugin.prototype.callHook = function(name, data) {
};
// Copy plugin assets fodler
-Plugin.prototype.copyAssets = function(out, options) {
+Plugin.prototype.copyAssets = function(out, base) {
var that = this;
- options = _.defaults(options || {}, {
- base: "book"
- });
- return this.getResources(options.base)
+ return this.getResources(base)
.get('assets')
.then(function(assets) {
// Assets are undefined
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;
diff --git a/test/plugins.js b/test/plugins.js
index 2351537..c7d1f90 100644
--- a/test/plugins.js
+++ b/test/plugins.js
@@ -48,19 +48,33 @@ describe('Plugins', function () {
assert(plugin.isValid());
});
- it('should return a valid list of resources', function(done) {
+ it('should return a valid list of resources (website)', function(done) {
qdone(
- plugin.getResources()
+ plugin.getResources("website")
.then(function(resources) {
assert.equal(resources["js"].length, 1);
}),
done);
});
- it('should extend books plugins', function() {
- var resources = books[0].plugins.resources;
+ it('should return a valid list of resources (ebook)', function(done) {
+ qdone(
+ plugin.getResources("ebook")
+ .then(function(resources) {
+ assert.equal(resources["css"].length, 1);
+ }),
+ done);
+ });
+
+ it('should extend books plugins (website)', function() {
+ var resources = books[0].plugins.resources("website");
assert.equal(resources["js"].length, 1);
});
+
+ it('should extend books plugins (ebook)', function() {
+ var resources = books[0].plugins.resources("ebook");
+ assert.equal(resources["css"].length, 1);
+ });
});
describe('filters', function() {
diff --git a/test/plugins/resources/index.js b/test/plugins/resources/index.js
index 68b3c39..d98d6f0 100644
--- a/test/plugins/resources/index.js
+++ b/test/plugins/resources/index.js
@@ -1,7 +1,12 @@
module.exports = {
- book: {
+ website: {
js: [
"https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
]
+ },
+ ebook: {
+ css: [
+ "test"
+ ]
}
};
diff --git a/theme/templates/ebook/layout.html b/theme/templates/ebook/layout.html
index ba969cb..2849404 100644
--- a/theme/templates/ebook/layout.html
+++ b/theme/templates/ebook/layout.html
@@ -1,8 +1,8 @@
<!DOCTYPE HTML>
<html lang="{{ language }}" {% block htmlTag %}{% endblock %}>
- {{ htmlSnippet("html:start")|default("") }}
+ {{ htmlSnippet("html:start")|default("")|safe }}
<head>
- {{ htmlSnippet("head:start")|default("") }}
+ {{ htmlSnippet("head:start")|default("")|safe }}
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
@@ -12,11 +12,11 @@
{{ htmlSnippet("head:end")|default("") }}
</head>
<body>
- {{ htmlSnippet("body:start")|default("") }}
+ {{ htmlSnippet("body:start")|default("")|safe }}
{% block style %}{% endblock %}
{% block content %}{% endblock %}
{% block javascript %}{% endblock %}
- {{ htmlSnippet("body:end")|default("") }}
+ {{ htmlSnippet("body:end")|default("")|safe }}
</body>
- {{ htmlSnippet("html:end")|default("") }}
+ {{ htmlSnippet("html:end")|default("")|safe }}
</html>
diff --git a/theme/templates/website/layout.html b/theme/templates/website/layout.html
index 4157634..fca3313 100644
--- a/theme/templates/website/layout.html
+++ b/theme/templates/website/layout.html
@@ -1,6 +1,6 @@
<!DOCTYPE HTML>
<html lang="{{ language }}">
- {{ htmlSnippet("html:start")|default("") }}
+ {{ htmlSnippet("html:start")|default("")|safe }}
<head>
{{ htmlSnippet("head:start")|default("") }}
<meta charset="UTF-8">
@@ -19,14 +19,14 @@
<meta name="identifier" content="{{ options.isbn }}" scheme="ISBN">
{% endif %}
{% block head %}{% endblock %}
- {{ htmlSnippet("head:end")|default("") }}
+ {{ htmlSnippet("head:end")|default("")|safe }}
</head>
<body>
- {{ htmlSnippet("body:start")|default("") }}
+ {{ htmlSnippet("body:start")|default("")|safe }}
{% block style %}{% endblock %}
{% block content %}{% endblock %}
{% block javascript %}{% endblock %}
- {{ htmlSnippet("body:end")|default("") }}
+ {{ htmlSnippet("body:end")|default("")|safe }}
</body>
- {{ htmlSnippet("html:end")|default("") }}
+ {{ htmlSnippet("html:end")|default("")|safe }}
</html>