Complete json generator for multilingual book

Add tests
This commit is contained in:
Samy Pessé
2015-01-19 20:41:34 +01:00
parent 6344928580
commit 2b2b4f46c2
3 changed files with 25 additions and 12 deletions
+8 -4
View File
@@ -108,7 +108,8 @@ Book.prototype.parse = function() {
// Generate the output
Book.prototype.generate = function(generator) {
var that = this, generator;
var that = this;
that.options.generator = generator || that.options.generator;
return Q()
@@ -122,14 +123,14 @@ Book.prototype.generate = function(generator) {
// Create generator
.then(function() {
generator = generator || that.options.generator;
var Generator = generators[generator];
if (!Generator) throw "Generator '"+generator+"' doesn't exist";
if (!Generator) throw "Generator '"+that.options.generator+"' doesn't exist";
generator = new Generator(that);
return generator.load();
})
// Generate content
.then(function() {
if (that.isMultilingual()) {
return that.generateMultiLingual(generator);
@@ -177,9 +178,12 @@ Book.prototype.generateMultiLingual = function(generator) {
// Generate sub-books
return _.reduce(that.books, function(prev, book) {
return prev.then(function() {
return book.generate();
return book.generate(that.options.generator);
});
}, Q());
})
.then(function() {
return generator.langsIndex(that.langs);
});
};
+6 -7
View File
@@ -12,7 +12,7 @@ var Generator = function() {
};
util.inherits(Generator, BaseGenerator);
// Ignore soem methods
// Ignore some methods
Generator.prototype.transferFile = function(input) { };
Generator.prototype.finish = function() { };
@@ -35,13 +35,12 @@ Generator.prototype.writeParsedFile = function(page, input) {
// Generate languages index
// Contains the first languages readme and langs infos
/*Generator.prototype.langsIndex = function(langs) {
Generator.prototype.langsIndex = function(langs) {
var that = this;
if (langs.list.length == 0) return Q.reject("Need at least one language");
if (langs.length == 0) return Q.reject("Need at least one language");
var mainLang = _.first(langs.list).lang;
console.log("Main language is", mainLang);
var mainLang = _.first(langs).lang;
return Q()
.then(function() {
@@ -52,7 +51,7 @@ Generator.prototype.writeParsedFile = function(page, input) {
.then(function(content) {
var json = JSON.parse(content);
_.extend(json, {
langs: langs.list
langs: langs
});
return fs.writeFile(
@@ -60,6 +59,6 @@ Generator.prototype.writeParsedFile = function(page, input) {
JSON.stringify(json, null, 4)
);
});
};*/
};
module.exports = Generator;
+11 -1
View File
@@ -5,7 +5,7 @@ var assert = require('assert');
var fs = require('../lib/utils/fs');
describe('Book generation', function () {
it('should correctly generate a book with json', function(done) {
it('should correctly generate a book to json', function(done) {
var OUTPUT_PATH = book1.options.output;
qdone(
@@ -14,4 +14,14 @@ describe('Book generation', function () {
return fs.remove(OUTPUT_PATH);
}), done);
});
it('should correctly generate a multilingual book to json', function(done) {
var OUTPUT_PATH = book2.options.output;
qdone(
book2.generate("json")
.fin(function() {
return fs.remove(OUTPUT_PATH);
}), done);
});
});