summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/backbone/summary.js36
-rw-r--r--lib/output/base.js19
-rw-r--r--lib/output/json.js7
3 files changed, 50 insertions, 12 deletions
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'),