diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2014-08-11 09:42:18 -0700 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@gmail.com> | 2014-08-11 09:42:18 -0700 |
commit | d216661f2743385ea760b9993fdd46968e37ee42 (patch) | |
tree | de00bb67ffe5728f6175beb1ee0328354aa4f624 /lib/generate | |
parent | 74f95f4d0d404e6df533d056a5acab60623407c8 (diff) | |
parent | 50dfee99b6880da4be442bd43f475ae87a565c46 (diff) | |
download | gitbook-d216661f2743385ea760b9993fdd46968e37ee42.zip gitbook-d216661f2743385ea760b9993fdd46968e37ee42.tar.gz gitbook-d216661f2743385ea760b9993fdd46968e37ee42.tar.bz2 |
Merge pull request #403 from GitbookIO/version/1.0.0
Version 1.0.0
Diffstat (limited to 'lib/generate')
-rw-r--r-- | lib/generate/config.js | 96 | ||||
-rw-r--r-- | lib/generate/ebook/index.js | 13 | ||||
-rw-r--r-- | lib/generate/index.js | 71 | ||||
-rw-r--r-- | lib/generate/manifest.js | 72 | ||||
-rw-r--r-- | lib/generate/site/index.js | 35 |
5 files changed, 103 insertions, 184 deletions
diff --git a/lib/generate/config.js b/lib/generate/config.js new file mode 100644 index 0000000..0b270a3 --- /dev/null +++ b/lib/generate/config.js @@ -0,0 +1,96 @@ +var _ = require('lodash'); +var path = require('path'); + +// Default configuration for gitbook +var CONFIG = { + // Folders to use for output + // Caution: it overrides the value from the command line + // It's not advised this option in the book.json + "output": null, + + // Generator to use for building + // Caution: it overrides the value from the command line + // It's not advised this option in the book.json + "generator": "site", + + // Configuration file to use + "configFile": "book", + + // Book title and description (defaults are extracted from the README) + "title": null, + "description": 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 + } + }, + + // 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 + } + } +}; + +module.exports = { + CONFIG: CONFIG, + defaults: function(options) { + return _.merge(options || {}, CONFIG, _.defaults); + } +} + diff --git a/lib/generate/ebook/index.js b/lib/generate/ebook/index.js index 6a9aa70..0e1cd4e 100644 --- a/lib/generate/ebook/index.js +++ b/lib/generate/ebook/index.js @@ -44,18 +44,7 @@ Generator.prototype.finish = function() { }; if (format == "pdf") { - var pdfOptions = _.defaults(that.options.pdf || {}, { - "fontSize": 12, - "toc": true, - "pageNumbers": false, - "paperSize": "a4", - "margin": { - "right": 62, - "left": 62, - "top": 36, - "bottom": 36 - } - }); + var pdfOptions = that.options.pdf; _.extend(_options, { "--margin-left": String(pdfOptions.margin.left), diff --git a/lib/generate/index.js b/lib/generate/index.js index 53de646..c71e0a6 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -7,6 +7,7 @@ var swig = require('./template'); var fs = require("./fs"); var parse = require("../parse"); var Plugin = require("./plugin"); +var defaultConfig = require("./config"); var generators = { "site": require("./site"), @@ -25,7 +26,7 @@ var containsFiles = function(dir, files) { .then(_.all); }; -// TEst if generator exists +// Test if generator exists var checkGenerator = function(options) { if (!generators[options.generator]) { return Q.reject(new Error("Invalid generator (availables are: "+_.keys(generators).join(", ")+")")); @@ -48,40 +49,7 @@ var loadGenerator = function(options) { var generate = function(options) { // Set defaults to options - options = _.defaults(options || {}, { - // Folders to use - "input": null, - "output": null, - - // Config file (relative to input) - "configFile": "book", - - // Output generator - "generator": "site", - - // Book title, keyword, description - "title": null, - "description": null, - - // Origin github repository id - "github": null, - "githubHost": 'https://github.com/', - - // Theming - "theme": path.resolve(__dirname, '../../theme'), - - // Plugins - "plugins": [], - "pluginsConfig": {}, - "defaultsPlugins": [], // Default plugins that can't be set in book.json - - // Links - "links": { - "about": null, - "issues": null, - "contribue": null, - } - }); + options = defaultConfig.defaults(options); // Validate options if (!options.input) { @@ -96,7 +64,7 @@ var generate = function(options) { try { var _config = require(path.resolve(options.input, options.configFile)); - _.extend(options, _.omit(_config, 'input', 'configFile', 'defaultsPlugins')); + options = _.merge(options, _.omit(_config, 'input', 'configFile', 'defaultsPlugins')); } catch(err) { // No config file: not a big deal @@ -217,37 +185,6 @@ var generateBook = function(options) { return fs.remove(options.output); }) - // Get repo's URL - .then(function() { - // Git deactivated - if(options.github === false) { - options.github = null; - return null; - } else if(options.github) { - // Git already specified in options - options.github = ( - // Support URLs in "github" entry of book.json - parse.git.githubID(options.github) || - - // Fallback - options.github - ); - return; - } - - // Try auto detecting - return parse.git.url(options.input) - .then(function(_url) { - // Get ID of repo - return parse.git.githubID(_url); - }, function(err) { - return null; - }) - .then(function(repoId) { - options.github = repoId; - }); - }) - .then(function() { return fs.mkdirp(options.output); }) diff --git a/lib/generate/manifest.js b/lib/generate/manifest.js deleted file mode 100644 index 8bb826a..0000000 --- a/lib/generate/manifest.js +++ /dev/null @@ -1,72 +0,0 @@ -var _ = require('lodash'); -var path = require('path'); -var Q = require('q'); - -var fs = require("./fs"); - -var extsToIgnore = [".gz"] - -var Manifest = function() { - this.revision = 0; - this.clear(Date.now()); -}; - -// Regenerate manifest -Manifest.prototype.clear = function(revision) { - if (revision) this.revision = revision; - this.sections = { - 'CACHE': {}, - 'NETWORK': {}, - 'FALLBACK': {} - }; - return Q(this); -}; - -// Add a resource -Manifest.prototype.add = function(category, resource, value) { - if (_.isArray(resource)) { - _.each(resource, function(subres) { - this.add(category, subres, value); - }, this); - return; - } - this.sections[category][resource] = value; -}; - -// Add a directory in cache -Manifest.prototype.addFolder = function(folder, root, except) { - var that = this; - root = root || "/"; - - return fs.list(folder) - .then(function(files) { - _.each( - // Ignore diretcories - _.filter(files, function(file) { - return file.substr(-1) != "/" && !_.contains(except, path.join(root, file)) && !_.contains(extsToIgnore, path.extname(file)); - }), - function(file) { - that.add("CACHE", path.join(root, file)); - } - ); - }) -}; - -// Get manifest content -Manifest.prototype.dump = function() { - var lines = [ - "CACHE MANIFEST", - "# Revision "+this.revision - ]; - - _.each(this.sections, function(content, section) { - if (_.size(content) == 0) return; - lines.push(""); - lines.push(section+":"); - lines = lines.concat(_.keys(content)); - }, this); - - return Q(lines.join("\n")); -}; - -module.exports = Manifest; diff --git a/lib/generate/site/index.js b/lib/generate/site/index.js index 73765ae..6f29991 100644 --- a/lib/generate/site/index.js +++ b/lib/generate/site/index.js @@ -9,7 +9,6 @@ var parse = require("../../parse"); var BaseGenerator = require("../generator"); var links = require("../../utils/links"); var indexer = require('./search_indexer'); -var Manifest = require('../manifest'); @@ -21,10 +20,6 @@ var Generator = function() { this.revision = Date.now(); this.indexer = indexer(); - this.manifest = new Manifest(Date.now()); - this.manifest.add("NETWORK", [ - '*' - ]); }; util.inherits(Generator, BaseGenerator); @@ -144,8 +139,6 @@ Generator.prototype.convertFile = function(content, _input) { return _callHook("page"); }) .then(function() { - that.manifest.add("CACHE", _output); - return that._writeTemplate(that.template, { progress: page.progress, @@ -189,22 +182,12 @@ Generator.prototype.copyAssets = function() { path.join(that.options.output, "gitbook") ) - // Add to cache manifest - .then(function() { - return that.manifest.addFolder(path.join(that.options.output, "gitbook"), "gitbook"); - }) - // Copy plugins assets .then(function() { return Q.all( _.map(that.plugins.list, function(plugin) { var pluginAssets = path.join(that.options.output, "gitbook/plugins/", plugin.name); - return plugin.copyAssets(pluginAssets) - .then(function(copiedStuff) { - // Nothing was copied - if(!copiedStuff) { return; } - return that.manifest.addFolder(pluginAssets, "gitbook/plugins/"+plugin.name); - }); + return plugin.copyAssets(pluginAssets); }) ); }); @@ -219,24 +202,10 @@ Generator.prototype.writeSearchIndex = function() { }; -// Add cache manifest -Generator.prototype.writeCacheManifest = function() { - return fs.writeFile( - path.join(this.options.output, 'manifest.appcache'), - this.manifest.dump() - ); -}; - Generator.prototype.finish = function() { - var deferred = this.copyAssets() + return this.copyAssets() .then(this.copyCover) .then(this.writeSearchIndex); - - if (this.options.cache !== false) { - deferred = deferred.then(this.writeCacheManifest); - } - - return deferred; }; module.exports = Generator; |