summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-19 17:25:16 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-19 17:25:16 +0100
commitbe4d2679a4daf885a470b5f89e8e6488b0c7a820 (patch)
tree4fb8929a39254a275ed75d7bc04786fa7338cc12
parent70189a5ee2da4ae2237178e6e6630b3355857f56 (diff)
downloadgitbook-be4d2679a4daf885a470b5f89e8e6488b0c7a820.zip
gitbook-be4d2679a4daf885a470b5f89e8e6488b0c7a820.tar.gz
gitbook-be4d2679a4daf885a470b5f89e8e6488b0c7a820.tar.bz2
Add base test for generation
-rw-r--r--lib/book.js13
-rw-r--r--lib/generator.js11
-rw-r--r--lib/generators/index.js4
-rw-r--r--lib/generators/json.js76
-rw-r--r--test/generation.js9
-rw-r--r--test/parsing.js2
6 files changed, 102 insertions, 13 deletions
diff --git a/lib/book.js b/lib/book.js
index 062e02c..73f3847 100644
--- a/lib/book.js
+++ b/lib/book.js
@@ -6,7 +6,9 @@ var fs = require("./utils/fs");
var Configuration = require("./configuration");
var TemplateEngine = require("./template");
var Plugin = require("./plugin");
+
var parsers = require("./parsers");
+var generators = require("./generators");
var Book = function(root, options, parent) {
// Root folder of the book
@@ -106,14 +108,21 @@ Book.prototype.parse = function() {
// Generate the output
Book.prototype.generate = function() {
- var that = this;
+ var that = this, generator;
+
if (that.isMultilingual()) return that.generateMultiLingual();
return Q()
// Clean output folder
.then(function() {
- return fs.clean(that.options.output);
+ return fs.remove(that.options.output);
+ })
+
+ // Create generator
+ .then(function() {
+ Generator = generators[that.options.generator];
+ if (!Generator) throw "Generator '"+that.options.generator+"' doesn't exist";
});
};
diff --git a/lib/generator.js b/lib/generator.js
index ef64018..d0db3b6 100644
--- a/lib/generator.js
+++ b/lib/generator.js
@@ -1,7 +1,7 @@
var _ = require("lodash");
var path = require("path");
var Q = require("q");
-var fs = require("./fs");
+var fs = require("./utils/fs");
var BaseGenerator = function(book, options) {
this.options = options;
@@ -19,17 +19,10 @@ BaseGenerator.prototype.load = function() {
return this.loadPlugins();
};
-BaseGenerator.prototype.loadPlugins = function() {
+BaseGenerator.prototype.preparePlugins = function() {
var that = this;
- return Plugin.fromList(this.options.plugins, this.options.input, this, {
- assetsBase: this.pluginAssetsBase
- })
- .then(function(_plugins) {
- that.plugins = _plugins;
- return that.callHook("init");
- });
};
BaseGenerator.prototype.convertFile = function(content, input) {
diff --git a/lib/generators/index.js b/lib/generators/index.js
new file mode 100644
index 0000000..74efb60
--- /dev/null
+++ b/lib/generators/index.js
@@ -0,0 +1,4 @@
+
+module.exports = {
+ json: require("./json")
+};
diff --git a/lib/generators/json.js b/lib/generators/json.js
new file mode 100644
index 0000000..4632a17
--- /dev/null
+++ b/lib/generators/json.js
@@ -0,0 +1,76 @@
+var util = require("util");
+var path = require("path");
+var Q = require("q");
+var _ = require("lodash");
+
+var fs = require("../utils/fs");
+var BaseGenerator = require("../generator");
+
+
+var Generator = function() {
+ BaseGenerator.apply(this, arguments);
+};
+util.inherits(Generator, BaseGenerator);
+
+Generator.prototype.transferFile = function(input) {
+ // ignore
+};
+
+Generator.prototype.convertFile = function(content, input) {
+ var that = this;
+ var json = {
+ progress: parse.progress(this.options.navigation, input)
+ };
+
+ return Q()
+ .then(function() {
+ return parse.page(content, {
+ dir: path.dirname(input) || '/'
+ });
+ })
+ .then(function(parsed) {
+ json.lexed = parsed.lexed;
+ json.sections = parsed.sections;
+ })
+ .then(function() {
+ return fs.writeFile(
+ path.join(that.options.output, input.replace(".md", ".json")),
+ JSON.stringify(json, null, 4)
+ );
+ });
+};
+
+// Generate languages index
+// Contains the first languages readme and langs infos
+Generator.prototype.langsIndex = function(langs) {
+ var that = this;
+
+ if (langs.list.length == 0) return Q.reject("Need at least one language");
+
+ var mainLang = _.first(langs.list).lang;
+ console.log("Main language is", mainLang);
+
+ return Q()
+ .then(function() {
+ return fs.readFile(
+ path.join(that.options.output, mainLang, "README.json")
+ );
+ })
+ .then(function(content) {
+ var json = JSON.parse(content);
+ _.extend(json, {
+ langs: langs.list
+ });
+
+ return fs.writeFile(
+ path.join(that.options.output, "README.json"),
+ JSON.stringify(json, null, 4)
+ );
+ });
+};
+
+Generator.prototype.finish = function() {
+ // ignore
+};
+
+module.exports = Generator;
diff --git a/test/generation.js b/test/generation.js
new file mode 100644
index 0000000..3a86fd5
--- /dev/null
+++ b/test/generation.js
@@ -0,0 +1,9 @@
+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);
+ });
+});
diff --git a/test/parsing.js b/test/parsing.js
index 2c63a02..636b574 100644
--- a/test/parsing.js
+++ b/test/parsing.js
@@ -2,8 +2,6 @@ var path = require('path');
var _ = require('lodash');
var assert = require('assert');
-var Book = require('../').Book;
-
describe('Book parsing', function () {
it('should correctly parse the readme', function() {
assert.equal(book1.options.title, 'My Book');