diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-10-30 17:52:31 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-10-30 17:52:39 +0100 |
commit | 38c750101c479940d94759bd820d00f90a03f14d (patch) | |
tree | b6d27691b6871d39070c858cd7123e4bcc242000 | |
parent | ba0f379a89dd44fc6d291e03ea69a92773a7ea1a (diff) | |
download | gitbook-38c750101c479940d94759bd820d00f90a03f14d.zip gitbook-38c750101c479940d94759bd820d00f90a03f14d.tar.gz gitbook-38c750101c479940d94759bd820d00f90a03f14d.tar.bz2 |
Fix #487: Use generated toc as base for ebook generation
-rw-r--r-- | lib/generate/ebook/index.js | 6 | ||||
-rw-r--r-- | lib/generate/page/index.js | 22 | ||||
-rw-r--r-- | lib/utils/string.js | 4 | ||||
-rw-r--r-- | theme/templates/page/page.html (renamed from theme/templates/page.html) | 6 | ||||
-rw-r--r-- | theme/templates/page/summary.html | 26 |
5 files changed, 53 insertions, 11 deletions
diff --git a/lib/generate/ebook/index.js b/lib/generate/ebook/index.js index 0acbbc6..bc39e15 100644 --- a/lib/generate/ebook/index.js +++ b/lib/generate/ebook/index.js @@ -39,7 +39,8 @@ Generator.prototype.finish = function() { "--level2-toc": "descendant-or-self::*[contains(concat(' ', normalize-space(@class), ' '), ' book-chapter-2 ')]", "--level3-toc": "descendant-or-self::*[contains(concat(' ', normalize-space(@class), ' '), ' book-chapter-3 ')]", "--no-chapters-in-toc": true, - "--max-levels": "1000" + "--max-levels": "1", + "--breadth-first": true }; if (format == "pdf") { @@ -50,7 +51,6 @@ Generator.prototype.finish = function() { "--margin-right": String(pdfOptions.margin.right), "--margin-top": String(pdfOptions.margin.top), "--margin-bottom": String(pdfOptions.margin.bottom), - "--pdf-add-toc": Boolean(pdfOptions.toc), "--pdf-default-font-size": String(pdfOptions.fontSize), "--pdf-mono-font-size": String(pdfOptions.fontSize), "--paper-size": String(pdfOptions.paperSize), @@ -62,7 +62,7 @@ Generator.prototype.finish = function() { var command = [ "ebook-convert", - path.join(that.options.output, "index.html"), + path.join(that.options.output, "SUMMARY.html"), path.join(that.options.output, "index."+format), stringUtils.optionsToShellArgs(_options) ].join(" "); diff --git a/lib/generate/page/index.js b/lib/generate/page/index.js index fb7c59d..068358c 100644 --- a/lib/generate/page/index.js +++ b/lib/generate/page/index.js @@ -21,10 +21,25 @@ util.inherits(Generator, BaseGenerator); Generator.prototype.loadTemplates = function() { this.template = swig.compileFile( - this.plugins.template("page") || path.resolve(this.options.theme, 'templates/page.html') + this.plugins.template("page") || path.resolve(this.options.theme, 'templates/page/page.html') + ); + this.summaryTemplate = swig.compileFile( + this.plugins.template("page/sumary") || path.resolve(this.options.theme, 'templates/page/summary.html') ); }; +// Generate table of contents +Generator.prototype.writeToc = function() { + var that = this; + var basePath = "."; + + return this._writeTemplate(this.summaryTemplate, { + toc: parse.progress(this.options.navigation, "README.md").chapters, + basePath: basePath, + staticBase: path.join(basePath, "gitbook"), + }, path.join(this.options.output, "SUMMARY.html")); +}; + Generator.prototype.finish = function() { var that = this; var basePath = "."; @@ -34,6 +49,11 @@ Generator.prototype.finish = function() { return Q() + // Write table of contents + .then(function() { + return that.writeToc(); + }) + // Copy cover .then(function() { return that.copyCover(); diff --git a/lib/utils/string.js b/lib/utils/string.js index 417d7af..54c4c66 100644 --- a/lib/utils/string.js +++ b/lib/utils/string.js @@ -11,8 +11,8 @@ function escapeShellArg(arg) { function optionsToShellArgs(options) { return _.chain(options) .map(function(value, key) { - if (value == null || value == false) return null; - if (value == true) return key; + if (value == null || value === false) return null; + if (value === true) return key; return key+"="+escapeShellArg(value); }) .compact() diff --git a/theme/templates/page.html b/theme/templates/page/page.html index 136971a..dbead6e 100644 --- a/theme/templates/page.html +++ b/theme/templates/page/page.html @@ -1,4 +1,4 @@ -{% extends "layout.html" %} +{% extends "../layout.html" %} {% block title %}{{ progress.current.title }} | {{ title }}{% endblock %} @@ -27,10 +27,6 @@ {% endif %} </section> {% endfor %} - - {% if progress.current.next and progress.current.next.path %} - <a href="{{ basePath }}/{{ progress.current.next.path|mdLink }}" /> - {% endif %} </div> {% endblock %} diff --git a/theme/templates/page/summary.html b/theme/templates/page/summary.html new file mode 100644 index 0000000..8bc4d7d --- /dev/null +++ b/theme/templates/page/summary.html @@ -0,0 +1,26 @@ +{% extends "../layout.html" %} + +{% block title %}Table of Contents | {{ title }}{% endblock %} + +{% 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 %} + +{% block content %} +<div class="page"> + <h1>Table of Contents</h1> + <p> + {% for chapter in toc %} + <a href="{{ basePath }}/{{ chapter.path|mdLink }}">{{ chapter.title }}</a><br/> + {% endfor %} + </p> +</div> +{% endblock %} + |