summaryrefslogtreecommitdiffstats
path: root/lib/config
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-01-27 10:24:06 +0100
committerSamy Pessé <samypesse@gmail.com>2016-01-27 10:24:06 +0100
commitf305d57ab7702c3ca10fd6e32366d19e524ee1f0 (patch)
tree381ccb09c7cedc72cdf8ad6c98b681b72f4d1bc0 /lib/config
parent877f2e477b010f9f37a9044606f110a90f077680 (diff)
downloadgitbook-f305d57ab7702c3ca10fd6e32366d19e524ee1f0.zip
gitbook-f305d57ab7702c3ca10fd6e32366d19e524ee1f0.tar.gz
gitbook-f305d57ab7702c3ca10fd6e32366d19e524ee1f0.tar.bz2
Add more classes structures
Diffstat (limited to 'lib/config')
-rw-r--r--lib/config/default.js109
-rw-r--r--lib/config/index.js109
2 files changed, 218 insertions, 0 deletions
diff --git a/lib/config/default.js b/lib/config/default.js
new file mode 100644
index 0000000..92defaa
--- /dev/null
+++ b/lib/config/default.js
@@ -0,0 +1,109 @@
+var path = require('path');
+
+module.exports = {
+ // Options that can't be extend
+ 'configFile': 'book',
+ 'generator': 'website',
+ 'extension': null,
+
+ // Book metadatas (somes are extracted from the README by default)
+ 'title': null,
+ 'description': null,
+ 'isbn': null,
+ 'language': 'en',
+ 'direction': null,
+ 'author': null,
+
+ // Version of gitbook to use
+ 'gitbook': '*',
+
+ // Structure
+ 'structure': {
+ 'langs': 'LANGS.md',
+ 'readme': 'README.md',
+ 'glossary': 'GLOSSARY.md',
+ 'summary': 'SUMMARY.md'
+ },
+
+ // CSS Styles
+ 'styles': {
+ 'website': 'styles/website.css',
+ 'print': 'styles/print.css',
+ 'ebook': 'styles/ebook.css',
+ 'pdf': 'styles/pdf.css',
+ 'mobi': 'styles/mobi.css',
+ 'epub': 'styles/epub.css'
+ },
+
+ // Plugins list, can contain '-name' for removing default plugins
+ 'plugins': [],
+
+ // Global configuration for plugins
+ 'pluginsConfig': {},
+
+ // 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 for the file content
+ 'fontSize': 12,
+ 'fontFamily': 'Arial',
+
+ // 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',
+
+ // How to mark detected chapters.
+ // Choices are “pagebreak”, “rule”, 'both' or “none”.
+ 'chapterMark' : 'pagebreak',
+
+ // An XPath expression. Page breaks are inserted before the specified elements.
+ // To disable use the expression: '/'
+ 'pageBreaksBefore': '/',
+
+ // Margin (in pts)
+ // Note: 72 pts equals 1 inch
+ 'margin': {
+ 'right': 62,
+ 'left': 62,
+ 'top': 56,
+ 'bottom': 56
+ },
+
+ // Header HTML template. Available variables: _PAGENUM_, _TITLE_, _AUTHOR_ and _SECTION_.
+ 'headerTemplate': null,
+
+ // Footer HTML template. Available variables: _PAGENUM_, _TITLE_, _AUTHOR_ and _SECTION_.
+ 'footerTemplate': null
+ }
+};
diff --git a/lib/config/index.js b/lib/config/index.js
new file mode 100644
index 0000000..267f650
--- /dev/null
+++ b/lib/config/index.js
@@ -0,0 +1,109 @@
+var Q = require('q');
+var _ = require('lodash');
+var semver = require('semver');
+
+var gitbook = require('../gitbook');
+var configDefault = require('./default');
+
+/*
+Config is an interface for the book's configuration stored in "book.json" (or "book.js")
+*/
+
+function Config(book, baseConfig) {
+ this.book = book;
+ this.fs = book.fs;
+ this.log = book.log;
+
+ this.replace(baseConfig || {});
+}
+
+// Load configuration of the book
+// and verify that the configuration is satisfying
+Config.prototype.load = function() {
+ var that = this;
+
+ this.log.debug.ln('loading configuration');
+ return this.fs.loadAsObject(this.book.resolve('book'))
+ .fail(function(err) {
+ if (err.code != 'MODULE_NOT_FOUND') throw(err);
+ else return Q({});
+ })
+ .then(function(_config) {
+ return that.replace(_config);
+ })
+ .then(function() {
+ if (!that.book.isLanguageBook()) {
+ if (!gitbook.satisfies(that.options.gitbook)) {
+ throw new Error('GitBook version doesn\'t satisfy version required by the book: '+that.options.gitbook);
+ }
+ if (that.options.gitbook != '*' && !semver.satisfies(semver.inc(gitbook.version, 'patch'), that.options.gitbook)) {
+ that.log.warn.ln('gitbook version specified in your book.json might be too strict for future patches, \''+(_.first(gitbook.version.split('.'))+'.x.x')+'\' is more adequate');
+ }
+ }
+
+ that.options.output = that.options.output || that.book.resolve('_book');
+ //that.options.plugins = normalizePluginsList(that.options.plugins);
+ //that.options.defaultsPlugins = normalizePluginsList(that.options.defaultsPlugins || '', false);
+ //that.options.plugins = _.union(that.options.plugins, that.options.defaultsPlugins);
+ //that.options.plugins = _.uniq(that.options.plugins, 'name');
+
+ // Default value for text direction (from language)
+ /*if (!that.options.direction) {
+ var lang = i18n.getCatalog(that.options.language);
+ if (lang) that.options.direction = lang.direction;
+ }*/
+
+ that.options.gitbook = gitbook.version;
+ });
+};
+
+// Replace the whole configuration
+Config.prototype.replace = function(options) {
+ var that = this;
+
+ this.options = _.cloneDeep(configDefault);
+ this.options = _.merge(this.options, options || {});
+
+ // options.input == book.root
+ Object.defineProperty(this.options, 'input', {
+ get: function () {
+ return that.book.root;
+ }
+ });
+
+ // options.originalInput == book.parent.root
+ Object.defineProperty(this.options, 'originalInput', {
+ get: function () {
+ return that.book.parent? that.book.parent.root : undefined;
+ }
+ });
+
+ // options.originalOutput == book.parent.options.output
+ Object.defineProperty(this.options, 'originalOutput', {
+ get: function () {
+ return that.book.parent? that.book.parent.options.output : undefined;
+ }
+ });
+};
+
+// Return path to a structure file
+// Strip the extension by default
+Config.prototype.getStructure = function(name, dontStripExt) {
+ var filename = this.options.structure[name];
+ if (dontStripExt) return filename;
+
+ filename = filename.split('.').slice(0, -1).join('.');
+ return filename;
+};
+
+// Return a configuration using a key and a default value
+Config.prototype.get = function(key, def) {
+ return _.get(this.options, key, def);
+};
+
+// Update a configuration
+Config.prototype.set = function(key, value) {
+ return _.set(this.options, key, value);
+};
+
+module.exports = Config;