mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Write file in json generator
This commit is contained in:
@@ -2,3 +2,10 @@ GitBook
|
||||
=======
|
||||
|
||||
:warning: This branch contains some test for a version 2.0.
|
||||
|
||||
ChangeLog with 1.0.0:
|
||||
|
||||
- Hooks `summary` and `glossary` (after and before) have been removed
|
||||
- Exercises and Quizzes are no longer parsed in the markdown parser
|
||||
- You can now also use the `.markdown` extension for markdown files
|
||||
|
||||
|
||||
+58
-2
@@ -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;
|
||||
|
||||
+20
-8
@@ -3,32 +3,40 @@ var path = require("path");
|
||||
var Q = require("q");
|
||||
var fs = require("./utils/fs");
|
||||
|
||||
var BaseGenerator = function(book, options) {
|
||||
this.options = options;
|
||||
var BaseGenerator = function(book) {
|
||||
this.book = book;
|
||||
|
||||
Object.defineProperty(this, "options", {
|
||||
get: function () {
|
||||
return this.book.options;
|
||||
}
|
||||
});
|
||||
|
||||
// Base for assets in plugins
|
||||
this.pluginAssetsBase = "book";
|
||||
};
|
||||
|
||||
BaseGenerator.prototype.callHook = function(name, data) {
|
||||
return this.plugins.hook(name, data);
|
||||
//return this.plugins.hook(name, data);
|
||||
return Q();
|
||||
};
|
||||
|
||||
// Sets up generator
|
||||
// Prepare the genertor
|
||||
BaseGenerator.prototype.load = function() {
|
||||
return this.loadPlugins();
|
||||
return this.preparePlugins();
|
||||
};
|
||||
|
||||
BaseGenerator.prototype.preparePlugins = function() {
|
||||
var that = this;
|
||||
|
||||
|
||||
};
|
||||
|
||||
BaseGenerator.prototype.convertFile = function(content, input) {
|
||||
// Write a parsed file to the output
|
||||
BaseGenerator.prototype.writeParsedFile = function(page, input) {
|
||||
return Q.reject(new Error("Could not convert "+input));
|
||||
};
|
||||
|
||||
// Copy file to the output (non parsable)
|
||||
BaseGenerator.prototype.transferFile = function(input) {
|
||||
return fs.copy(
|
||||
path.join(this.options.input, input),
|
||||
@@ -36,12 +44,14 @@ BaseGenerator.prototype.transferFile = function(input) {
|
||||
);
|
||||
};
|
||||
|
||||
// Copy a folder to the output
|
||||
BaseGenerator.prototype.transferFolder = function(input) {
|
||||
return fs.mkdirp(
|
||||
path.join(this.options.output, input)
|
||||
path.join(this.book.options.output, input)
|
||||
);
|
||||
};
|
||||
|
||||
// Copy the cover picture
|
||||
BaseGenerator.prototype.copyCover = function() {
|
||||
var that = this;
|
||||
|
||||
@@ -63,10 +73,12 @@ BaseGenerator.prototype.copyCover = function() {
|
||||
});
|
||||
};
|
||||
|
||||
// Generate the langs index
|
||||
BaseGenerator.prototype.langsIndex = function(langs) {
|
||||
return Q.reject(new Error("Langs index is not supported in this generator"));
|
||||
};
|
||||
|
||||
// At teh end of the generation
|
||||
BaseGenerator.prototype.finish = function() {
|
||||
return Q.reject(new Error("Could not finish generation"));
|
||||
};
|
||||
|
||||
+12
-20
@@ -17,33 +17,25 @@ Generator.prototype.transferFile = function(input) { };
|
||||
Generator.prototype.finish = function() { };
|
||||
|
||||
// Convert an input file
|
||||
Generator.prototype.convertFile = function(content, input) {
|
||||
Generator.prototype.writeParsedFile = function(page, input) {
|
||||
var that = this;
|
||||
var json = {
|
||||
progress: parse.progress(this.options.navigation, input)
|
||||
progress: [],
|
||||
sections: page.sections
|
||||
};
|
||||
|
||||
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)
|
||||
);
|
||||
});
|
||||
var output = path.basename(input, path.extname(input))+".json";
|
||||
output = path.join(that.options.output, output);
|
||||
|
||||
return fs.writeFile(
|
||||
output,
|
||||
JSON.stringify(json, null, 4)
|
||||
);
|
||||
};
|
||||
|
||||
// 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");
|
||||
@@ -68,6 +60,6 @@ Generator.prototype.langsIndex = function(langs) {
|
||||
JSON.stringify(json, null, 4)
|
||||
);
|
||||
});
|
||||
};
|
||||
};*/
|
||||
|
||||
module.exports = Generator;
|
||||
|
||||
+9
-1
@@ -2,8 +2,16 @@ var path = require('path');
|
||||
var _ = require('lodash');
|
||||
var assert = require('assert');
|
||||
|
||||
var fs = require('../lib/utils/fs');
|
||||
|
||||
describe('Book generation', function () {
|
||||
it('should correctly generate a book with json', function(done) {
|
||||
qdone(book1.generate("json"), done);
|
||||
var OUTPUT_PATH = book1.options.output;
|
||||
|
||||
qdone(
|
||||
book1.generate("json")
|
||||
.fin(function() {
|
||||
return fs.remove(OUTPUT_PATH);
|
||||
}), done);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user