summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-19 16:00:12 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-19 16:00:12 +0100
commit5ed584449007d9c6c816e765ce3e999e14abbf97 (patch)
treef8db5ec4357bd93a541412748997056dc83165bc
parent777963c97c41094555b1ea94b351adf380637922 (diff)
downloadgitbook-5ed584449007d9c6c816e765ce3e999e14abbf97.zip
gitbook-5ed584449007d9c6c816e765ce3e999e14abbf97.tar.gz
gitbook-5ed584449007d9c6c816e765ce3e999e14abbf97.tar.bz2
Parse list of files and add test for it
-rw-r--r--lib/book.js35
-rw-r--r--test/parsing.js8
2 files changed, 43 insertions, 0 deletions
diff --git a/lib/book.js b/lib/book.js
index df19f74..8fbac81 100644
--- a/lib/book.js
+++ b/lib/book.js
@@ -36,6 +36,9 @@ var Book = function(root, options, parent) {
// Sub-books
this.books = [];
+
+ // Files in the book
+ this.files = [];
};
// Initialize and parse the book: config, summary, glossary
@@ -76,6 +79,10 @@ Book.prototype.parse = function() {
if (multilingal) return;
return that.parseGlossary();
})
+ .then(function() {
+ if (multilingal) return;
+ return that.listAllFiles();
+ })
.then(function() {
// Init sub-books
@@ -92,10 +99,28 @@ Book.prototype.parse = function() {
// Generate the output
Book.prototype.generate = function() {
var that = this;
+ if (that.isMultilingual()) return that.generateMultiLingual();
return Q()
+
+ // Clean output folder
.then(function() {
+ return fs.clean(that.options.output);
+ });
+};
+// Generate the output for a multilingual book
+Book.prototype.generateMultiLingual = function() {
+ var that = this;
+
+ return Q()
+ .then(function() {
+ // Generate sub-books
+ return _.reduce(that.books, function(prev, book) {
+ return prev.then(function() {
+ return book.generate();
+ });
+ }, Q());
});
};
@@ -219,6 +244,16 @@ Book.prototype.statFile = function(filename) {
return fs.stat(path.join(this.root, filename));
};
+// List all files in the book
+Book.prototype.listAllFiles = function() {
+ var that = this;
+
+ return fs.list(this.root)
+ .then(function(_files) {
+ that.files = _files;
+ });
+};
+
// Retrun true if the book is a multilingual book
Book.prototype.isMultilingual = function(filename) {
return this.books.length > 0;
diff --git a/test/parsing.js b/test/parsing.js
index db14d13..2c63a02 100644
--- a/test/parsing.js
+++ b/test/parsing.js
@@ -1,4 +1,5 @@
var path = require('path');
+var _ = require('lodash');
var assert = require('assert');
var Book = require('../').Book;
@@ -29,6 +30,13 @@ describe('Book parsing', function () {
assert.equal(LEXED[1].description, "a second test");
});
+ it('should correctly parse files', function() {
+ var FILES = book1.files;
+
+ assert.equal(FILES.length, 4);
+ assert.equal(_.difference(FILES, [ 'GLOSSARY.md', 'README.md', 'SUMMARY.md', 'intro.md' ]).length, 0);
+ });
+
it('should correctly parse the languages', function() {
assert.equal(book2.books.length, 2);
assert(book2.isMultilingual());