diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-19 10:08:49 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-19 10:08:49 +0100 |
commit | d9c020194d949bd7591c51f6a6bfb48612f39a09 (patch) | |
tree | d9715f3fda2751481a89a2afcfa9ada2a1d8ccb7 | |
parent | ec586dd3cdf06e9567f5d3e4961022ddc3c94778 (diff) | |
download | gitbook-d9c020194d949bd7591c51f6a6bfb48612f39a09.zip gitbook-d9c020194d949bd7591c51f6a6bfb48612f39a09.tar.gz gitbook-d9c020194d949bd7591c51f6a6bfb48612f39a09.tar.bz2 |
Add base object to load configuration
-rw-r--r-- | lib/book.js | 18 | ||||
-rw-r--r-- | lib/configuration.js | 114 | ||||
-rw-r--r-- | lib/index.js | 5 | ||||
-rw-r--r-- | lib/utils/fs.js | 90 | ||||
-rw-r--r-- | package.json | 5 | ||||
-rw-r--r-- | test/configuration.js | 17 | ||||
-rw-r--r-- | test/fixtures/config/book.json | 3 |
7 files changed, 251 insertions, 1 deletions
diff --git a/lib/book.js b/lib/book.js new file mode 100644 index 0000000..62667a9 --- /dev/null +++ b/lib/book.js @@ -0,0 +1,18 @@ +var _ = require("lodash"); + +var Configuration = require("./configuration"); + +var Book = function(root) { + // Root folder of the book + this.root = root; + + // Configuration + this.config = new Configuration(this); + Object.defineProperty(this, "options", { + get: function () { + return this.config.options; + } + }); +}; + +module.exports= Book; diff --git a/lib/configuration.js b/lib/configuration.js new file mode 100644 index 0000000..c5c1ba2 --- /dev/null +++ b/lib/configuration.js @@ -0,0 +1,114 @@ +var _ = require("lodash"); +var Q = require("q"); +var path = require("path"); + +var fs = require("./utils/fs"); + +var Configuration = function(book, options) { + this.book = book; + this.options = _.defaults(options || {}, Configuration.DEFAULT); +}; + +// Read and parse the configuration +Configuration.prototype.load = function() { + var that = this; + + return Q() + .then(function() { + try { + var _config = require(path.resolve(that.book.root, "book")); + that.options = _.merge( + that.options, + _.omit(_config, 'input', 'configFile', 'defaultsPlugins', 'generator') + ); + } + catch(err) { + return Q(); + } + }); +}; + +// Default configuration +Configuration.DEFAULT = { + // Book metadats (somes are extracted from the README by default) + "title": null, + "description": null, + "isbn": null, + + // For ebook format, the extension to use for generation (default is detected from output extension) + // "epub", "pdf", "mobi" + // Caution: it overrides the value from the command line + // It's not advised this option in the book.json + "extension": null, + + // Plugins list, can contain "-name" for removing default plugins + "plugins": [], + + // Global configuration for plugins + "pluginsConfig": { + "fontSettings": { + "theme": null, //"sepia", "night" or "white", + "family": "sans",// "serif" or "sans", + "size": 2 // 1 - 4 + } + }, + + // Variables for templating + "variables": {}, + + // Set another theme with your own layout + // It's recommended to use plugins or add more options for default theme, though + // See https://github.com/GitbookIO/gitbook/issues/209 + "theme": path.resolve(__dirname, '../../theme'), + + // Links in template (null: default, false: remove, string: new value) + "links": { + // Custom links at top of sidebar + "sidebar": { + //"Custom link name": "https://customlink.com" + }, + + // Sharing links + "sharing": { + "google": null, + "facebook": null, + "twitter": null, + "weibo": null, + "all": null + } + }, + + + // Options for PDF generation + "pdf": { + // Add toc at the end of the file + "toc": true, + + // Add page numbers to the bottom of every page + "pageNumbers": false, + + // Font size for the file content + "fontSize": 12, + + // Paper size for the pdf + // Choices are [u’a0’, u’a1’, u’a2’, u’a3’, u’a4’, u’a5’, u’a6’, u’b0’, u’b1’, u’b2’, u’b3’, u’b4’, u’b5’, u’b6’, u’legal’, u’letter’] + "paperSize": "a4", + + // Margin (in pts) + // Note: 72 pts equals 1 inch + "margin": { + "right": 62, + "left": 62, + "top": 36, + "bottom": 36 + }, + + //Header HTML template. Available variables: _PAGENUM_, _TITLE_, _AUTHOR_ and _SECTION_. + "headerTemplate": "", + + //Footer HTML template. Available variables: _PAGENUM_, _TITLE_, _AUTHOR_ and _SECTION_. + "footerTemplate": "" + } +}; + +module.exports= Configuration; diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..a968800 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,5 @@ + + +module.exports = { + Book: require("./book") +}; diff --git a/lib/utils/fs.js b/lib/utils/fs.js new file mode 100644 index 0000000..d39cfaa --- /dev/null +++ b/lib/utils/fs.js @@ -0,0 +1,90 @@ +var Q = require("q"); +var fs = require('graceful-fs'); +var fsExtra = require("fs-extra"); +var Ignore = require("fstream-ignore"); + +var getFiles = function(path) { + var d = Q.defer(); + + // Our list of files + var files = []; + + var ig = Ignore({ + path: path, + ignoreFiles: ['.ignore', '.gitignore', '.bookignore'] + }); + + // Add extra rules to ignore common folders + ig.addIgnoreRules([ + // Skip Git stuff + '.git/', + '.gitignore', + + // Skip OS X meta data + '.DS_Store', + + // Skip stuff installed by plugins + 'node_modules', + + // Skip book outputs + '*.pdf', + '*.epub', + '*.mobi', + + // Skip config files + '.ignore', + '.bookignore', + 'book.json', + ], '__custom_stuff'); + + // Push each file to our list + ig.on('child', function (c) { + files.push( + c.path.substr(c.root.path.length + 1) + (c.props.Directory === true ? '/' : '') + ); + }); + + ig.on('end', function() { + // Normalize paths on Windows + if(process.platform === 'win32') { + return d.resolve(files.map(function(file) { + return file.replace(/\\/g, '/'); + })); + } + + // Simply return paths otherwise + return d.resolve(files); + }); + + ig.on('error', d.reject); + + return d.promise; +}; + +module.exports = { + list: getFiles, + readFile: Q.denodeify(fs.readFile), + writeFile: function(filename, data, options) { + var d = Q.defer(); + + try { + fs.writeFileSync(filename, data, options) + } catch(err) { + d.reject(err); + } + d.resolve(); + + + return d.promise; + }, + mkdirp: Q.denodeify(fsExtra.mkdirp), + copy: Q.denodeify(fsExtra.copy), + remove: Q.denodeify(fsExtra.remove), + symlink: Q.denodeify(fsExtra.symlink), + exists: function(path) { + var d = Q.defer(); + fs.exists(path, d.resolve); + return d.promise; + }, + readFileSync: fs.readFileSync.bind(fs) +}; diff --git a/package.json b/package.json index 231db63..e8a279d 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,10 @@ "main": "lib/index.js", "dependencies": { "q": "1.0.1", - "lodash": "2.4.1" + "lodash": "2.4.1", + "graceful-fs": "3.0.5", + "fs-extra": "0.14.0", + "fstream-ignore": "1.0.2" }, "devDependencies": { "mocha": "1.18.2" diff --git a/test/configuration.js b/test/configuration.js new file mode 100644 index 0000000..6540d34 --- /dev/null +++ b/test/configuration.js @@ -0,0 +1,17 @@ +var path = require('path'); +var assert = require('assert'); + +var Book = require('../').Book; + +describe('Configuration parsing', function () { + it('should correctly load from json', function(done) { + var book = new Book(path.join(__dirname, './fixtures/config')); + book.config.load() + .then(function() { + assert(book.options.title == "Test"); + }) + .then(function() { + done() + }, done); + }); +}); diff --git a/test/fixtures/config/book.json b/test/fixtures/config/book.json new file mode 100644 index 0000000..a2191b8 --- /dev/null +++ b/test/fixtures/config/book.json @@ -0,0 +1,3 @@ +{ + "title": "Test" +}
\ No newline at end of file |