summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-04-27 16:41:17 +0200
committerSamy Pessé <samypesse@gmail.com>2016-04-27 16:41:17 +0200
commit5b342b50ce862a4d60002451e6c1abd09a7b20ce (patch)
treea867e78a06111c24e35ae2f176b334bba23e25b4 /lib
parent869822d180f3fa7bf3a263bc29e280789abfed7c (diff)
downloadgitbook-5b342b50ce862a4d60002451e6c1abd09a7b20ce.zip
gitbook-5b342b50ce862a4d60002451e6c1abd09a7b20ce.tar.gz
gitbook-5b342b50ce862a4d60002451e6c1abd09a7b20ce.tar.bz2
Add language property to book for multilingual
Diffstat (limited to 'lib')
-rw-r--r--lib/api/encodeGlobal.js94
-rw-r--r--lib/models/book.js30
-rw-r--r--lib/parse/parseBook.js5
3 files changed, 71 insertions, 58 deletions
diff --git a/lib/api/encodeGlobal.js b/lib/api/encodeGlobal.js
index 20b706e..fa33696 100644
--- a/lib/api/encodeGlobal.js
+++ b/lib/api/encodeGlobal.js
@@ -20,61 +20,65 @@ function encodeGlobal(output) {
var result = {
log: logger,
- config: encodeConfig(output, book.getConfig())
- };
+ config: encodeConfig(output, book.getConfig()),
- result.output = {
- /**
- Name of the generator being used
- {String}
- */
- name: output.getGenerator(),
-
- /**
- Return absolute path to the root folder of output
- @return {String}
- */
- root: function() {
- return outputFolder;
+ isMultilingual: function() {
+ return book.isMultilingual();
},
- /**
- Convert a filepath into an url
- @return {String}
- */
- toURL: function(filePath) {
- return fileToURL(output, filePath);
+ isLanguageBook: function() {
+ return book.isLanguageBook();
},
- /**
- Write a file to the output folder,
- It creates the required folder
-
- @param {String} fileName
- @param {Buffer} content
- @return {Promise}
- */
- writeFile: function(fileName, content) {
- return Promise()
- .then(function() {
- var filePath = PathUtils.resolveInRoot(outputFolder, fileName);
-
- return fs.ensure(filePath)
+ isSubBook: deprecate.method(output, 'this.isSubBook', function() {
+ return book.isLanguageBook();
+ }, '"isSubBook" is deprecated, use "isLanguageBook()" instead'),
+
+ output: {
+ /**
+ Name of the generator being used
+ {String}
+ */
+ name: output.getGenerator(),
+
+ /**
+ Return absolute path to the root folder of output
+ @return {String}
+ */
+ root: function() {
+ return outputFolder;
+ },
+
+ /**
+ Convert a filepath into an url
+ @return {String}
+ */
+ toURL: function(filePath) {
+ return fileToURL(output, filePath);
+ },
+
+ /**
+ Write a file to the output folder,
+ It creates the required folder
+
+ @param {String} fileName
+ @param {Buffer} content
+ @return {Promise}
+ */
+ writeFile: function(fileName, content) {
+ return Promise()
.then(function() {
- return fs.writeFile(filePath, content);
+ var filePath = PathUtils.resolveInRoot(outputFolder, fileName);
+
+ return fs.ensure(filePath)
+ .then(function() {
+ return fs.writeFile(filePath, content);
+ });
});
- });
+ }
}
};
- result.isMultilingual = function() {
- return book.isMultilingual();
- };
-
- result.isLanguageBook = function() {
- return false;
- };
-
deprecate.field(output, 'this.book', result, 'book',
result, '"book" property is deprecated, use "this" directly instead');
diff --git a/lib/models/book.js b/lib/models/book.js
index 1835032..50276fa 100644
--- a/lib/models/book.js
+++ b/lib/models/book.js
@@ -29,9 +29,8 @@ var Book = Immutable.Record({
glossary: Glossary(),
languages: Languages(),
-
- // Parent of this book, if multilingual
- parent: null,
+ // ID of the language for language books
+ language: String(),
// List of children, if multilingual (String -> Book)
books: Immutable.OrderedMap()
@@ -69,14 +68,14 @@ Book.prototype.getLanguages = function() {
return this.get('languages');
};
-Book.prototype.getParent = function() {
- return this.get('parent');
-};
-
Book.prototype.getBooks = function() {
return this.get('books');
};
+Book.prototype.getLanguage = function() {
+ return this.get('language');
+};
+
/**
Return FS instance to access the content
@@ -162,6 +161,15 @@ Book.prototype.isMultilingual = function() {
};
/**
+ Return true if book is associated to a language
+
+ @return {Boolean}
+*/
+Book.prototype.isLanguageBook = function() {
+ return Boolean(this.getLanguage());
+};
+
+/**
Add a new language book
@param {String} language
@@ -203,14 +211,14 @@ Book.createForFS = function createForFS(fs) {
Create a language book from a parent
@param {Book} parent
- @param {String} basePath: folder o the book in the parent
+ @param {String} language
@return {Book}
*/
-Book.createFromParent = function createFromParent(parent, basePath) {
+Book.createFromParent = function createFromParent(parent, language) {
return new Book({
logger: parent.getLogger(),
- parent: parent,
- fs: FS.reduceScope(parent.getFS(), basePath)
+ language: language,
+ fs: FS.reduceScope(parent.getFS(), language)
});
};
diff --git a/lib/parse/parseBook.js b/lib/parse/parseBook.js
index c30027c..33f19f2 100644
--- a/lib/parse/parseBook.js
+++ b/lib/parse/parseBook.js
@@ -32,14 +32,15 @@ function parseMultilingualBook(book) {
var langList = languages.getList();
return Promise.reduce(langList, function(currentBook, lang) {
- var child = Book.createFromParent(currentBook, lang.getPath());
+ var langID = lang.getID();
+ var child = Book.createFromParent(currentBook, langID);
return Promise(child)
.then(parseIgnore)
.then(parseConfig)
.then(parseBookContent)
.then(function(result) {
- return currentBook.addLanguageBook(lang, result);
+ return currentBook.addLanguageBook(langID, result);
});
}, book);
}