summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-04-13 23:53:34 +0200
committerSamy Pessé <samypesse@gmail.com>2014-04-13 23:53:34 +0200
commit3c7cae6262fdbc2dd3c9944661097d7cee60d9ce (patch)
tree07feb14f5f2d2ff90805a1cb929173c6c81fb5ff
parent555c0d63ee905f9a258cccf164b58b1e1e1bd950 (diff)
downloadgitbook-3c7cae6262fdbc2dd3c9944661097d7cee60d9ce.zip
gitbook-3c7cae6262fdbc2dd3c9944661097d7cee60d9ce.tar.gz
gitbook-3c7cae6262fdbc2dd3c9944661097d7cee60d9ce.tar.bz2
Move more build logic to the core generation module
-rw-r--r--bin/build.js145
-rwxr-xr-xbin/gitbook.js39
-rw-r--r--lib/generate/index.js75
3 files changed, 131 insertions, 128 deletions
diff --git a/bin/build.js b/bin/build.js
index 0384b77..4133d50 100644
--- a/bin/build.js
+++ b/bin/build.js
@@ -1,7 +1,6 @@
var path = require('path');
var Q = require('q');
var _ = require('lodash');
-var tmp = require('tmp');
var utils = require('./utils');
@@ -10,108 +9,56 @@ var parse = require("../lib/parse");
var fs = require('../lib/generate/fs');
var generators = require("../lib/generate").generators;
-var buildFunc = function(dir, options) {
- dir = dir || process.cwd();
- outputDir = options.output || path.join(dir, '_book');
+var makeBuildFunc = function(converter) {
+ return function(dir, options) {
+ dir = dir || process.cwd();
+ outputDir = options.output
- console.log('Starting build ...');
- // Get repo's URL
- return utils.gitURL(dir)
- .then(function(url) {
- // Get ID of repo
- return utils.githubID(url);
- }, function(err) {
- return null;
- })
- .then(function(repoID) {
- var title = options.title;
- var githubID = options.github || repoID;
+ console.log('Starting build ...');
+ // Get repo's URL
+ return utils.gitURL(dir)
+ .then(function(url) {
+ // Get ID of repo
+ return utils.githubID(url);
+ }, function(err) {
+ return null;
+ })
+ .then(function(repoID) {
+ var title = options.title;
+ var githubID = options.github || repoID;
- if (!title && !githubID) {
- throw new Error('Needs either a title or a githubID (username/repo).\n'+
- ' If using github, either set repo origin to a github repo or use the -g flag.\n'+
- ' For title, use the -t flag.');
- } else if (!title) {
- var parts = githubID.split('/', 2);
- var user = parts[0], repo = parts[1];
+ if (!title && !githubID) {
+ throw new Error('Needs either a title or a githubID (username/repo).\n'+
+ ' If using github, either set repo origin to a github repo or use the -g flag.\n'+
+ ' For title, use the -t flag.');
+ } else if (!title) {
+ var parts = githubID.split('/', 2);
+ var user = parts[0], repo = parts[1];
- title = utils.titleCase(repo);
- }
+ title = utils.titleCase(repo);
+ }
- return generate.folder(
- _.extend(options.options || {}, {
- input: dir,
- output: outputDir,
- title: title,
- description: options.intro,
- github: githubID,
- githubHost: options.githubHost,
- generator: options.format,
- theme: options.theme
- })
- );
- })
- .then(function(output) {
- console.log("Successfuly built !");
- return output;
- }, utils.logError);
-};
-
-var buildFiles = function(dir, outputFile, options, masterOptions) {
- var ext = masterOptions.extension;
-
- outputFile = outputFile || path.resolve(dir, "book."+ext);
-
- Q.nfcall(tmp.dir)
- .then(function(tmpDir) {
- return buildFunc(
- dir,
- _.extend(options, {
- output: tmpDir,
- format: masterOptions.format,
- options: masterOptions.options
- })
- )
- .then(function(_options) {
- var copyPDF = function(lang) {
- var _outputFile = outputFile;
- var _tmpDir = tmpDir;
-
- if (lang) {
- _outputFile = _outputFile.slice(0, -path.extname(_outputFile).length)+"_"+lang+path.extname(_outputFile);
- _tmpDir = path.join(_tmpDir, lang);
- }
-
- console.log("Generating in", _outputFile);
- return fs.copy(
- path.join(_tmpDir, "index."+ext),
- _outputFile
- );
- };
-
- // Multi-langs book
- return Q()
- .then(function() {
- if (_options.langsSummary) {
- console.log("Generating for all the languages");
- return Q.all(
- _.map(_options.langsSummary.list, function(lang) {
- return copyPDF(lang.lang);
- })
- );
- } else {
- return copyPDF();
- }
- })
- .then(function() {
- return fs.remove(tmpDir);
- })
- .fail(utils.logError);
- });
- })
-};
+ return converter(
+ _.extend(options || {}, {
+ input: dir,
+ output: outputDir,
+ title: title,
+ description: options.intro,
+ github: githubID,
+ githubHost: options.githubHost,
+ generator: options.format,
+ theme: options.theme
+ })
+ );
+ })
+ .then(function(output) {
+ console.log("Successfuly built !");
+ return output;
+ }, utils.logError);
+ };
+}
module.exports = {
- folder: buildFunc,
- files: buildFiles
+ folder: makeBuildFunc(generate.folder),
+ file: makeBuildFunc(generate.file)
};
diff --git a/bin/gitbook.js b/bin/gitbook.js
index 6cf2f1a..e01b511 100755
--- a/bin/gitbook.js
+++ b/bin/gitbook.js
@@ -28,17 +28,15 @@ var buildCommand = function(command) {
.option('--theme <path>', 'Path to theme directory');
};
-buildCommand(prog)
-.command('build [source_dir]')
+buildCommand(prog.command('build [source_dir]'))
.description('Build a gitbook from a directory')
.action(build.folder);
-buildCommand(prog)
-.command('serve [source_dir]')
+buildCommand(prog.command('serve [source_dir]'))
.description('Build then serve a gitbook from a directory')
.option('-p, --port <port>', 'Port for server to listen on', 4000)
.action(function(dir, options) {
- build.folder(dir, options)
+ build.folder(dir, options || {})
.then(function(_options) {
console.log();
console.log('Starting server ...');
@@ -52,35 +50,26 @@ buildCommand(prog)
});
});
-buildCommand(prog)
-.command('pdf [source_dir] [output_file]')
+buildCommand(prog.command('pdf [source_dir]'))
.description('Build a gitbook as a PDF')
.option('-pf, --paperformat <format>', 'PDF paper format (default is A4): "5in*7.5in", "10cm*20cm", "A4", "Letter"')
-.action(function(dir, output, options) {
- build.files(dir, output, options, {
+.action(function(dir, options) {
+ build.file(dir, _.extend(options, {
extension: "pdf",
- format: "pdf",
- options: {
- paperformat: options.paperformat
- }
- });
+ format: "pdf"
+ }));
});
-buildCommand(prog)
-.command('ebook [source_dir] [output_file]')
+buildCommand(prog.command('ebook [source_dir]'))
.description('Build a gitbook as a eBook')
.option('-c, --cover <path>', 'Cover image, default is cover.png if exists')
-.action(function(dir, output, options) {
- var ext = output ? path.extname(output) : "epub";
+.action(function(dir, options) {
+ var ext = options.output ? path.extname(options.output) : "epub";
- build.files(dir, output, options, {
+ build.file(dir, _.extend(options, {
extension: ext,
- format: "ebook",
- options: {
- extension: ext,
- cover: options.cover
- }
- });
+ format: "ebook"
+ }));
});
prog
diff --git a/lib/generate/index.js b/lib/generate/index.js
index 4804548..ba0e340 100644
--- a/lib/generate/index.js
+++ b/lib/generate/index.js
@@ -1,8 +1,8 @@
var Q = require("q");
var _ = require("lodash");
-
var path = require("path");
var swig = require('swig');
+var tmp = require('tmp');
var fs = require("./fs");
var parse = require("../parse");
@@ -15,6 +15,10 @@ var generators = {
"json": require("./json")
};
+/*
+ * Use a specific generator to convert a gitbook to a site/pdf/ebook/
+ * output is always a folder
+ */
var generate = function(options) {
var generator = null;
var files;
@@ -39,14 +43,16 @@ var generate = function(options) {
theme: path.resolve(__dirname, '../../theme')
});
- if (!options.title || !options.input || !options.output) {
- return Q.reject(new Error("Need options: title, input, output"));
+ if (!options.title || !options.input) {
+ return Q.reject(new Error("Need options: title, input"));
}
if (!generators[options.generator]) {
return Q.reject(new Error("Invalid generator (availables are: "+_.keys(generators).join(", ")));
}
+ options.output = options.output || path.join(options.input, "_book");
+
// Clean output folder
return fs.remove(options.output)
@@ -142,7 +148,68 @@ var generate = function(options) {
});
};
+/*
+ * Extract files from generate output in a temporary folder
+ */
+var generateFile = function(options) {
+ options = _.defaults(options || {}, {
+ input: null,
+ output: null,
+ extension: null
+ });
+ console.log(options);
+
+ Q.nfcall(tmp.dir)
+ .then(function(tmpDir) {
+ return generate(
+ _.extend({},
+ options,
+ {
+ output: tmpDir
+ })
+ )
+ .then(function(_options) {
+ var ext = options.extension;
+ var outputFile = options.output || path.resolve(options.input, "book."+ext);
+
+ var copyFile = function(lang) {
+ var _outputFile = outputFile;
+ var _tmpDir = tmpDir;
+
+ if (lang) {
+ _outputFile = _outputFile.slice(0, -path.extname(_outputFile).length)+"_"+lang+path.extname(_outputFile);
+ _tmpDir = path.join(_tmpDir, lang);
+ }
+
+ console.log("generating", _outputFile);
+ return fs.copy(
+ path.join(_tmpDir, "index."+ext),
+ _outputFile
+ );
+ };
+
+ // Multi-langs book
+ return Q()
+ .then(function() {
+ if (_options.langsSummary) {
+ return Q.all(
+ _.map(_options.langsSummary.list, function(lang) {
+ return copyFile(lang.lang);
+ })
+ );
+ } else {
+ return copyFile();
+ }
+ })
+ .then(function() {
+ return fs.remove(tmpDir);
+ });
+ });
+ })
+};
+
module.exports = {
generators: generators,
- folder: generate
+ folder: generate,
+ file: generateFile
};