diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-14 19:48:12 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-14 19:48:12 +0200 |
commit | 05f1eed9cee6ba0cbffb3285803fd6e9760291ef (patch) | |
tree | dbe1340b916dddeb817dabef6a9a6f9832853dd2 | |
parent | c0ca055bf4c5cbc776b2822afaa3a1efb1b97c19 (diff) | |
download | gitbook-05f1eed9cee6ba0cbffb3285803fd6e9760291ef.zip gitbook-05f1eed9cee6ba0cbffb3285803fd6e9760291ef.tar.gz gitbook-05f1eed9cee6ba0cbffb3285803fd6e9760291ef.tar.bz2 |
Fix #91: Extract defaults title and description from README.md
-rw-r--r-- | bin/build.js | 18 | ||||
-rwxr-xr-x | bin/gitbook.js | 4 | ||||
-rw-r--r-- | lib/generate/index.js | 32 |
3 files changed, 28 insertions, 26 deletions
diff --git a/bin/build.js b/bin/build.js index 4133d50..3059dde 100644 --- a/bin/build.js +++ b/bin/build.js @@ -24,27 +24,13 @@ var makeBuildFunc = function(converter) { 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]; - - title = utils.titleCase(repo); - } - return converter( _.extend(options || {}, { input: dir, output: outputDir, - title: title, + title: options.title, description: options.intro, - github: githubID, + github: options.github || repoID, githubHost: options.githubHost, generator: options.format, theme: options.theme diff --git a/bin/gitbook.js b/bin/gitbook.js index e3d8a36..773e566 100755 --- a/bin/gitbook.js +++ b/bin/gitbook.js @@ -21,8 +21,8 @@ var buildCommand = function(command) { return command .option('-o, --output <directory>', 'Path to output directory, defaults to ./_book') .option('-f, --format <name>', 'Change generation format, defaults to site, availables are: '+_.keys(generators).join(", ")) - .option('-t, --title <name>', 'Name of the book to generate, defaults to repo name') - .option('-i, --intro <intro>', 'Description of the book to generate') + .option('-t, --title <name>', 'Name of the book to generate, default is extracted from readme') + .option('-i, --intro <intro>', 'Description of the book to generate, default is extracted from readme') .option('-g, --github <repo_path>', 'ID of github repo like : username/repo') .option('--githubHost <url>', 'The url of the github host (defaults to https://github.com/') .option('--theme <path>', 'Path to theme directory'); diff --git a/lib/generate/index.js b/lib/generate/index.js index cba9071..48bcaca 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -15,6 +15,8 @@ var generators = { "json": require("./json") }; +var defaultDescription = "Book generated using GitBook"; + /* * Use a specific generator to convert a gitbook to a site/pdf/ebook/ * output is always a folder @@ -33,7 +35,7 @@ var generate = function(options) { // Book title, keyword, description title: null, - description: "Book generated using GitBook", + description: null, // Origin github repository id github: null, @@ -43,8 +45,8 @@ var generate = function(options) { theme: path.resolve(__dirname, '../../theme') }); - if (!options.title || !options.input) { - return Q.reject(new Error("Need options: title, input")); + if (!options.input) { + return Q.reject(new Error("Need option input (book input directory)")); } if (!generators[options.generator]) { @@ -103,14 +105,28 @@ var generate = function(options) { 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") + return Q() + + // Read readme + .then(function() { + return fs.readFile(path.join(options.input, "README.md"), "utf-8") + .then(function(_readme) { + _readme = parse.readme(_readme); + + options.title = options.title || _readme.title; + options.description = options.description || _readme.description || defaultDescription; + }); + }) // Get summary - .then(function(_summary) { - options.summary = parse.summary(_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); + // Parse navigation + options.navigation = parse.navigation(options.summary); + }); }) // Copy file and replace markdown file |