mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 10:03:31 +00:00
Add base for multi-languages book
This commit is contained in:
+74
-48
@@ -2,6 +2,7 @@ var Q = require("q");
|
||||
var _ = require("lodash");
|
||||
|
||||
var path = require("path");
|
||||
var swig = require('swig');
|
||||
|
||||
var fs = require("./fs");
|
||||
var parse = require("../parse");
|
||||
@@ -54,60 +55,85 @@ var generate = function(options) {
|
||||
|
||||
// List all files in the repository
|
||||
.then(function() {
|
||||
return fs.list(options.input);
|
||||
return fs.list(options.input)
|
||||
.then(function(_files) {
|
||||
files = _files;
|
||||
})
|
||||
})
|
||||
|
||||
// Check repository is valid
|
||||
.then(function(_files) {
|
||||
files = _files;
|
||||
// Detect multi-languages book
|
||||
.then(function() {
|
||||
if (_.contains(files, "README.md") && _.contains(files, "LANGS.md")) {
|
||||
// Multi-languages book
|
||||
return fs.readFile(path.join(options.input, "LANGS.md"), "utf-8")
|
||||
|
||||
if (!_.contains(files, "SUMMARY.md") || !_.contains(files, "README.md")) {
|
||||
// Generate sub-books
|
||||
.then(function(_langsSummary) {
|
||||
options.langsSummary = parse.summary(_langsSummary);
|
||||
|
||||
// Generated a book for each valid entry
|
||||
return Q.all(
|
||||
_.chain(langsSummary.chapters)
|
||||
.filter(function(entry) {
|
||||
return entry.path != null;
|
||||
})
|
||||
.map(function(entry) {
|
||||
return generate(_.extend({}, options, {
|
||||
input: path.join(options.input, entry.path),
|
||||
output: path.join(options.output, entry.path)
|
||||
}));
|
||||
})
|
||||
.value()
|
||||
);
|
||||
});
|
||||
} else if (!_.contains(files, "SUMMARY.md") || !_.contains(files, "README.md")) {
|
||||
// Invalid book
|
||||
return Q.reject(new Error("Invalid gitbook repository, need SUMMARY.md and README.md"));
|
||||
} else {
|
||||
// Generate the book
|
||||
return fs.readFile(path.join(options.input, "SUMMARY.md"), "utf-8")
|
||||
|
||||
// Get summary
|
||||
.then(function(_summary) {
|
||||
options.summary = parse.summary(_summary);
|
||||
|
||||
// Parse navigation
|
||||
options.navigation = parse.navigation(options.summary);
|
||||
})
|
||||
|
||||
// Create the generator
|
||||
.then(function() {
|
||||
generator = new generators[options.generator](options);
|
||||
})
|
||||
|
||||
// Copy file and replace markdown file
|
||||
.then(function() {
|
||||
return Q.all(
|
||||
_.chain(files)
|
||||
.map(function(file) {
|
||||
if (!file) return;
|
||||
|
||||
if (file[file.length -1] == "/") {
|
||||
return Q(generator.transferFolder(file));
|
||||
} else if (path.extname(file) == ".md" && options.navigation[file] != null) {
|
||||
return fs.readFile(path.join(options.input, file), "utf-8")
|
||||
.then(function(content) {
|
||||
return Q(generator.convertFile(content, file));
|
||||
});
|
||||
} else {
|
||||
return Q(generator.transferFile(file));
|
||||
}
|
||||
})
|
||||
.value()
|
||||
);
|
||||
})
|
||||
|
||||
// Finish gneration
|
||||
.then(function() {
|
||||
return generator.finish();
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
// Get summary
|
||||
.then(function() {
|
||||
return fs.readFile(path.join(options.input, "SUMMARY.md"), "utf-8")
|
||||
.then(function(_summary) {
|
||||
options.summary = parse.summary(_summary);
|
||||
|
||||
// Parse navigation
|
||||
options.navigation = parse.navigation(options.summary);
|
||||
});
|
||||
})
|
||||
|
||||
// Create the generator
|
||||
.then(function() {
|
||||
generator = new generators[options.generator](options);
|
||||
})
|
||||
|
||||
// Copy file and replace markdown file
|
||||
.then(function() {
|
||||
return Q.all(
|
||||
_.chain(files)
|
||||
.map(function(file) {
|
||||
if (!file) return;
|
||||
|
||||
if (file[file.length -1] == "/") {
|
||||
return Q(generator.transferFolder(file));
|
||||
} else if (path.extname(file) == ".md" && options.navigation[file] != null) {
|
||||
return fs.readFile(path.join(options.input, file), "utf-8")
|
||||
.then(function(content) {
|
||||
return Q(generator.convertFile(content, file));
|
||||
});
|
||||
} else {
|
||||
return Q(generator.transferFile(file));
|
||||
}
|
||||
})
|
||||
.value()
|
||||
);
|
||||
})
|
||||
|
||||
// Finish gneration
|
||||
.then(function() {
|
||||
return generator.finish();
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user