diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-17 13:59:52 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-17 13:59:52 +0100 |
commit | c0196c97a50786cd28c9b72197b39fefb47be333 (patch) | |
tree | 1798de09f97132777613aec6c5f3714d56417bd3 | |
parent | 96105d6ba0ac1bc86bd34088ae4763bf09b3c3ea (diff) | |
download | gitbook-c0196c97a50786cd28c9b72197b39fefb47be333.zip gitbook-c0196c97a50786cd28c9b72197b39fefb47be333.tar.gz gitbook-c0196c97a50786cd28c9b72197b39fefb47be333.tar.bz2 |
Add context for summary
-rw-r--r-- | docs/variables.md | 8 | ||||
-rw-r--r-- | lib/backbone/summary.js | 36 | ||||
-rw-r--r-- | lib/output/base.js | 19 | ||||
-rw-r--r-- | lib/output/json.js | 7 |
4 files changed, 58 insertions, 12 deletions
diff --git a/docs/variables.md b/docs/variables.md index a0fc64e..bf2e88d 100644 --- a/docs/variables.md +++ b/docs/variables.md @@ -10,6 +10,7 @@ The following is a reference of the available data during book's parsing and the | `gitbook` | GitBook specific information | | `page` | Current page specific information | | `file` | File associated with the current page specific information | +| `summary` | Information about the table of contents | ### Book Variables @@ -41,6 +42,13 @@ The following is a reference of the available data during book's parsing and the | `page.previous` | Previous page in the Table of Contents (can be `null`) | | `page.next` | Next page in the Table of Contents (can be `null`) | +#### Table of Contents Variables +| Variable | Description | +| -------- | ----------- | +| `summary.parts` | List of sections in the Table of Contents | + +Thw whole table of contents (`SUMMARY.md`) can be accessed: +`summary.parts[0].articles[0].title` will return the title of the first article. diff --git a/lib/backbone/summary.js b/lib/backbone/summary.js index cef3ae9..5d4d53c 100644 --- a/lib/backbone/summary.js +++ b/lib/backbone/summary.js @@ -45,7 +45,8 @@ TOCArticle.prototype.walk = function(iter) { TOCArticle.prototype.getContext = function() { return { title: this.title, - path: this.path + path: this.path, + anchor: this.anchor }; }; @@ -98,6 +99,12 @@ TOCArticle.prototype.prev = function() { } }; +// Map over all articles +TOCArticle.prototype.map = function(iter) { + return _.map(this.articles, iter); +}; + + /* A part of a ToC is a composed of a tree of articles. */ @@ -121,6 +128,11 @@ TOCPart.prototype.walk = function(iter) { }); }; +// Map over all articles +TOCPart.prototype.map = function(iter) { + return _.map(this.articles, iter); +}; + /* A summary is composed of a list of parts, each composed wit a tree of articles. */ @@ -152,6 +164,28 @@ Summary.prototype.parse = function(content) { }); }; +// Return templating context for the summary +Summary.prototype.getContext = function() { + function onArticle(article) { + var result = article.getContext(); + if (article.hasChildren()) { + result.articles = article.map(onArticle); + } + + return result; + } + + return { + summary: { + parts: _.map(this.parts, function(part) { + return { + articles: part.map(onArticle) + }; + }) + } + }; +}; + // Iterate over all entries of the summary // iter is called with an TOCArticle Summary.prototype.walk = function(iter) { diff --git a/lib/output/base.js b/lib/output/base.js index 6ac3d7e..987179a 100644 --- a/lib/output/base.js +++ b/lib/output/base.js @@ -5,6 +5,7 @@ var path = require('path'); var Promise = require('../utils/promise'); var PluginsManager = require('../plugins'); var TemplateEngine = require('../template'); +var gitbook = require('../gitbook'); /* Output is like a stream interface for a parsed book @@ -117,6 +118,11 @@ Output.prototype.onAsset = function(filename) { }; +// Finish the generation +Output.prototype.finish = function() { + +}; + // Resolve an HTML link Output.prototype.onRelativeLink = function(currentPage, href) { var to = this.book.getPage(href); @@ -146,13 +152,16 @@ Output.prototype.onResolveTemplate = function(from, to) { return path.resolve(path.dirname(from), to); }; -// Finish the generation -Output.prototype.finish = function() { - -}; -Output.createMixin = function(def) { +// ---- Utilities ---- +Output.prototype.getPageContext = function(page) { + return _.extend( + page.getContext(), + gitbook.getContext(), + this.book.getContext(), + this.book.summary.getContext() + ); }; module.exports = Output; diff --git a/lib/output/json.js b/lib/output/json.js index c4317c9..4fb3bbf 100644 --- a/lib/output/json.js +++ b/lib/output/json.js @@ -1,5 +1,4 @@ var _ = require('lodash'); -var gitbook = require('../gitbook'); var conrefsLoader = require('./conrefs'); var JSONOutput = conrefsLoader(); @@ -18,11 +17,7 @@ JSONOutput.prototype.onPage = function(page) { // Write as json .then(function() { - var json = _.extend( - page.getContext(), - gitbook.getContext(), - that.book.getContext() - ); + var json = that.getPageContext(page); return that.writeFile( page.withExtension('.json'), |