summaryrefslogtreecommitdiffstats
path: root/lib/book.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-19 18:49:02 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-19 18:49:02 +0100
commit6344928580e521974508d445238c207aecec299b (patch)
treeecf21b32a777115b4fdec1adbdbd7ec987adbd7d /lib/book.js
parent2dbf9b0d4a88a6461ca60f49f3d66ccd42143be3 (diff)
downloadgitbook-6344928580e521974508d445238c207aecec299b.zip
gitbook-6344928580e521974508d445238c207aecec299b.tar.gz
gitbook-6344928580e521974508d445238c207aecec299b.tar.bz2
Write file in json generator
Diffstat (limited to 'lib/book.js')
-rw-r--r--lib/book.js60
1 files changed, 58 insertions, 2 deletions
diff --git a/lib/book.js b/lib/book.js
index 435e05c..3ad302a 100644
--- a/lib/book.js
+++ b/lib/book.js
@@ -126,11 +126,46 @@ Book.prototype.generate = function(generator) {
var Generator = generators[generator];
if (!Generator) throw "Generator '"+generator+"' doesn't exist";
generator = new Generator(that);
+
+ return generator.load();
})
.then(function() {
- if (that.isMultilingual()) return that.generateMultiLingual(generator);
- });
+ if (that.isMultilingual()) {
+ return that.generateMultiLingual(generator);
+ } else {
+ // Copy file and replace markdown file
+ return Q.all(
+ _.chain(that.files)
+ .map(function(file) {
+ if (!file) return;
+
+ if (file[file.length -1] == "/") {
+ return Q(generator.transferFolder(file));
+ } else if (_.contains(parsers.extensions, path.extname(file)) && 1) {
+ return that.parsePage(file)
+ .then(function(content) {
+ return Q(generator.writeParsedFile(content, file));
+ });
+ } else {
+ return Q(generator.transferFile(file));
+ }
+ })
+ .value()
+ );
+ }
+ })
+
+ // Finish generation
+ .then(function() {
+ return generator.callHook("finish:before");
+ })
+ .then(function() {
+ return generator.finish();
+ })
+ .then(function() {
+ return generator.callHook("finish");
+ });
};
// Generate the output for a multilingual book
@@ -213,6 +248,9 @@ Book.prototype.parseSummary = function() {
.spread(function(summary, readme) {
if (!summary) throw "No SUMMARY file";
+ // Remove the summary from the list of files to parse
+ that.files = _.without(that.files, summary.path);
+
return that.template.renderFile(summary.path)
.then(function(content) {
return summary.parser.summary(content, readme.path);
@@ -231,6 +269,9 @@ Book.prototype.parseGlossary = function() {
.then(function(glossary) {
if (!glossary) return {};
+ // Remove the glossary from the list of files to parse
+ that.files = _.without(that.files, glossary.path);
+
return that.template.renderFile(glossary.path)
.then(function(content) {
return glossary.parser.glossary(content);
@@ -241,6 +282,21 @@ Book.prototype.parseGlossary = function() {
});
};
+// Parse a page
+Book.prototype.parsePage = function(filename) {
+ var that = this;
+
+ var extension = path.extname(filename);
+ var filetype = parsers.get(extension);
+
+ if (!filetype) return Q.reject(new Error("Can't parse file: "+filename));
+
+ return that.template.renderFile(filename)
+ .then(function(content) {
+ return filetype.parser.page(content);
+ });
+};
+
// Find file that can be parsed with a specific filename
Book.prototype.findFile = function(filename) {
var that = this;