diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-09-17 15:02:22 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-09-17 15:02:22 +0200 |
commit | d69cc746472684195f5b628d0ee4dc79e10f59e1 (patch) | |
tree | c668eba1bc444a9d1e0f6a461a0759d234a1eb35 | |
parent | be0a4f08e9a45efc4766632a787adbb187e931af (diff) | |
parent | f83874a9ca5877c9b5417e1603df9c9d3ffe19f2 (diff) | |
download | gitbook-d69cc746472684195f5b628d0ee4dc79e10f59e1.zip gitbook-d69cc746472684195f5b628d0ee4dc79e10f59e1.tar.gz gitbook-d69cc746472684195f5b628d0ee4dc79e10f59e1.tar.bz2 |
Merge pull request #440 from GitbookIO/feature/ebookplugins
Apply plugins to ebook generation
-rw-r--r-- | lib/generate/generator.js | 7 | ||||
-rw-r--r-- | lib/generate/json/index.js | 1 | ||||
-rw-r--r-- | lib/generate/page/index.js | 25 | ||||
-rw-r--r-- | lib/generate/plugin.js | 29 | ||||
-rw-r--r-- | lib/generate/site/index.js | 51 | ||||
-rwxr-xr-x | theme/javascript/core/state.js | 1 | ||||
-rwxr-xr-x | theme/javascript/gitbook.js | 5 | ||||
-rw-r--r-- | theme/templates/page.html | 11 | ||||
-rw-r--r-- | theme/templates/site.html | 2 |
9 files changed, 70 insertions, 62 deletions
diff --git a/lib/generate/generator.js b/lib/generate/generator.js index ad5f8ab..5367b8b 100644 --- a/lib/generate/generator.js +++ b/lib/generate/generator.js @@ -8,6 +8,9 @@ var Plugin = require("./plugin"); var BaseGenerator = function(options) { this.options = options; + // Base for assets in plugins + this.pluginAssetsBase = "book"; + this.options.plugins = Plugin.normalizeNames(this.options.plugins); this.options.plugins = _.union(this.options.plugins, this.options.defaultsPlugins); this.plugins = []; @@ -20,7 +23,9 @@ BaseGenerator.prototype.callHook = function(name, data) { BaseGenerator.prototype.loadPlugins = function() { var that = this; - return Plugin.fromList(this.options.plugins, this.options.input, this) + return Plugin.fromList(this.options.plugins, this.options.input, this, { + assetsBase: this.pluginAssetsBase + }) .then(function(_plugins) { that.plugins = _plugins; diff --git a/lib/generate/json/index.js b/lib/generate/json/index.js index 745933b..6a68f68 100644 --- a/lib/generate/json/index.js +++ b/lib/generate/json/index.js @@ -26,7 +26,6 @@ Generator.prototype.convertFile = function(content, input) { return Q() .then(function() { return parse.page(content, { - repo: that.options.githubId, dir: path.dirname(input) || '/' }); }) diff --git a/lib/generate/page/index.js b/lib/generate/page/index.js index bd9b233..9e48a5e 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,20 +37,9 @@ Generator.prototype.convertFile = function(content, input) { progress: parse.progress(this.options.navigation, input) }; - return Q() - .then(function() { - return parse.page(content, { - repo: that.options.githubId, - dir: path.dirname(input) || '/', - outdir: './', - singleFile: true - }); - }) - .then(function(sections) { - json.content = sections; - }) - .then(function() { - that.pages[input] = json; + return this.prepareFile(content, input) + .then(function(page) { + that.pages[input] = page; }); }; @@ -82,10 +74,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..e699a26 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 @@ -181,7 +182,11 @@ Plugin.normalizeNames = function(names) { }; // Extract data from a list of plugin -Plugin.fromList = function(names, root, generator) { +Plugin.fromList = function(names, root, generator, options) { + options = _.defaults(options || {}, { + assetsBase: "book" + }); + var failed = []; // Load plugins @@ -198,7 +203,7 @@ Plugin.fromList = function(names, root, generator) { // Get resources of plugins return Q.all(_.map(plugins, function(plugin) { - return plugin.getResources(); + return plugin.getResources(options.assetsBase); })) // Extract resources out // css, js, etc ... diff --git a/lib/generate/site/index.js b/lib/generate/site/index.js index 0bd2318..a694f39 100644 --- a/lib/generate/site/index.js +++ b/lib/generate/site/index.js @@ -59,10 +59,6 @@ Generator.prototype._writeTemplate = function(tpl, options, output, interpolate) title: that.options.title, description: that.options.description, - githubAuthor: that.options.github ? that.options.github.split("/")[0] : "", - githubId: that.options.github, - githubHost: that.options.githubHost, - glossary: that.options.glossary, summary: that.options.summary, @@ -95,20 +91,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,14 +122,10 @@ 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, dir: path.dirname(_input) || '/', outdir: path.dirname(_input) || '/', }); @@ -150,10 +137,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 +168,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 +245,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: that.pluginAssetsBase + }); }) ); }); diff --git a/theme/javascript/core/state.js b/theme/javascript/core/state.js index 2f7c633..de5c65c 100755 --- a/theme/javascript/core/state.js +++ b/theme/javascript/core/state.js @@ -7,7 +7,6 @@ define([ var $book = $(dom.find(".book")); state.$book = $book; - state.githubId = $book.data("github"); state.level = $book.data("level"); state.basePath = $book.data("basepath"); state.revision = $book.data("revision"); diff --git a/theme/javascript/gitbook.js b/theme/javascript/gitbook.js index 0682b2f..77c7ae0 100755 --- a/theme/javascript/gitbook.js +++ b/theme/javascript/gitbook.js @@ -18,11 +18,6 @@ define([ var $book; $book = state.$book; - if (state.githubId) { - // Initialize storage - storage.setBaseKey(state.githubId); - } - // Init sidebar sidebar.init(); diff --git a/theme/templates/page.html b/theme/templates/page.html index 9c58ab1..5966cda 100644 --- a/theme/templates/page.html +++ b/theme/templates/page.html @@ -4,6 +4,13 @@ {% block style %} <link rel="stylesheet" href="{{ staticBase }}/print.css"> +{% for resource in plugins.resources.css %} + {% if resource.url %} + <link rel="stylesheet" href="{{ resource.url }}"> + {% else %} + <link rel="stylesheet" href="{{ staticBase }}/plugins/{{ resource.path }}"> + {% endif %} +{% endfor %} {% endblock %} @@ -41,7 +48,7 @@ <article id="{{ article.path }}"> <h1 class="book-chapter book-chapter-{{ item.level|lvl }}">{{ item.title }}</h1> {% if pages[item.path] %} - {{ articleContent(pages[item.path].content) }} + {{ articleContent(pages[item.path].sections) }} {% endif %} </article> {% endfor %} @@ -56,7 +63,7 @@ {% for item in progress.chapters %} {% if pages[item.path] %} - {% for section in pages[item.path].content %} + {% for section in pages[item.path].sections %} {% if section.type == "exercise" %} <div class="exercise"> <div class="exercise-header">Exercise #{{ exercise }}</div> diff --git a/theme/templates/site.html b/theme/templates/site.html index 7db7449..cf90c9a 100644 --- a/theme/templates/site.html +++ b/theme/templates/site.html @@ -17,7 +17,7 @@ {% block description %}{% if progress.current.level == "0" %}{{ description }}{% endif %}{% endblock %} {% block content %} - <div class="book" {% if githubId %}data-github="{{ githubId }}"{% endif %} data-level="{{ progress.current.level }}" data-basepath="{{ basePath }}" data-revision="{{ revision }}"> + <div class="book" data-level="{{ progress.current.level }}" data-basepath="{{ basePath }}" data-revision="{{ revision }}"> {% include "includes/book/summary.html" %} <div class="book-body"> <div class="body-inner"> |