diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-09-17 13:00:28 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-09-17 13:00:28 +0200 |
commit | c8c07fe8ce7ccc3b4a83412025123cc05853dc40 (patch) | |
tree | 96e167b96d43d259cbdc6723fd6fafac84920db7 | |
parent | 9dc19026c6c849ee545204b5aecd84b22e7e8d84 (diff) | |
download | gitbook-c8c07fe8ce7ccc3b4a83412025123cc05853dc40.zip gitbook-c8c07fe8ce7ccc3b4a83412025123cc05853dc40.tar.gz gitbook-c8c07fe8ce7ccc3b4a83412025123cc05853dc40.tar.bz2 |
Add base to use plugins in page format
-rw-r--r-- | lib/generate/page/index.js | 21 | ||||
-rw-r--r-- | lib/generate/plugin.js | 21 | ||||
-rw-r--r-- | lib/generate/site/index.js | 49 |
3 files changed, 61 insertions, 30 deletions
diff --git a/lib/generate/page/index.js b/lib/generate/page/index.js index bd9b233..a013219 100644 --- a/lib/generate/page/index.js +++ b/lib/generate/page/index.js @@ -15,6 +15,9 @@ var BaseGenerator = require("../site"); var Generator = function() { BaseGenerator.apply(this, arguments); + // Base for assets in plugins + this.pluginAssetsBase = "ebook"; + // List of pages content this.pages = {}; }; @@ -34,6 +37,14 @@ Generator.prototype.convertFile = function(content, input) { progress: parse.progress(this.options.navigation, input) }; + var _callHook = function(name) { + return that.callHook(name, json) + .then(function(_page) { + json = _page; + return json; + }); + }; + return Q() .then(function() { return parse.page(content, { @@ -46,6 +57,11 @@ Generator.prototype.convertFile = function(content, input) { .then(function(sections) { json.content = sections; }) + .then(function(sections) { + + // Use plugin hook + return _callHook("ebook:page"); + }) .then(function() { that.pages[input] = json; }); @@ -82,10 +98,7 @@ Generator.prototype.finish = function() { // Copy assets .then(function() { - return fs.copy( - path.join(that.options.theme, "assets"), - path.join(that.options.output, "gitbook") - ); + return that.copyAssets(); }); }; diff --git a/lib/generate/plugin.js b/lib/generate/plugin.js index ca34923..1e19ed3 100644 --- a/lib/generate/plugin.js +++ b/lib/generate/plugin.js @@ -65,8 +65,9 @@ Plugin.prototype.normalizeResource = function(resource) { }; // Return resources -Plugin.prototype._getResources = function() { - var book = this.infos.book; +Plugin.prototype._getResources = function(base) { + base = base || "book"; + var book = this.infos[base]; // Nothing specified, fallback to default if (!book) { @@ -84,12 +85,10 @@ Plugin.prototype._getResources = function() { }; // Normalize resources and return them -Plugin.prototype.getResources = function() { +Plugin.prototype.getResources = function(base) { var that = this; - - - return this._getResources() + return this._getResources(base) .then(function(resources) { _.each(RESOURCES, function(resourceType) { @@ -133,10 +132,14 @@ Plugin.prototype.callHook = function(name, data) { }; // Copy plugin assets fodler -Plugin.prototype.copyAssets = function(out) { +Plugin.prototype.copyAssets = function(out, options) { var that = this; + options = _.defaults(options || {}, { + base: "book" + }); - return this.getResources().get('assets') + return this.getResources(options.base) + .get('assets') .then(function(assets) { // Assets are undefined if(!assets) return false; @@ -149,8 +152,6 @@ Plugin.prototype.copyAssets = function(out) { }; - - // Normalize a list of plugin name to use Plugin.normalizeNames = function(names) { // Normalize list to an array diff --git a/lib/generate/site/index.js b/lib/generate/site/index.js index 0bd2318..5d3a57b 100644 --- a/lib/generate/site/index.js +++ b/lib/generate/site/index.js @@ -18,6 +18,9 @@ var Generator = function() { // Attach methods to instance _.bindAll(this); + // Base for assets in plugins + this.pluginAssetsBase = "book"; + this.revision = Date.now(); this.indexer = indexer(); }; @@ -95,20 +98,15 @@ Generator.prototype.indexPage = function(lexed, pagePath) { return Q(); }; -// Convert a markdown file to html -Generator.prototype.convertFile = function(content, _input) { +// Convert a markdown file into a normalized data set +Generator.prototype.prepareFile = function(content, _input) { var that = this; - _output = _input.replace(".md", ".html"); - if (_output == "README.html") _output = "index.html"; - var input = path.join(this.options.input, _input); - var output = path.join(this.options.output, _output); - var basePath = path.relative(path.dirname(output), this.options.output) || "."; var page = { path: _input, - rawPath: input, // path to raw md file + rawPath: input, content: content, progress: parse.progress(this.options.navigation, _input) }; @@ -131,11 +129,8 @@ Generator.prototype.convertFile = function(content, _input) { return parse.lex(page.content); }) .then(function(lexed) { - // Index page in search - return that.indexPage(lexed, _output) - .then(_.constant(lexed)); - }) - .then(function(lexed) { + page.lexed = lexed; + // Get HTML generated sections return parse.page(lexed, { repo: that.options.githubId, @@ -150,10 +145,30 @@ Generator.prototype.convertFile = function(content, _input) { return _callHook("page"); }) .then(function() { + return page; + }); +}; + +// Convert a markdown file to html +Generator.prototype.convertFile = function(content, _input) { + var that = this; + + var _output = _input.replace(".md", ".html"); + if (_output == "README.html") _output = "index.html"; + var output = path.join(this.options.output, _output); + var basePath = path.relative(path.dirname(output), this.options.output) || "."; + + return this.prepareFile(content, _input) + .then(function(page) { + // Index page in search + return that.indexPage(page.lexed, _output).thenResolve(page); + }) + .then(function(page) { + // Write file return that._writeTemplate(that.template, { progress: page.progress, - _input: _input, + _input: page.path, content: page.sections, basePath: basePath, @@ -161,7 +176,7 @@ Generator.prototype.convertFile = function(content, _input) { }, output, function(html) { page.content = html; - return _callHook("page:after").get("content") + return that.callHook("page:after", page).get("content") }); }); }; @@ -238,7 +253,9 @@ Generator.prototype.copyAssets = function() { return Q.all( _.map(that.plugins.list, function(plugin) { var pluginAssets = path.join(that.options.output, "gitbook/plugins/", plugin.name); - return plugin.copyAssets(pluginAssets); + return plugin.copyAssets(pluginAssets, { + base: this.pluginAssetsBase + }); }) ); }); |