diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-05-12 11:15:13 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-05-12 11:15:13 +0200 |
commit | 8aa2f3075c81161746cd63a388ce0eddc536ac15 (patch) | |
tree | a33ac0df67023a69b985b650e3b33af7e12ae117 | |
parent | 839ee0385af2e6a9917826b07dd61c851244e6e0 (diff) | |
download | gitbook-8aa2f3075c81161746cd63a388ce0eddc536ac15.zip gitbook-8aa2f3075c81161746cd63a388ce0eddc536ac15.tar.gz gitbook-8aa2f3075c81161746cd63a388ce0eddc536ac15.tar.bz2 |
Fix #1294: multilingual book should extend the book's config
-rw-r--r-- | lib/api/decodeConfig.js | 4 | ||||
-rw-r--r-- | lib/models/config.js | 54 | ||||
-rw-r--r-- | lib/output/generateBook.js | 3 | ||||
-rw-r--r-- | lib/parse/__tests__/parseBook.js | 35 | ||||
-rw-r--r-- | lib/parse/parseConfig.js | 16 |
5 files changed, 87 insertions, 25 deletions
diff --git a/lib/api/decodeConfig.js b/lib/api/decodeConfig.js index 351ed05..5e00df5 100644 --- a/lib/api/decodeConfig.js +++ b/lib/api/decodeConfig.js @@ -1,5 +1,3 @@ -var Config = require('../models/config'); - /** Decode changes from a JS API to a config object @@ -13,7 +11,7 @@ function decodeGlobal(config, result) { delete values.generator; delete values.output; - return Config.updateValues(config, values); + return config.updateValues(values); } module.exports = decodeGlobal; diff --git a/lib/models/config.js b/lib/models/config.js index 070fe92..99b0f1a 100644 --- a/lib/models/config.js +++ b/lib/models/config.js @@ -19,6 +19,16 @@ Config.prototype.getValues = function() { }; /** + Change the file for the configuration + + @param {File} file + @return {Config} +*/ +Config.prototype.setFile = function(file) { + return this.set('file', file); +}; + +/** Return a configuration value by its key path @param {String} key @@ -40,7 +50,7 @@ Config.prototype.getValue = function(keyPath, def) { @param {String} key @param {Mixed} value - @return {Mixed} + @return {Config} */ Config.prototype.setValue = function(keyPath, value) { keyPath = Config.keyToKeyPath(keyPath); @@ -94,6 +104,35 @@ Config.prototype.setPluginDependencies = function(deps) { return this.setValue('plugins', plugins); }; + +/** + Update values for an existing configuration + + @param {Object} values + @returns {Config} +*/ +Config.prototype.updateValues = function(values) { + values = Immutable.fromJS(values); + + return this.set('values', values); +}; + +/** + Update values for an existing configuration + + @param {Config} config + @param {Object} values + @returns {Config} +*/ +Config.prototype.mergeValues = function(values) { + var currentValues = this.getValues(); + values = Immutable.fromJS(values); + + currentValues = currentValues.mergeDeep(values); + + return this.set('values', currentValues); +}; + /** Create a new config for a file @@ -120,19 +159,6 @@ Config.createWithValues = function(values) { }); }; -/** - Update values for an existing configuration - - @param {Config} config - @param {Object} values - @returns {Config} -*/ -Config.updateValues = function(config, values) { - values = Immutable.fromJS(values); - - return config.set('values', values); -}; - /** Convert a keyPath to an array of keys diff --git a/lib/output/generateBook.js b/lib/output/generateBook.js index f17997c..bf40274 100644 --- a/lib/output/generateBook.js +++ b/lib/output/generateBook.js @@ -2,7 +2,6 @@ var path = require('path'); var Immutable = require('immutable'); var Output = require('../models/output'); -var Config = require('../models/config'); var Promise = require('../utils/promise'); var fs = require('../utils/fs'); @@ -41,7 +40,7 @@ function processOutput(generator, startOutput) { var book = output.getBook(); var config = book.getConfig(); - config = Config.updateValues(config, result); + config = config.updateValues(result); book = book.set('config', config); return output.set('book', book); } diff --git a/lib/parse/__tests__/parseBook.js b/lib/parse/__tests__/parseBook.js index 912d32d..b1236c9 100644 --- a/lib/parse/__tests__/parseBook.js +++ b/lib/parse/__tests__/parseBook.js @@ -27,6 +27,41 @@ describe('parseBook', function() { }); }); + it('should extend configuration for multilingual book', function() { + var fs = createMockFS({ + 'LANGS.md': '# Languages\n\n* [en](en)\n* [fr](fr)', + 'book.json': '{ "title": "Test", "author": "GitBook" }', + 'en': { + 'README.md': 'Hello', + 'book.json': '{ "title": "Test EN" }' + }, + 'fr': { + 'README.md': 'Bonjour' + } + }); + var book = Book.createForFS(fs); + + return parseBook(book) + .then(function(resultBook) { + var books = resultBook.getBooks(); + + expect(resultBook.isMultilingual()).toBe(true); + expect(books.size).toBe(2); + + var en = books.get('en'); + var fr = books.get('fr'); + + var enConfig = en.getConfig(); + var frConfig = fr.getConfig(); + + expect(enConfig.getValue('title')).toBe('Test EN'); + expect(enConfig.getValue('author')).toBe('GitBook'); + + expect(frConfig.getValue('title')).toBe('Test'); + expect(frConfig.getValue('author')).toBe('GitBook'); + }); + }); + it('should parse book in a directory', function() { var fs = createMockFS({ 'book.json': JSON.stringify({ diff --git a/lib/parse/parseConfig.js b/lib/parse/parseConfig.js index 5200de2..39d57f0 100644 --- a/lib/parse/parseConfig.js +++ b/lib/parse/parseConfig.js @@ -1,7 +1,5 @@ var Promise = require('../utils/promise'); -var Config = require('../models/config'); -var File = require('../models/file'); var validateConfig = require('./validateConfig'); var CONFIG_FILES = require('../constants/configFiles'); @@ -13,6 +11,7 @@ var CONFIG_FILES = require('../constants/configFiles'); */ function parseConfig(book) { var fs = book.getFS(); + var config = book.getConfig(); return Promise.some(CONFIG_FILES, function(filename) { // Is this file ignored? @@ -38,13 +37,18 @@ function parseConfig(book) { }) .then(function(result) { - var file = result? result.file : File(); var values = result? result.values : {}; - values = validateConfig(values); - var config = Config.create(file, values); - return book.set('config', config); + // Set the file + if (result.file) { + config = config.setFile(result.file); + } + + // Merge with old values + config = config.mergeValues(values); + + return book.setConfig(config); }); } |