diff options
Diffstat (limited to 'lib/backbone/summary.js')
-rw-r--r-- | lib/backbone/summary.js | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/lib/backbone/summary.js b/lib/backbone/summary.js index a1caae7..405c192 100644 --- a/lib/backbone/summary.js +++ b/lib/backbone/summary.js @@ -11,29 +11,35 @@ var BackboneFile = require('./file'); An article represent an entry in the Summary. It's defined by a title, a reference, and children articles, the reference (ref) can be a filename + anchor or an external file (optional) */ -function TOCArticle(summary, title, ref, articles, parent) { - this.summary = summary; - this.title = title; +function TOCArticle(def, parent) { + // Title + this.title = def.title; + + // Parent TOCPart or TOCArticle this.parent = parent; - if (ref && location.isExternal(ref)) { + // As string indicating the overall position + // ex: '1.0.0' + this.level = def.level; + + if (def.path && location.isExternal(def.path)) { throw error.ParsingError(new Error('SUMMARY can only contains relative locations')); } - if (!title) { + if (!def.title) { throw error.ParsingError(new Error('SUMMARY entries should have an non-empty title')); } - var parts = url.parse(ref); - this.ref = ref; + var parts = url.parse(def.path); + this.ref = def.path; if (!this.isExternal()) { this.path = parts.pathname; this.anchor = parts.hash; } - this.articles = _.map(articles || [], function(article) { + this.articles = _.map(def.articles || [], function(article) { if (article instanceof TOCArticle) return article; - return new TOCArticle(article.title, article.ref, article.articles, this); + return new TOCArticle(article, this); }, this); } @@ -48,6 +54,7 @@ TOCArticle.prototype.walk = function(iter) { // Return templating context for an article TOCArticle.prototype.getContext = function() { return { + level: this.level, title: this.title, path: this.isExternal()? undefined : this.path, anchor: this.isExternal()? undefined : this.anchor, @@ -118,12 +125,9 @@ TOCArticle.prototype.map = function(iter) { /* A part of a ToC is a composed of a tree of articles. */ -function TOCPart(summary, part) { - var that = this; - - this.summary = summary; +function TOCPart(part) { this.articles = _.map(part.articles || part.chapters, function(article) { - return new TOCArticle(that.summary, article.title, article.path, article.articles, this); + return new TOCArticle(article, this); }, this); } @@ -164,7 +168,7 @@ Summary.prototype.parse = function(content) { // TODO: update GitBook's parsers to return a list of parts .then(function(part) { - that.parts = [new TOCPart(that, part)]; + that.parts = [new TOCPart(part)]; // Update count of articles that._length = 0; |