summaryrefslogtreecommitdiffstats
path: root/lib/configuration.js
blob: af4d3e28327d815da1958cbf7a2468e18aa66277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
var _ = require("lodash");
var Q = require("q");
var path = require("path");
var npmi = require('npmi');
var semver = require('semver');

var pkg = require('../package.json');
var fs = require("./utils/fs");

// Default plugins added to each books
var defaultsPlugins = [];

// Normalize a list of plugins to use
function normalizePluginsList(plugins) {
    // Normalize list to an array
    plugins = _.isString(plugins) ? plugins.split(",") : (plugins || []);

    // Remove empty parts
    plugins = _.compact(plugins);

    // Divide as {name, version} to handle format like "myplugin@1.0.0"
    plugins = _.map(plugins, function(plugin) {
        if (plugin.name) return plugin;

        var parts = plugin.split("@");
        return {
            'name': parts[0],
            'version': parts[1] // optional
        }
    });

    // List plugins to remove
    var toremove = _.chain(plugins)
    .filter(function(plugin) {
        return plugin.name.length > 0 && plugin.name[0] == "-";
    })
    .map(function(plugin) {
        return plugin.name.slice(1);
    })
    .value();

    // Merge with defaults
    plugins = _.chain(plugins)
    .concat(_.map(defaultsPlugins, function(plugin) {
        return { 'name': plugin }
    }))
    .uniq()
    .value();

    // Build final list
    plugins = _.filter(plugins, function(plugin) {
        return !_.contains(toremove, plugin.name) && !(plugin.name.length > 0 && plugin.name[0] == "-");
    });

    return plugins;
}

// Normalize a list of plugin name to use
function normalizePluginsNames(plugins) {
    return _.pluck(normalizePluginsList(plugins), "name");
};


var Configuration = function(book, options) {
    var that = this;

	this.book = book;

    this.options = _.cloneDeep(Configuration.DEFAULT);
	this.options = _.merge(this.options, options || {});

    // options.input == book.root
    Object.defineProperty(this.options, "input", {
        get: function () {
            return that.book.root;
        }
    });
};

// 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, that.options.configFile));
            that.options = _.merge(
            	that.options,
            	_.omit(_config, 'configFile', 'defaultsPlugins', 'generator', 'extension')
            );
        }
        catch(err) {
            if (err instanceof SyntaxError) return Q.reject(err);
            return Q();
        }
    })
    .then(function() {
        that.options.output = path.resolve(that.options.output || path.join(that.book.root, "_book"));
        that.options.plugins = normalizePluginsList(that.options.plugins);
        that.options.defaultsPlugins = normalizePluginsList(that.options.defaultsPlugins || "");
        that.options.plugins = _.union(that.options.plugins, that.options.defaultsPlugins);
        that.options.plugins = _.uniq(that.options.plugins, "name");

        if (!semver.satisfies(pkg.version, that.options.gitbook)) {
            throw "GitBook version doesn't satisfy version required by the book: "+that.options.gitbook;
        }
        if (that.options.gitbook == "*") {
            that.book.log.warn.ln("you should specify a gitbook version to use in your book.json, for example: "+(_.first(pkg.version.split("."))+".x.x"));
        }
    });
};

// Extend the configuration
Configuration.prototype.extend = function(options) {
    _.extend(this.options, options);
};

// Get structure file
Configuration.prototype.getStructure = function(name) {
    return this.options.structure[name].split(".").slice(0, -1).join(".");
};

// Return normalized language
Configuration.prototype.normalizeLanguage = function() {
    return _.first(this.options.language.split("-")).toLowerCase();
};

// Install plugins
Configuration.prototype.installPlugins = function(options) {
    var that = this;
    options = _.defaults(options || {
        log: true
    });

    // Remov defaults (no need to install)
    var plugins = _.filter(that.options.plugins, function(plugin) {
        return !_.contains(defaultsPlugins, plugin.name);
    });

    // Install plugins one by one
    if (options.log) that.book.log.info.ln(plugins.length+" plugins to install");
    return _.reduce(plugins, function(prev, plugin) {
        return prev.then(function() {
            var fullname = "gitbook-plugin-"+plugin.name;
            if (options.log) that.book.log.info.ln("install plugin", plugin.name, "from npm ("+fullname+") with version", (plugin.version || "*"));
            return Q.nfcall(npmi, {
                'name': fullname,
                'version': plugin.version,
                'path': that.book.root,
                'npmLoad': {
                    'loglevel': 'silent',
                    'loaded': false,
                    'prefix': that.book.root
                }
            })
            .then(function() {
                that.book.log.info.ok("plugin", plugin.name, "installed with success");
            });
        });
    }, Q());
}

// Default configuration
Configuration.DEFAULT = {
    // Options that can't be extend
    "configFile": "book",
    "generator": "site",
    "extension": null,

    // Book metadats (somes are extracted from the README by default)
    "title": null,
    "description": null,
    "isbn": null,
    "language": "en",

    // 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",
        "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": {
        "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;