diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-27 12:49:08 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-27 12:49:08 +0100 |
commit | 54e1be1e775945b0f7e397c36a1ff1413c63f475 (patch) | |
tree | e08fe8e9738d02aa234ece356cffd11d35c9434e | |
parent | ce259eb4ec74dbcbff10938125ff00ba145befc3 (diff) | |
download | gitbook-54e1be1e775945b0f7e397c36a1ff1413c63f475.zip gitbook-54e1be1e775945b0f7e397c36a1ff1413c63f475.tar.gz gitbook-54e1be1e775945b0f7e397c36a1ff1413c63f475.tar.bz2 |
Parse plugins in book instead of generator
-rw-r--r-- | lib/book.js | 11 | ||||
-rw-r--r-- | lib/generator.js | 18 | ||||
-rw-r--r-- | lib/generators/ebook.js | 6 | ||||
-rw-r--r-- | lib/generators/website.js | 14 | ||||
-rw-r--r-- | lib/plugin.js | 6 |
5 files changed, 23 insertions, 32 deletions
diff --git a/lib/book.js b/lib/book.js index 2782544..417af37 100644 --- a/lib/book.js +++ b/lib/book.js @@ -70,7 +70,7 @@ var Book = function(root, context, parent) { this.files = []; // List of plugins - this.plugins = []; + this.plugins = {}; // Structure files this.summaryFile = null; @@ -327,7 +327,7 @@ Book.prototype.parsePlugins = function() { var failed = []; // Load plugins - that.plugins = _.map(that.options.plugins, function(plugin) { + var pluginsList = _.map(that.options.plugins, function(plugin) { var plugin = new Plugin(that, plugin.name); that.log.info("load plugin", plugin.name, "...."); @@ -343,7 +343,12 @@ Book.prototype.parsePlugins = function() { if (_.size(failed) > 0) return Q.reject(new Error("Error loading plugins: "+failed.join(",")+". Run 'gitbook install' to install plugins from NPM.")); that.log.info.ok(that.plugins.length+" plugins loaded"); - return Q(); + that.log.debug.ln("normalize plugins list"); + + return Plugin.normalize(pluginsList) + .then(function(_plugins) { + that.plugins = _plugins; + }); }; // Parse readme to extract defaults title and description diff --git a/lib/generator.js b/lib/generator.js index 4ff1373..6557461 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -14,32 +14,18 @@ var BaseGenerator = function(book) { } }); - // Base for assets in plugins - this.pluginAssetsBase = "book"; - _.bindAll(this); }; BaseGenerator.prototype.callHook = function(name, data) { - return this.plugins.hook(name, data); + return this.book.plugins.hook(name, data); }; // Prepare the genertor BaseGenerator.prototype.prepare = function() { var that = this; - return this.preparePlugins() - .then(function() { - return that.callHook("init"); - }); -}; - -BaseGenerator.prototype.preparePlugins = function() { - var that = this; - return Plugin.normalize(that.book.plugins) - .then(function(_plugins) { - that.plugins = _plugins; - }); + return that.callHook("init"); }; // Write a parsed file to the output diff --git a/lib/generators/ebook.js b/lib/generators/ebook.js index 481f547..237e22e 100644 --- a/lib/generators/ebook.js +++ b/lib/generators/ebook.js @@ -20,9 +20,9 @@ var Generator = function(book, format) { util.inherits(Generator, BaseGenerator); Generator.prototype.prepareTemplates = function() { - this.templates["page"] = this.plugins.template("ebook:page") || path.resolve(this.options.theme, 'templates/ebook/page.html'); - this.templates["summary"] = this.plugins.template("ebook:summary") || path.resolve(this.options.theme, 'templates/ebook/summary.html'); - this.templates["glossary"] = this.plugins.template("ebook:glossary") || path.resolve(this.options.theme, 'templates/ebook/glossary.html'); + this.templates["page"] = this.book.plugins.template("ebook:page") || path.resolve(this.options.theme, 'templates/ebook/page.html'); + this.templates["summary"] = this.book.plugins.template("ebook:summary") || path.resolve(this.options.theme, 'templates/ebook/summary.html'); + this.templates["glossary"] = this.book.plugins.template("ebook:glossary") || path.resolve(this.options.theme, 'templates/ebook/glossary.html'); return Q(); }; diff --git a/lib/generators/website.js b/lib/generators/website.js index 4b7595a..480e56b 100644 --- a/lib/generators/website.js +++ b/lib/generators/website.js @@ -57,9 +57,9 @@ Generator.prototype.prepareStyles = function() { // Prepare templates Generator.prototype.prepareTemplates = function() { - this.templates["page"] = this.plugins.template("site:page") || path.resolve(this.options.theme, 'templates/website/page.html'); - this.templates["langs"] = this.plugins.template("site:langs") || path.resolve(this.options.theme, 'templates/website/langs.html'); - this.templates["glossary"] = this.plugins.template("site:glossary") || path.resolve(this.options.theme, 'templates/website/glossary.html'); + this.templates["page"] = this.book.plugins.template("site:page") || path.resolve(this.options.theme, 'templates/website/page.html'); + this.templates["langs"] = this.book.plugins.template("site:langs") || path.resolve(this.options.theme, 'templates/website/langs.html'); + this.templates["glossary"] = this.book.plugins.template("site:glossary") || path.resolve(this.options.theme, 'templates/website/glossary.html'); return Q(); }; @@ -232,9 +232,9 @@ Generator.prototype._writeTemplate = function(tpl, options, output, interpolate) summary: that.book.summary, allNavigation: that.book.navigation, - plugins: that.plugins, + plugins: that.book.plugins, pluginsConfig: JSON.stringify(that.options.pluginsConfig), - htmlSnippet: _.partialRight(that.plugins.html, that, options), + htmlSnippet: _.partialRight(that.book.plugins.html, that, options), options: that.options, @@ -265,10 +265,10 @@ Generator.prototype.copyAssets = function() { // Copy plugins assets .then(function() { return Q.all( - _.map(that.plugins.list, function(plugin) { + _.map(that.book.plugins.list, function(plugin) { var pluginAssets = path.join(that.options.output, "gitbook/plugins/", plugin.name); return plugin.copyAssets(pluginAssets, { - base: that.pluginAssetsBase + base: "book" }); }) ); diff --git a/lib/plugin.js b/lib/plugin.js index 99e8402..ec55513 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -78,7 +78,7 @@ Plugin.prototype._getResources = function(base) { // Dynamic function if(typeof book === "function") { - // Call giving it the context of our generator + // Call giving it the context of our book return Q().then(book.bind(this.book)); } @@ -159,7 +159,7 @@ Plugin.prototype.resolveFile = function(filename) { // Resolve file path Plugin.prototype.callHook = function(name, data) { - // Our generator will be the context to apply + // Our book will be the context to apply var context = this.book; var hookFunc = this.infos.hooks? this.infos.hooks[name] : null; @@ -198,7 +198,7 @@ Plugin.prototype.copyAssets = function(out, options) { // Extract data from a list of plugin -Plugin.normalize = function(plugins, generator, options) { +Plugin.normalize = function(plugins, options) { options = _.defaults(options || {}, { assetsBase: "book" }); |