diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-24 12:45:25 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-24 12:45:25 +0100 |
commit | b244d506bd49526abbcfe84e2175b3410bbabb8c (patch) | |
tree | 361d53e96ab19e4e5eaca2176022da7d5d43b565 | |
parent | 44f03865c0358d3c109256e58d5e8e6d1091d0b8 (diff) | |
download | gitbook-b244d506bd49526abbcfe84e2175b3410bbabb8c.zip gitbook-b244d506bd49526abbcfe84e2175b3410bbabb8c.tar.gz gitbook-b244d506bd49526abbcfe84e2175b3410bbabb8c.tar.bz2 |
Correctly setup summary instance when file doesn't exist
-rw-r--r-- | lib/backbone/file.js | 7 | ||||
-rw-r--r-- | lib/backbone/summary.js | 64 | ||||
-rw-r--r-- | test/summary.js | 26 |
3 files changed, 67 insertions, 30 deletions
diff --git a/lib/backbone/file.js b/lib/backbone/file.js index 6c46d5e..209e261 100644 --- a/lib/backbone/file.js +++ b/lib/backbone/file.js @@ -21,6 +21,11 @@ BackboneFile.prototype.parse = function() { // To be implemented by each child }; +// Handle case where file doesn't exists +BackboneFile.prototype.parseNotFound = function() { + +}; + // Return true if backbone file exists BackboneFile.prototype.exists = function() { return Boolean(this.path); @@ -48,7 +53,7 @@ BackboneFile.prototype.load = function() { return this.locate() .then(function() { - if (!that.path) return; + if (!that.path) return that.parseNotFound(); that.log.debug.ln(that.type, 'located at', that.path); diff --git a/lib/backbone/summary.js b/lib/backbone/summary.js index 4cfff28..439546b 100644 --- a/lib/backbone/summary.js +++ b/lib/backbone/summary.js @@ -124,12 +124,10 @@ TOCArticle.prototype.map = function(iter) { /* A part of a ToC is a composed of a tree of articles and an optiona title */ -function TOCPart(part, summary) { - if (!(this instanceof TOCPart)) return new TOCPart(part); +function TOCPart(part, parent) { + if (!(this instanceof TOCPart)) return new TOCPart(part, parent); TOCArticle.apply(this, arguments); - - this.summary = summary; } util.inherits(TOCPart, TOCArticle); @@ -138,7 +136,7 @@ TOCPart.prototype.validate = function() { }; // Return a sibling (next or prev) of this part TOCPart.prototype.sibling = function(direction) { - var parts = this.summary.parts; + var parts = this.parent.parts; var pos = _.findIndex(parts, this); if (parts[pos + direction]) { @@ -200,6 +198,11 @@ util.inherits(Summary, BackboneFile); Summary.prototype.type = 'summary'; +// Prepare summary when non existant +Summary.prototype.parseNotFound = function() { + this.update([]); +}; + // Parse the summary content Summary.prototype.parse = function(content) { var that = this; @@ -207,28 +210,7 @@ Summary.prototype.parse = function(content) { return this.parser.summary(content) .then(function(summary) { - that.parts = _.map(summary.parts, function(part) { - return new TOCPart(part, that); - }); - - // Create first part if none - if (that.parts.length == 0) { - that.parts.push(new TOCPart({}, that)); - } - - // Add README as first entry - var firstArticle = that.parts[0].articles[0]; - if (!firstArticle || firstArticle.path != that.book.readme.path) { - that.parts[0].articles.unshift(new TOCArticle({ - title: 'Introduction', - path: that.book.readme.path, - isAutoIntro: true - }, that.parts[0])); - } - that.parts[0].articles[0].isIntroduction = true; - - // Update count of articles and create "level" - that.update(); + that.update(summary.parts); }); }; @@ -297,9 +279,33 @@ Summary.prototype.count = function() { return this._length; }; -// Update the count and indexing of "level" -Summary.prototype.update = function() { +// Prepare the summary +Summary.prototype.update = function(parts) { var that = this; + + + that.parts = _.map(parts, function(part) { + return new TOCPart(part, that); + }); + + // Create first part if none + if (that.parts.length == 0) { + that.parts.push(new TOCPart({}, that)); + } + + // Add README as first entry + var firstArticle = that.parts[0].articles[0]; + if (!firstArticle || firstArticle.path != that.book.readme.path) { + that.parts[0].articles.unshift(new TOCArticle({ + title: 'Introduction', + path: that.book.readme.path, + isAutoIntro: true + }, that.parts[0])); + } + that.parts[0].articles[0].isIntroduction = true; + + + // Update the count and indexing of "level" var prev = undefined; that._length = 0; diff --git a/test/summary.js b/test/summary.js index 68c23b4..a98b6b7 100644 --- a/test/summary.js +++ b/test/summary.js @@ -33,6 +33,32 @@ describe('Summary / Table of contents', function() { }); }); + describe('Non-existant summary', function() { + var book; + + before(function() { + return mock.setupBook({ + 'README.md': 'Hello' + }) + .then(function(_book) { + book = _book; + + return book.readme.load() + .then(function() { + return book.summary.load(); + }); + }); + }); + + it('should add README as first entry', function() { + should(book.summary.getArticle('README.md')).be.ok(); + }); + + it('should correctly count articles', function() { + book.summary.count().should.equal(1); + }); + }); + describe('Non-empty summary list', function() { var book; |