diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-23 18:03:15 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-23 18:03:15 +0100 |
commit | 112782d11837428c260211bb3afeb3263e479593 (patch) | |
tree | 3288c6972ab6d769ccbf8aade56ef6188c1c1761 | |
parent | b88b4a0578c28af78a01a28591772f94d0dfe82e (diff) | |
download | gitbook-112782d11837428c260211bb3afeb3263e479593.zip gitbook-112782d11837428c260211bb3afeb3263e479593.tar.gz gitbook-112782d11837428c260211bb3afeb3263e479593.tar.bz2 |
Add logs methods to Book object
-rw-r--r-- | lib/book.js | 31 | ||||
-rw-r--r-- | lib/generators/ebook.js | 2 | ||||
-rw-r--r-- | lib/generators/website.js | 5 | ||||
-rw-r--r-- | test/helper.js | 4 |
4 files changed, 38 insertions, 4 deletions
diff --git a/lib/book.js b/lib/book.js index dcf0a33..4c1c33f 100644 --- a/lib/book.js +++ b/lib/book.js @@ -16,13 +16,18 @@ var Plugin = require("./plugin"); var generators = require("./generators"); + + var Book = function(root, context, parent) { this.context = _.defaults(context || {}, { // Extend book configuration config: {}, // Log function - log: console.log.bind(console) + log: console.log.bind(console), + + // Log level + logLevel: Book.LOG_LEVELS.INFO }) // Root folder of the book @@ -73,6 +78,14 @@ var Book = function(root, context, parent) { }); }; +Book.LOG_LEVELS = { + DEBUG: 0, + INFO: 1, + WARNING: 2, + ERROR: 3, + DISABLED: 10 +}; + // Initialize and parse the book: config, summary, glossary Book.prototype.parse = function() { var that = this; @@ -170,13 +183,16 @@ Book.prototype.generate = function(generator) { if (!file) return; if (file[file.length -1] == "/") { + that.logDebug("transferring folder", file); return Q(generator.transferFolder(file)); } else if (_.contains(parsers.extensions, path.extname(file)) && that.navigation[file]) { + that.logDebug("parsing", file); return that.parsePage(file) .then(function(content) { return Q(generator.writeParsedFile(content, file)); }); } else { + that.logDebug("transferring file", file); return Q(generator.transferFile(file)); } }) @@ -449,4 +465,17 @@ Book.prototype.indexPage = function(page) { }); }; +// Log message +Book.prototype.log = function(level) { + if (level < this.context.logLevel) return; + + var args = Array.prototype.slice.apply(arguments); + args[0] = _.findKey(Book.LOG_LEVELS)+":"; + this.context.log.apply(null, args); +}; +Book.prototype.logDebug = _.partial(Book.prototype.log, Book.LOG_LEVELS.DEBUG); +Book.prototype.logInfo = _.partial(Book.prototype.log, Book.LOG_LEVELS.INFO); +Book.prototype.logWarn = _.partial(Book.prototype.log, Book.LOG_LEVELS.WARNING); +Book.prototype.logError = _.partial(Book.prototype.log, Book.LOG_LEVELS.ERROR); + module.exports= Book; diff --git a/lib/generators/ebook.js b/lib/generators/ebook.js index b173c19..88159f3 100644 --- a/lib/generators/ebook.js +++ b/lib/generators/ebook.js @@ -31,6 +31,7 @@ Generator.prototype.prepareTemplates = function() { Generator.prototype.writeSummary = function() { var that = this; + that.book.logInfo("write SUMMARY.html"); return this._writeTemplate(this.templates["summary"], {}, path.join(this.options.output, "SUMMARY.html")); }; @@ -97,6 +98,7 @@ Generator.prototype.finish = function() { stringUtils.optionsToShellArgs(_options) ].join(" "); + that.book.logInfo("start conversion to", that.ebookFormat); exec(command, function (error, stdout, stderr) { if (error) { if (error.code == 127) { diff --git a/lib/generators/website.js b/lib/generators/website.js index a60e039..af4cd0c 100644 --- a/lib/generators/website.js +++ b/lib/generators/website.js @@ -103,12 +103,13 @@ Generator.prototype.finish = function() { Generator.prototype.writeParsedFile = function(page) { var that = this; - var output = this.book.contentLink(page.path); - output = path.join(that.options.output, output); + var relativeOutput = this.book.contentLink(page.path); + var output = path.join(that.options.output, relativeOutput); var basePath = path.relative(path.dirname(output), this.options.output) || "."; if (process.platform === 'win32') basePath = basePath.replace(/\\/g, '/'); + that.book.logInfo("write parsed file", page.path, "to", relativeOutput); return that.normalizePage(page) .then(function() { return that._writeTemplate(that.templates["page"], { diff --git a/test/helper.js b/test/helper.js index 7e53151..341b698 100644 --- a/test/helper.js +++ b/test/helper.js @@ -41,7 +41,9 @@ before(function(done) { .sortBy() .map(function(book) { if (book.indexOf("test") !== 0) return null; - return new Book(path.join(__dirname, './fixtures/', book));; + return new Book(path.join(__dirname, './fixtures/', book), { + logLevel: Book.LOG_LEVELS.DISABLED + }); }) .compact() .value(); |