mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-20 01:25:16 +00:00
Add Book.generateFile to output a pdf
This commit is contained in:
+53
@@ -237,6 +237,59 @@ Book.prototype.generateMultiLingual = function(generator) {
|
||||
});
|
||||
};
|
||||
|
||||
// Extract files from ebook generated
|
||||
Book.prototype.generateFile = function(output, options) {
|
||||
var book = this;
|
||||
|
||||
options = _.defaults(options || {}, {
|
||||
ebookFormat: path.extname(output).slice(1)
|
||||
});
|
||||
output = output || path.resolve(book.root, "book."+options.ebookFormat);
|
||||
|
||||
return fs.tmp.dir()
|
||||
.then(function(tmpDir) {
|
||||
book.config.extend({
|
||||
output: tmpDir
|
||||
});
|
||||
|
||||
return book.generate(options.ebookFormat)
|
||||
.then(function(_options) {
|
||||
var copyFile = function(lang) {
|
||||
var _outputFile = output;
|
||||
var _tmpDir = tmpDir;
|
||||
|
||||
if (lang) {
|
||||
_outputFile = _outputFile.slice(0, -path.extname(_outputFile).length)+"_"+lang+path.extname(_outputFile);
|
||||
_tmpDir = path.join(_tmpDir, lang);
|
||||
}
|
||||
|
||||
book.logInfo("copy ebook to", output);
|
||||
return fs.copy(
|
||||
path.join(_tmpDir, "index."+options.ebookFormat),
|
||||
_outputFile
|
||||
);
|
||||
};
|
||||
|
||||
// Multi-langs book
|
||||
return Q()
|
||||
.then(function() {
|
||||
if (book.isMultilingual()) {
|
||||
return Q.all(
|
||||
_.map(book.langs, function(lang) {
|
||||
return copyFile(lang.lang);
|
||||
})
|
||||
);
|
||||
} else {
|
||||
return copyFile();
|
||||
}
|
||||
})
|
||||
.then(function() {
|
||||
return fs.remove(tmpDir);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Parse list of plugins
|
||||
Book.prototype.parsePlugins = function() {
|
||||
var that = this;
|
||||
|
||||
@@ -101,6 +101,11 @@ Configuration.prototype.load = function() {
|
||||
});
|
||||
};
|
||||
|
||||
// Extend the configuration
|
||||
Configuration.prototype.extend = function(options) {
|
||||
_.extend(this.options, options);
|
||||
};
|
||||
|
||||
// Get structure file
|
||||
Configuration.prototype.getStructure = function(name) {
|
||||
return this.options.structure[name].split(".").slice(0, -1).join(".");
|
||||
|
||||
@@ -53,6 +53,30 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
// Build an ebook and output it
|
||||
{
|
||||
name: "pdf [book] [output]",
|
||||
description: "build a book to pdf",
|
||||
options: [
|
||||
LOG_OPTION
|
||||
],
|
||||
exec: function(args, kwargs) {
|
||||
var input = args[0] || process.cwd();
|
||||
var output = args[1];
|
||||
|
||||
var book = new Book(input, _.extend({}, {
|
||||
'logLevel': Book.LOG_LEVELS[(kwargs.log).toUpperCase()]
|
||||
}));
|
||||
|
||||
return book.parse()
|
||||
.then(function() {
|
||||
return book.generateFile(output, {
|
||||
ebookFormat: "pdf"
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// Build and serve a book
|
||||
{
|
||||
name: "serve [book]",
|
||||
|
||||
+6
-2
@@ -64,8 +64,12 @@ var getFiles = function(path) {
|
||||
|
||||
module.exports = {
|
||||
tmp: {
|
||||
file: Q.denodeify(tmp.file.bind(tmp)),
|
||||
dir: Q.denodeify(tmp.dir.bind(tmp))
|
||||
file: function() {
|
||||
return Q.nfcall(tmp.file.bind(tmp)).get(0)
|
||||
},
|
||||
dir: function() {
|
||||
return Q.nfcall(tmp.dir.bind(tmp)).get(0)
|
||||
}
|
||||
},
|
||||
list: getFiles,
|
||||
stat: Q.denodeify(fs.stat),
|
||||
|
||||
+1
-1
@@ -59,7 +59,7 @@ function cloneGitRepo(host, ref) {
|
||||
// Create temporary folder to store git repos
|
||||
.then(function() {
|
||||
if (GIT_TMP) return;
|
||||
return fs.tmp.dir().get(0)
|
||||
return fs.tmp.dir()
|
||||
.then(function(_tmp) {
|
||||
GIT_TMP = _tmp;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user