diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-19 17:32:59 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-19 17:32:59 +0100 |
commit | 2dbf9b0d4a88a6461ca60f49f3d66ccd42143be3 (patch) | |
tree | 136b4674ef40a301a3c39cb23297cf969086d425 | |
parent | be4d2679a4daf885a470b5f89e8e6488b0c7a820 (diff) | |
download | gitbook-2dbf9b0d4a88a6461ca60f49f3d66ccd42143be3.zip gitbook-2dbf9b0d4a88a6461ca60f49f3d66ccd42143be3.tar.gz gitbook-2dbf9b0d4a88a6461ca60f49f3d66ccd42143be3.tar.bz2 |
Start adapting json generator
-rw-r--r-- | lib/book.js | 27 | ||||
-rw-r--r-- | lib/generator.js | 12 | ||||
-rw-r--r-- | lib/generators/json.js | 11 | ||||
-rw-r--r-- | test/generation.js | 6 |
4 files changed, 33 insertions, 23 deletions
diff --git a/lib/book.js b/lib/book.js index 73f3847..435e05c 100644 --- a/lib/book.js +++ b/lib/book.js @@ -107,27 +107,34 @@ Book.prototype.parse = function() { }; // Generate the output -Book.prototype.generate = function() { +Book.prototype.generate = function(generator) { var that = this, generator; - if (that.isMultilingual()) return that.generateMultiLingual(); - return Q() // Clean output folder .then(function() { return fs.remove(that.options.output); }) + .then(function() { + return fs.mkdirp(that.options.output); + }) // Create generator .then(function() { - Generator = generators[that.options.generator]; - if (!Generator) throw "Generator '"+that.options.generator+"' doesn't exist"; + generator = generator || that.options.generator; + var Generator = generators[generator]; + if (!Generator) throw "Generator '"+generator+"' doesn't exist"; + generator = new Generator(that); + }) + + .then(function() { + if (that.isMultilingual()) return that.generateMultiLingual(generator); }); }; // Generate the output for a multilingual book -Book.prototype.generateMultiLingual = function() { +Book.prototype.generateMultiLingual = function(generator) { var that = this; return Q() @@ -287,9 +294,15 @@ Book.prototype.listAllFiles = function() { }); }; -// Retrun true if the book is a multilingual book +// Return true if the book is a multilingual book Book.prototype.isMultilingual = function(filename) { return this.books.length > 0; }; +// Return root of the parent +Book.prototype.parentRoot = function() { + if (this.parent) return this.parent.parentRoot(); + return this.root; +}; + module.exports= Book; diff --git a/lib/generator.js b/lib/generator.js index d0db3b6..32ce3b5 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -46,16 +46,16 @@ BaseGenerator.prototype.copyCover = function() { var that = this; return Q.all([ - fs.copy(path.join(this.options.input, "cover.jpg"), path.join(this.options.output, "cover.jpg")), - fs.copy(path.join(this.options.input, "cover_small.jpg"), path.join(this.options.output, "cover_small.jpg")) + fs.copy(path.join(this.book.root, "cover.jpg"), path.join(this.options.output, "cover.jpg")), + fs.copy(path.join(this.book.root, "cover_small.jpg"), path.join(this.options.output, "cover_small.jpg")) ]) .fail(function() { - // If orignally from multi-lang, try copy from originalInput - if (!that.options.originalInput) return; + // If orignaly from multi-lang, try copy from parent + if (!that.isMultilingual()) return; return Q.all([ - fs.copy(path.join(that.options.originalInput, "cover.jpg"), path.join(that.options.output, "cover.jpg")), - fs.copy(path.join(that.options.originalInput, "cover_small.jpg"), path.join(that.options.output, "cover_small.jpg")) + fs.copy(path.join(that.book.parentRoot(), "cover.jpg"), path.join(that.options.output, "cover.jpg")), + fs.copy(path.join(that.book.parentRoot(), "cover_small.jpg"), path.join(that.options.output, "cover_small.jpg")) ]); }) .fail(function(err) { diff --git a/lib/generators/json.js b/lib/generators/json.js index 4632a17..661e587 100644 --- a/lib/generators/json.js +++ b/lib/generators/json.js @@ -12,10 +12,11 @@ var Generator = function() { }; util.inherits(Generator, BaseGenerator); -Generator.prototype.transferFile = function(input) { - // ignore -}; +// Ignore soem methods +Generator.prototype.transferFile = function(input) { }; +Generator.prototype.finish = function() { }; +// Convert an input file Generator.prototype.convertFile = function(content, input) { var that = this; var json = { @@ -69,8 +70,4 @@ Generator.prototype.langsIndex = function(langs) { }); }; -Generator.prototype.finish = function() { - // ignore -}; - module.exports = Generator; diff --git a/test/generation.js b/test/generation.js index 3a86fd5..2083ca0 100644 --- a/test/generation.js +++ b/test/generation.js @@ -2,8 +2,8 @@ var path = require('path'); var _ = require('lodash'); var assert = require('assert'); -describe('Book parsing', function () { - it('should correctly generate a book', function(done) { - qdone(book1.generate(), done); +describe('Book generation', function () { + it('should correctly generate a book with json', function(done) { + qdone(book1.generate("json"), done); }); }); |