summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/book.js26
-rw-r--r--lib/cli/helper.js13
-rw-r--r--lib/cli/index.js23
3 files changed, 44 insertions, 18 deletions
diff --git a/lib/book.js b/lib/book.js
index c81d255..86b1d67 100644
--- a/lib/book.js
+++ b/lib/book.js
@@ -328,4 +328,30 @@ Book.prototype.isInLanguageBook = function(filename) {
});
};
+// Locate a book in a folder
+// - Read the ".gitbook" is exists
+// - Try the folder itself
+// - Try a "docs" folder
+Book.locate = function(fs, root) {
+ return fs.readAsString(path.join(root, '.gitbook'))
+ .then(function(content) {
+ return path.join(root, content);
+ }, function() {
+ // .gitbook doesn't exists, fall back to the root folder
+ return Promise(root);
+ });
+};
+
+// Locate and setup a book
+Book.setup = function(fs, root, opts) {
+ return Book.locate(fs, root)
+ .then(function(_root) {
+ return new Book(_.extend(opts || {}, {
+ root: _root,
+ fs: fs
+ }));
+ });
+};
+
+
module.exports = Book;
diff --git a/lib/cli/helper.js b/lib/cli/helper.js
index 9510b49..e62c8d9 100644
--- a/lib/cli/helper.js
+++ b/lib/cli/helper.js
@@ -9,6 +9,8 @@ var JSONOutput = require('../output/json');
var WebsiteOutput = require('../output/website');
var EBookOutput = require('../output/ebook');
+var nodeFS = new NodeFS();
+
var LOG_OPTION = {
name: 'log',
description: 'Minimum log level to display',
@@ -39,14 +41,12 @@ var FORMATS = {
function bookCmd(fn) {
return function(args, kwargs) {
var input = path.resolve(args[0] || process.cwd());
- var book = new Book({
- fs: new NodeFS(),
- root: input,
-
+ return Book.setup(nodeFS, input, {
logLevel: kwargs.log
+ })
+ .then(function(book) {
+ return fn(book, args.slice(1), kwargs);
});
-
- return fn(book, args.slice(1), kwargs);
};
}
@@ -94,6 +94,7 @@ function ebookCmd(format) {
}
module.exports = {
+ nodeFS: nodeFS,
bookCmd: bookCmd,
outputCmd: outputCmd,
ebookCmd: ebookCmd,
diff --git a/lib/cli/index.js b/lib/cli/index.js
index 78c5286..0b2366a 100644
--- a/lib/cli/index.js
+++ b/lib/cli/index.js
@@ -114,22 +114,21 @@ module.exports = {
// Generate the book
.then(function() {
- var book = new Book({
- fs: new NodeFS(),
- root: input,
+ return Book.setup(helper.nodeFS, input, {
'config': {
'defaultsPlugins': ['livereload']
},
'logLevel': kwargs.log
- });
-
- return book.parse()
- .then(function() {
- var Out = helper.FORMATS[kwargs.format];
- var output = new Out(book);
-
- return output.generate()
- .thenResolve(output);
+ })
+ .then(function(book) {
+ return book.parse()
+ .then(function() {
+ var Out = helper.FORMATS[kwargs.format];
+ var output = new Out(book);
+
+ return output.generate()
+ .thenResolve(output);
+ });
});
})