diff options
Diffstat (limited to 'lib/output')
-rw-r--r-- | lib/output/base.js | 141 | ||||
-rw-r--r-- | lib/output/index.js | 10 | ||||
-rw-r--r-- | lib/output/json.js | 47 | ||||
-rw-r--r-- | lib/output/website/index.js | 22 | ||||
-rw-r--r-- | lib/output/website/theme.js | 6 |
5 files changed, 226 insertions, 0 deletions
diff --git a/lib/output/base.js b/lib/output/base.js new file mode 100644 index 0000000..8bc27d4 --- /dev/null +++ b/lib/output/base.js @@ -0,0 +1,141 @@ +var _ = require('lodash'); +var Ignore = require('ignore'); +var path = require('path'); + +var Promise = require('../utils/promise'); +var pathUtil = require('../utils/path'); +var fs = require('../utils/fs'); +var PluginsManager = require('../plugins'); + +function Output(book, type) { + this.book = book; + this.log = this.book.log; + + this.type = type; + this.plugins = new PluginsManager(book); + + // Files to ignore in output + this.ignore = Ignore(); + this.ignore.addPattern(_.compact([ + '.gitignore', + '.ignore', + '.bookignore', + + // The configuration file should not be copied in the output + this.book.config.path + ])); +} + +// Return path to the root folder +Output.prototype.root = function(filename) { + return path.resolve(process.cwd(), this.book.config.get('output')); +}; + +// Resolve a file in the output directory +Output.prototype.resolve = function(filename) { + return pathUtil.resolveInRoot.apply(null, [this.root()].concat(_.toArray(arguments))); +}; + +// Write a file/buffer to the output folder +Output.prototype.writeFile = function(filename, buf) { + var that = this; + + return Promise() + .then(function() { + filename = that.resolve(filename); + var folder = path.dirname(filename); + + // Ensure fodler exists + return fs.mkdirp(folder); + }) + + // Write the file + .then(function() { + return fs.writeFile(filename, buf); + }); +}; + +// Copy a file to the output +Output.prototype.copyFile = function(from, to) { + var that = this; + + return Promise() + .then(function() { + to = that.resolve(to); + + return fs.copy(from, to); + }); +}; + +// Start the generation, for a parsed book +Output.prototype.generate = function() { + var that = this; + var isMultilingual = this.book.isMultilingual(); + + return Promise() + + // Load all plugins + .then(function() { + that.log.info.ln('Loading and preparing plugins'); + + var plugins = _.pluck(that.book.config.get('plugins'), 'name'); + + return that.plugins.load(plugins); + }) + + // Create the output folder + .then(function() { + return fs.mkdirp(that.root()); + }) + + // Initialize the generation + .then(function() { + return that.prepare(); + }) + + // Process all files + .then(function() { + return that.book.fs.listAllFiles(that.book.root); + }) + .then(function(files) { + return Promise.serie(files, function(filename) { + // Ignore file present in a language book + if (isMultilingual && that.book.isInLanguageBook(filename)) return; + + // Process file as page or asset + if (that.book.hasPage(filename)) { + return that.writePage(that.book.getPage(filename)); + } else { + return that.copyAsset(filename); + } + }); + }) + + // Finish the generation + .then(function() { + return that.finish(); + }); +}; + +// Prepare the generation +Output.prototype.prepare = function() { + +}; + +// Copy an asset file (non-parsable), ex: images, etc +Output.prototype.copyAsset = function(filename) { + +}; + +// Write a page (parsable file), ex: markdown, etc +Output.prototype.writePage = function(page) { + +}; + +// Finish the generation +Output.prototype.finish = function() { + +}; + + +module.exports = Output; diff --git a/lib/output/index.js b/lib/output/index.js new file mode 100644 index 0000000..dcb2ffe --- /dev/null +++ b/lib/output/index.js @@ -0,0 +1,10 @@ +var _ = require('lodash'); +//var EbookGenerator = require('./ebook'); + +module.exports = { + json: require('./json'), + /*website: require('./website'), + pdf: _.partialRight(EbookGenerator, 'pdf'), + mobi: _.partialRight(EbookGenerator, 'mobi'), + epub: _.partialRight(EbookGenerator, 'epub')*/ +}; diff --git a/lib/output/json.js b/lib/output/json.js new file mode 100644 index 0000000..b03df6e --- /dev/null +++ b/lib/output/json.js @@ -0,0 +1,47 @@ +var util = require('util'); +var Output = require('./base'); +var gitbook = require('../gitbook'); + +function JSONOutput() { + Output.apply(this, arguments); +} +util.inherits(JSONOutput, Output); + +// Write a page (parsable file) +JSONOutput.prototype.writePage = function(page) { + var that = this; + + // Parse the page + return page.parse() + + // Write as json + .then(function() { + var json = { + gitbook: { + version: gitbook.version + }, + path: page.path, + sections: page.content + }; + + return that.writeFile( + page.withExtension('.json'), + JSON.stringify(json, null, 4) + ); + }); +}; + +// At the end of generation, generate README.json for multilingual books +JSONOutput.prototype.finish = function() { + if (!this.book.isMultilingual()) return; + + // Copy README.json from main book + var mainLanguage = this.book.langs.getDefault().id; + return this.copyFile( + this.resolve(mainLanguage, 'README.json'), + 'README.json' + ); +}; + + +module.exports = JSONOutput; diff --git a/lib/output/website/index.js b/lib/output/website/index.js new file mode 100644 index 0000000..2b7db73 --- /dev/null +++ b/lib/output/website/index.js @@ -0,0 +1,22 @@ +var util = require('util'); +var Output = require('../base'); + +function WebsiteOutput() { + Output.apply(this, arguments); +} +util.inherits(WebsiteOutput, Output); + +// Copy an asset file +WebsiteOutput.prototype.writeAsset = function(filename) { + return this.copyFile( + this.book.resolve(filename), + filename + ); +}; + +// Write a page (parsable file) +WebsiteOutput.prototype.writePage = function(page) { + +}; + +module.exports = WebsiteOutput; diff --git a/lib/output/website/theme.js b/lib/output/website/theme.js new file mode 100644 index 0000000..1cc2891 --- /dev/null +++ b/lib/output/website/theme.js @@ -0,0 +1,6 @@ + +function Theme() { + +} + +module.exports = Theme; |