mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-22 18:43:29 +00:00
Improve code style for book.js and index.js
This commit is contained in:
+48
-55
@@ -1,31 +1,25 @@
|
||||
var Q = require("q");
|
||||
var _ = require("lodash");
|
||||
var path = require("path");
|
||||
var util = require("util");
|
||||
var lunr = require("lunr");
|
||||
var parsers = require("gitbook-parsers");
|
||||
var color = require("bash-color");
|
||||
|
||||
var fs = require("./utils/fs");
|
||||
var parseNavigation = require("./utils/navigation");
|
||||
var parseProgress = require("./utils/progress");
|
||||
var pageUtil = require("./utils/page");
|
||||
var pathUtil = require("./utils/path");
|
||||
var batch = require("./utils/batch");
|
||||
var links = require("./utils/links");
|
||||
var i18n = require("./utils/i18n");
|
||||
var logger = require("./utils/logger");
|
||||
|
||||
var Configuration = require("./configuration");
|
||||
var TemplateEngine = require("./template");
|
||||
var Plugin = require("./plugin");
|
||||
var PluginsList = require("./pluginslist");
|
||||
|
||||
var generators = require("./generators");
|
||||
|
||||
var Book = function(root, context, parent) {
|
||||
var that = this;
|
||||
|
||||
this.context = _.defaults(context || {}, {
|
||||
// Extend book configuration
|
||||
config: {},
|
||||
@@ -86,10 +80,10 @@ var Book = function(root, context, parent) {
|
||||
|
||||
// Search Index
|
||||
this.searchIndex = lunr(function () {
|
||||
this.ref('url');
|
||||
this.ref("url");
|
||||
|
||||
this.field('title', { boost: 10 });
|
||||
this.field('body');
|
||||
this.field("title", { boost: 10 });
|
||||
this.field("body");
|
||||
});
|
||||
|
||||
// Bind methods
|
||||
@@ -125,12 +119,12 @@ Book.prototype.parse = function() {
|
||||
path.join(that.root, lang.path),
|
||||
_.merge({}, that.context, {
|
||||
config: _.extend({}, that.options, {
|
||||
'output': path.join(that.options.output, lang.lang),
|
||||
'language': lang.lang
|
||||
"output": path.join(that.options.output, lang.lang),
|
||||
"language": lang.lang
|
||||
})
|
||||
}),
|
||||
that
|
||||
)
|
||||
);
|
||||
});
|
||||
});
|
||||
})
|
||||
@@ -187,7 +181,7 @@ Book.prototype.generate = function(generator) {
|
||||
// Create generator
|
||||
.then(function() {
|
||||
var Generator = generators[generator];
|
||||
if (!Generator) throw "Generator '"+that.options.generator+"' doesn't exist";
|
||||
if (!Generator) throw "Generator \""+that.options.generator+"\" doesn't exist";
|
||||
generator = new Generator(that);
|
||||
|
||||
return generator.prepare();
|
||||
@@ -212,9 +206,9 @@ Book.prototype.generate = function(generator) {
|
||||
|
||||
return Q()
|
||||
|
||||
// First, let's create folder
|
||||
// First, let"s create folder
|
||||
.then(function() {
|
||||
return _.reduce(ops["directories"] || [], function(prev, folder) {
|
||||
return _.reduce(ops.directories || [], function(prev, folder) {
|
||||
return prev.then(function() {
|
||||
that.log.debug.ln("transferring folder", folder);
|
||||
return Q(generator.transferFolder(folder));
|
||||
@@ -222,18 +216,18 @@ Book.prototype.generate = function(generator) {
|
||||
}, Q());
|
||||
})
|
||||
|
||||
// Then, let's copy other files
|
||||
// Then, let"s copy other files
|
||||
.then(function() {
|
||||
return Q.all(_.map(ops["files"] || [], function(file) {
|
||||
return Q.all(_.map(ops.files || [], function(file) {
|
||||
that.log.debug.ln("transferring file", file);
|
||||
return Q(generator.transferFile(file));
|
||||
}));
|
||||
})
|
||||
|
||||
// Finally let's generate content
|
||||
// Finally let"s generate content
|
||||
.then(function() {
|
||||
var nFiles = (ops["content"] || []).length;
|
||||
return _.reduce(ops["content"] || [], function(prev, file, i) {
|
||||
var nFiles = (ops.content || []).length;
|
||||
return _.reduce(ops.content || [], function(prev, file, i) {
|
||||
return prev.then(function() {
|
||||
var p = ((i*100)/nFiles).toFixed(0)+"%";
|
||||
that.log.debug.ln("processing", file, p);
|
||||
@@ -267,7 +261,7 @@ Book.prototype.generate = function(generator) {
|
||||
};
|
||||
|
||||
// Generate the output for a multilingual book
|
||||
Book.prototype.generateMultiLingual = function(generator) {
|
||||
Book.prototype.generateMultiLingual = function() {
|
||||
var that = this;
|
||||
|
||||
return Q()
|
||||
@@ -295,7 +289,7 @@ Book.prototype.generateFile = function(output, options) {
|
||||
book.setOutput(tmpDir);
|
||||
|
||||
return book.generate(options.ebookFormat)
|
||||
.then(function(_options) {
|
||||
.then(function() {
|
||||
var copyFile = function(lang) {
|
||||
var _outputFile = output;
|
||||
var _tmpDir = tmpDir;
|
||||
@@ -339,7 +333,7 @@ Book.prototype.generateFile = function(output, options) {
|
||||
Book.prototype.parseConfig = function() {
|
||||
var that = this;
|
||||
|
||||
that.log.info("loading book configuration....")
|
||||
that.log.info("loading book configuration....");
|
||||
return that.config.load()
|
||||
.then(function() {
|
||||
that.log.info.ok();
|
||||
@@ -349,12 +343,11 @@ Book.prototype.parseConfig = function() {
|
||||
// Parse list of plugins
|
||||
Book.prototype.parsePlugins = function() {
|
||||
var that = this;
|
||||
var failed = [];
|
||||
|
||||
// Load plugins
|
||||
return that.plugins.load(that.options.plugins)
|
||||
.then(function() {
|
||||
if (_.size(that.plugins.failed) > 0) return Q.reject(new Error("Error loading plugins: "+that.plugins.failed.join(",")+". Run 'gitbook install' to install plugins from NPM."));
|
||||
if (_.size(that.plugins.failed) > 0) return Q.reject(new Error("Error loading plugins: "+that.plugins.failed.join(",")+". Run \"gitbook install\" to install plugins from NPM."));
|
||||
|
||||
that.log.info.ok(that.plugins.count()+" plugins loaded");
|
||||
that.log.debug.ln("normalize plugins list");
|
||||
@@ -446,7 +439,7 @@ Book.prototype.parseSummary = function() {
|
||||
.then(function(content) {
|
||||
return summary.parser.summary(content, {
|
||||
entryPoint: that.readmeFile,
|
||||
entryPointTitle: that.i18n('SUMMARY_INTRODUCTION'),
|
||||
entryPointTitle: that.i18n("SUMMARY_INTRODUCTION"),
|
||||
files: that.files
|
||||
})
|
||||
.fail(function(err) {
|
||||
@@ -575,7 +568,7 @@ Book.prototype.parsePage = function(filename, options) {
|
||||
.then(function(content) {
|
||||
section.content = content;
|
||||
return _sections.concat([section]);
|
||||
})
|
||||
});
|
||||
});
|
||||
}, Q([]));
|
||||
})
|
||||
@@ -587,8 +580,8 @@ Book.prototype.parsePage = function(filename, options) {
|
||||
convertImages: options.convertImages,
|
||||
input: filename,
|
||||
navigation: that.navigation,
|
||||
base: path.dirname(filename) || './',
|
||||
output: path.dirname(filename) || './',
|
||||
base: path.dirname(filename) || "./",
|
||||
output: path.dirname(filename) || "./",
|
||||
glossary: that.glossary
|
||||
});
|
||||
})
|
||||
@@ -623,7 +616,7 @@ Book.prototype.findFile = function(filename) {
|
||||
parser: parsers.get(ext),
|
||||
path: filepath
|
||||
};
|
||||
})
|
||||
});
|
||||
});
|
||||
}, Q(null));
|
||||
};
|
||||
@@ -658,28 +651,28 @@ Book.prototype.listAllFiles = function() {
|
||||
var that = this;
|
||||
|
||||
return fs.list(this.root, {
|
||||
ignoreFiles: ['.ignore', '.gitignore', '.bookignore'],
|
||||
ignoreFiles: [".ignore", ".gitignore", ".bookignore"],
|
||||
ignoreRules: [
|
||||
// Skip Git stuff
|
||||
'.git/',
|
||||
'.gitignore',
|
||||
".git/",
|
||||
".gitignore",
|
||||
|
||||
// Skip OS X meta data
|
||||
'.DS_Store',
|
||||
".DS_Store",
|
||||
|
||||
// Skip stuff installed by plugins
|
||||
'node_modules',
|
||||
"node_modules",
|
||||
|
||||
// Skip book outputs
|
||||
'_book',
|
||||
'*.pdf',
|
||||
'*.epub',
|
||||
'*.mobi',
|
||||
"_book",
|
||||
"*.pdf",
|
||||
"*.epub",
|
||||
"*.mobi",
|
||||
|
||||
// Skip config files
|
||||
'.ignore',
|
||||
'.bookignore',
|
||||
'book.json',
|
||||
".ignore",
|
||||
".bookignore",
|
||||
"book.json",
|
||||
]
|
||||
})
|
||||
.then(function(_files) {
|
||||
@@ -688,7 +681,7 @@ Book.prototype.listAllFiles = function() {
|
||||
};
|
||||
|
||||
// Return true if the book is a multilingual book
|
||||
Book.prototype.isMultilingual = function(filename) {
|
||||
Book.prototype.isMultilingual = function() {
|
||||
return this.books.length > 0;
|
||||
};
|
||||
|
||||
@@ -698,7 +691,7 @@ Book.prototype.parentRoot = function() {
|
||||
return this.root;
|
||||
};
|
||||
|
||||
// Return true if it's a sub-book
|
||||
// Return true if it"s a sub-book
|
||||
Book.prototype.isSubBook = function() {
|
||||
return !!this.parent;
|
||||
};
|
||||
@@ -727,20 +720,20 @@ Book.prototype.relative = function(p) {
|
||||
// Normalize a path to .html and convert README -> index
|
||||
Book.prototype.contentPath = function(link) {
|
||||
if (
|
||||
path.basename(link, path.extname(link)) == "README"
|
||||
|| link == this.readmeFile
|
||||
path.basename(link, path.extname(link)) == "README" ||
|
||||
link == this.readmeFile
|
||||
) {
|
||||
link = path.join(path.dirname(link), "index"+path.extname(link));
|
||||
}
|
||||
|
||||
link = links.changeExtension(link, ".html");
|
||||
return link;
|
||||
}
|
||||
};
|
||||
|
||||
// Normalize a link to .html and convert README -> index
|
||||
Book.prototype.contentLink = function(link) {
|
||||
return links.normalize(this.contentPath(link));
|
||||
}
|
||||
};
|
||||
|
||||
// Index a page into the search index
|
||||
Book.prototype.indexPage = function(page) {
|
||||
@@ -764,7 +757,7 @@ Book.prototype._defaultsStructure = function(filename) {
|
||||
that.summaryFile = that.summaryFile || that.config.getStructure("summary")+extension;
|
||||
that.glossaryFile = that.glossaryFile || that.config.getStructure("glossary")+extension;
|
||||
that.langsFile = that.langsFile || that.config.getStructure("langs")+extension;
|
||||
}
|
||||
};
|
||||
|
||||
// Change output path
|
||||
Book.prototype.setOutput = function(p) {
|
||||
@@ -777,7 +770,7 @@ Book.prototype.setOutput = function(p) {
|
||||
};
|
||||
|
||||
// Translate a strign according to the book language
|
||||
Book.prototype.i18n = function(phrase) {
|
||||
Book.prototype.i18n = function() {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
return i18n.__.apply({}, [this.config.normalizeLanguage()].concat(args));
|
||||
};
|
||||
@@ -796,10 +789,10 @@ Book.prototype.normError = function(err, opts, defs) {
|
||||
err.toString = function() {
|
||||
var attributes = [];
|
||||
|
||||
if (this.fileName) attributes.push("In file '"+this.fileName+"'");
|
||||
if (this.fileName) attributes.push("In file \""+this.fileName+"\"");
|
||||
if (this.lineNumber) attributes.push("Line "+this.lineNumber);
|
||||
if (this.columnNumber) attributes.push("Column "+this.columnNumber);
|
||||
return (this.name || "Error")+": "+this.message+((attributes.length > 0)? " ("+attributes.join(", ")+")" : "")
|
||||
return (this.name || "Error")+": "+this.message+((attributes.length > 0)? " ("+attributes.join(", ")+")" : "");
|
||||
};
|
||||
|
||||
return err;
|
||||
@@ -837,7 +830,7 @@ Book.init = function(root, opts) {
|
||||
var chapters = book.summary.chapters || [];
|
||||
extensionToUse = path.extname(summary);
|
||||
|
||||
if (chapters.length == 0) {
|
||||
if (chapters.length === 0) {
|
||||
chapters = [
|
||||
{
|
||||
title: "Summary",
|
||||
@@ -853,7 +846,7 @@ Book.init = function(root, opts) {
|
||||
return Q(chaptersPaths(chapters));
|
||||
})
|
||||
.then(function(chapters) {
|
||||
// Create files that don't exist
|
||||
// Create files that don"t exist
|
||||
return Q.all(_.map(chapters, function(chapter) {
|
||||
if (!chapter.path) return Q();
|
||||
var absolutePath = path.resolve(book.root, chapter.path);
|
||||
@@ -869,7 +862,7 @@ Book.init = function(root, opts) {
|
||||
|
||||
return fs.mkdirp(path.dirname(absolutePath))
|
||||
.then(function() {
|
||||
return fs.writeFile(absolutePath, '# '+chapter.title+'\n');
|
||||
return fs.writeFile(absolutePath, "# "+chapter.title+"\n");
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
+20
-21
@@ -1,11 +1,10 @@
|
||||
var Q = require("q");
|
||||
var _ = require("lodash");
|
||||
var path = require("path");
|
||||
var tinylr = require('tiny-lr');
|
||||
var color = require('bash-color');
|
||||
var tinylr = require("tiny-lr");
|
||||
var color = require("bash-color");
|
||||
|
||||
var Book = require("./book");
|
||||
var Plugin = require("./plugin");
|
||||
var Server = require("./utils/server");
|
||||
var stringUtils = require("./utils/string");
|
||||
var watch = require("./utils/watch");
|
||||
@@ -42,10 +41,10 @@ module.exports = {
|
||||
var output = args[1] || path.join(input, "_book");
|
||||
|
||||
var book = new Book(input, _.extend({}, {
|
||||
'config': {
|
||||
'output': output
|
||||
"config": {
|
||||
"output": output
|
||||
},
|
||||
'logLevel': kwargs.log
|
||||
"logLevel": kwargs.log
|
||||
}));
|
||||
|
||||
return book.parse()
|
||||
@@ -71,7 +70,7 @@ module.exports = {
|
||||
var output = args[1];
|
||||
|
||||
var book = new Book(input, _.extend({}, {
|
||||
'logLevel': kwargs.log
|
||||
"logLevel": kwargs.log
|
||||
}));
|
||||
|
||||
return book.parse()
|
||||
@@ -116,7 +115,7 @@ module.exports = {
|
||||
|
||||
// Init livereload server
|
||||
var lrServer = tinylr({});
|
||||
var lrPath = undefined;
|
||||
var lrPath;
|
||||
|
||||
var generate = function() {
|
||||
if (server.isRunning()) console.log("Stopping server");
|
||||
@@ -124,10 +123,10 @@ module.exports = {
|
||||
return server.stop()
|
||||
.then(function() {
|
||||
var book = new Book(input, _.extend({}, {
|
||||
'config': {
|
||||
'defaultsPlugins': ["livereload"]
|
||||
"config": {
|
||||
"defaultsPlugins": ["livereload"]
|
||||
},
|
||||
'logLevel': kwargs.log
|
||||
"logLevel": kwargs.log
|
||||
}));
|
||||
|
||||
return book.parse()
|
||||
@@ -138,10 +137,10 @@ module.exports = {
|
||||
})
|
||||
.then(function(book) {
|
||||
console.log();
|
||||
console.log('Starting server ...');
|
||||
console.log("Starting server ...");
|
||||
return server.start(book.options.output, kwargs.port)
|
||||
.then(function() {
|
||||
console.log('Serving book on http://localhost:'+kwargs.port);
|
||||
console.log("Serving book on http://localhost:"+kwargs.port);
|
||||
|
||||
if (lrPath) {
|
||||
// trigger livereload
|
||||
@@ -159,18 +158,18 @@ module.exports = {
|
||||
// set livereload path
|
||||
lrPath = filepath;
|
||||
console.log("Restart after change in file", filepath);
|
||||
console.log('');
|
||||
console.log("");
|
||||
return generate();
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return Q.nfcall(lrServer.listen.bind(lrServer), kwargs.lrport)
|
||||
.then(function() {
|
||||
console.log('Live reload server started on port:', kwargs.lrport);
|
||||
console.log('Press CTRL+C to quit ...');
|
||||
console.log('')
|
||||
console.log("Live reload server started on port:", kwargs.lrport);
|
||||
console.log("Press CTRL+C to quit ...");
|
||||
console.log("");
|
||||
return generate();
|
||||
});
|
||||
}
|
||||
@@ -179,7 +178,7 @@ module.exports = {
|
||||
{
|
||||
name: "install [book]",
|
||||
description: "install plugins dependencies",
|
||||
exec: function(args, kwargs) {
|
||||
exec: function(args) {
|
||||
var input = args[0] || process.cwd();
|
||||
|
||||
var book = new Book(input);
|
||||
@@ -198,7 +197,7 @@ module.exports = {
|
||||
{
|
||||
name: "init [directory]",
|
||||
description: "create files and folders based on contents of SUMMARY.md",
|
||||
exec: function(args, kwargs) {
|
||||
exec: function(args) {
|
||||
return Book.init(args[0] || process.cwd())
|
||||
.then(function(){
|
||||
console.log("");
|
||||
|
||||
Reference in New Issue
Block a user