diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-22 21:52:13 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-22 21:52:13 +0100 |
commit | 0c8513801c131ec6e2c7b348403a4c66a9bf2a11 (patch) | |
tree | 0d30ae9e895d930ad2d7a9bed0c6fbee3d8e59b3 | |
parent | 90789aa5b92a17825c6d98beced5abc3794f4d84 (diff) | |
download | gitbook-0c8513801c131ec6e2c7b348403a4c66a9bf2a11.zip gitbook-0c8513801c131ec6e2c7b348403a4c66a9bf2a11.tar.gz gitbook-0c8513801c131ec6e2c7b348403a4c66a9bf2a11.tar.bz2 |
Write glossary for website
-rw-r--r-- | lib/book.js | 3 | ||||
-rw-r--r-- | lib/generators/site.js | 22 | ||||
-rw-r--r-- | lib/utils/page.js | 9 | ||||
-rw-r--r-- | test/website.js | 6 | ||||
-rw-r--r-- | theme/templates/website/glossary.html | 24 |
5 files changed, 44 insertions, 20 deletions
diff --git a/lib/book.js b/lib/book.js index 036844f..90322f2 100644 --- a/lib/book.js +++ b/lib/book.js @@ -282,7 +282,7 @@ Book.prototype.parseGlossary = function() { return that.findFile(that.config.getStructure("glossary")) .then(function(glossary) { - if (!glossary) return {}; + if (!glossary) return []; // Remove the glossary from the list of files to parse that.files = _.without(that.files, glossary.path); @@ -325,6 +325,7 @@ Book.prototype.parsePage = function(filename) { // Content sections page.sections = pageUtil.normalize(page.sections, { + input: filename, navigation: that.navigation, base: path.dirname(filename) || './', output: path.dirname(filename) || './', diff --git a/lib/generators/site.js b/lib/generators/site.js index d5a0c08..51a6cb3 100644 --- a/lib/generators/site.js +++ b/lib/generators/site.js @@ -89,6 +89,7 @@ Generator.prototype.prepareTemplates = function() { Generator.prototype.finish = function() { return this.copyAssets() .then(this.copyCover) + .then(this.writeGlossary) }; // Normalize a link to .html and convert README -> index @@ -131,16 +132,21 @@ Generator.prototype.writeParsedFile = function(page) { // Write the index for langs Generator.prototype.langsIndex = function(langs) { var that = this; - var basePath = "."; return this._writeTemplate(this.langsTemplate, { - langs: langs, - - basePath: basePath, - staticBase: path.join(basePath, "gitbook"), + langs: langs }, path.join(this.options.output, "index.html")); }; +// Write glossary +Generator.prototype.writeGlossary = function() { + var that = this; + + // No glossary + if (this.book.glossary.length == 0) return Q(); + + return this._writeTemplate(this.glossaryTemplate, {}, path.join(this.options.output, "GLOSSARY.html")); +}; // Convert a page into a normalized data set Generator.prototype.normalizePage = function(page) { @@ -189,7 +195,10 @@ Generator.prototype._writeTemplate = function(tpl, options, output, interpolate) pluginsConfig: JSON.stringify(that.options.pluginsConfig), htmlSnippet: _.partialRight(that.plugins.html, that, options), - options: that.options + options: that.options, + + basePath: ".", + staticBase: path.join(".", "gitbook"), }, options) ); }) @@ -202,7 +211,6 @@ Generator.prototype._writeTemplate = function(tpl, options, output, interpolate) }); }; - // Copy assets Generator.prototype.copyAssets = function() { var that = this; diff --git a/lib/utils/page.js b/lib/utils/page.js index 9a468d6..0168831 100644 --- a/lib/utils/page.js +++ b/lib/utils/page.js @@ -95,9 +95,15 @@ function normalizeHtml(src, options) { // Replace glossayr terms _.each(options.glossary, function(term) { var r = new RegExp( "\\b(" + pregQuote(term.name.toLowerCase()) + ")\\b" , 'gi' ); + var includedInFiles = false; $("*").each(function() { replaceText($, this, r, function(match) { + if (!includedInFiles) { + includedInFiles = true; + term.files = term.files || []; + term.files.push(options.navigation[options.input]); + } return "<a href='"+links.toAbsolute("GLOSSARY.html", options.base, options.output)+"#"+term.id+"' class='glossary-term' title='"+term.description+"'>"+match+"</span>"; }); }); @@ -110,6 +116,9 @@ function normalizeHtml(src, options) { // Adapt page content to be relative to a base folder function normalizePage(sections, options) { options = _.defaults(options || {}, { + // Current file path + input: ".", + // Navigation to use to transform path navigation: {}, diff --git a/test/website.js b/test/website.js index 54dfc8a..064d226 100644 --- a/test/website.js +++ b/test/website.js @@ -22,6 +22,12 @@ describe('Website Generator', function () { }, done); }); + it('should correctly include glossary in website', function(done) { + testGeneration(books[0], "site", function(output) { + assert(fs.existsSync(path.join(output, "GLOSSARY.html"))); + }, done); + }); + it('should correctly generate a multilingual book to website', function(done) { testGeneration(books[2], "site", function(output) { assert(fs.existsSync(path.join(output, "index.html"))); diff --git a/theme/templates/website/glossary.html b/theme/templates/website/glossary.html index 5841f5e..e19d1b9 100644 --- a/theme/templates/website/glossary.html +++ b/theme/templates/website/glossary.html @@ -3,16 +3,16 @@ {% block title %}Glossary | {{ title }}{% endblock %} {% block page_inner %} -{% for item in glossaryIndex %} -<section class="normal glossary" id="{{ item.id }}"> - <h2><a href="#{{ item.id }}">{{ item.name }}</a></h2> - <p>{{ item.description }}</p> - <h4>Index</h4> - <ul class="glossary-index"> - {% for file in item.files %} - <li><a href="{{ basePath }}/{{ file.path|contentLink }}"><span class="level">{{ file.level }}.</span> {{ file.title }}</a></li> - {% endfor %} - </ul> -</section> -{% endfor %} + {% for item in glossary %} + <section class="normal glossary" id="{{ item.id }}"> + <h2><a href="#{{ item.id }}">{{ item.name }}</a></h2> + <p>{{ item.description }}</p> + <h4>Index</h4> + <ul class="glossary-index"> + {% for file in item.files %} + <li><a href="{{ basePath }}/{{ file.path|contentLink }}"><span class="level">{{ file.level }}.</span> {{ file.title }}</a></li> + {% endfor %} + </ul> + </section> + {% endfor %} {% endblock %} |