diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-09-05 12:32:25 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-09-05 12:32:25 +0200 |
commit | b796069adeadd655ad34dc425ff7a05d7a54cd47 (patch) | |
tree | 30edaffa584e7d3db3f4c65f401f4a8f58f4eef9 /packages/gitbook/src/json | |
parent | 839904a70419aaef1006be820ec092d978236a20 (diff) | |
download | gitbook-b796069adeadd655ad34dc425ff7a05d7a54cd47.zip gitbook-b796069adeadd655ad34dc425ff7a05d7a54cd47.tar.gz gitbook-b796069adeadd655ad34dc425ff7a05d7a54cd47.tar.bz2 |
Move gitbook source code to es6 (linted)
Diffstat (limited to 'packages/gitbook/src/json')
-rw-r--r-- | packages/gitbook/src/json/encodeBook.js | 39 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeBookWithPage.js | 22 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeFile.js | 21 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeGlossary.js | 21 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeGlossaryEntry.js | 16 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeLanguages.js | 26 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeOutput.js | 25 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeOutputWithPage.js | 23 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodePage.js | 39 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeReadme.js | 17 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeSummary.js | 20 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeSummaryArticle.js | 28 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeSummaryPart.js | 17 | ||||
-rw-r--r-- | packages/gitbook/src/json/index.js | 13 |
14 files changed, 327 insertions, 0 deletions
diff --git a/packages/gitbook/src/json/encodeBook.js b/packages/gitbook/src/json/encodeBook.js new file mode 100644 index 0000000..0b259a9 --- /dev/null +++ b/packages/gitbook/src/json/encodeBook.js @@ -0,0 +1,39 @@ +const extend = require('extend'); + +const gitbook = require('../gitbook'); +const encodeSummary = require('./encodeSummary'); +const encodeGlossary = require('./encodeGlossary'); +const encodeReadme = require('./encodeReadme'); +const encodeLanguages = require('./encodeLanguages'); + +/** + Encode a book to JSON + + @param {Book} + @return {Object} +*/ +function encodeBookToJson(book) { + const config = book.getConfig(); + const language = book.getLanguage(); + + const variables = config.getValue('variables', {}); + + return { + summary: encodeSummary(book.getSummary()), + glossary: encodeGlossary(book.getGlossary()), + readme: encodeReadme(book.getReadme()), + config: book.getConfig().getValues().toJS(), + + languages: book.isMultilingual() ? encodeLanguages(book.getLanguages()) : undefined, + + gitbook: { + version: gitbook.version, + time: gitbook.START_TIME + }, + book: extend({ + language: language ? language : undefined + }, variables.toJS()) + }; +} + +module.exports = encodeBookToJson; diff --git a/packages/gitbook/src/json/encodeBookWithPage.js b/packages/gitbook/src/json/encodeBookWithPage.js new file mode 100644 index 0000000..f593af2 --- /dev/null +++ b/packages/gitbook/src/json/encodeBookWithPage.js @@ -0,0 +1,22 @@ +const encodeBook = require('./encodeBook'); +const encodePage = require('./encodePage'); +const encodeFile = require('./encodeFile'); + +/** + * Return a JSON representation of a book with a specific file + * + * @param {Book} output + * @param {Page} page + * @return {Object} + */ +function encodeBookWithPage(book, page) { + const file = page.getFile(); + + const result = encodeBook(book); + result.page = encodePage(page, book.getSummary()); + result.file = encodeFile(file); + + return result; +} + +module.exports = encodeBookWithPage; diff --git a/packages/gitbook/src/json/encodeFile.js b/packages/gitbook/src/json/encodeFile.js new file mode 100644 index 0000000..487a74c --- /dev/null +++ b/packages/gitbook/src/json/encodeFile.js @@ -0,0 +1,21 @@ + +/** + Return a JSON representation of a file + + @param {File} file + @return {Object} +*/ +function encodeFileToJson(file) { + const filePath = file.getPath(); + if (!filePath) { + return undefined; + } + + return { + path: filePath, + mtime: file.getMTime(), + type: file.getType() + }; +} + +module.exports = encodeFileToJson; diff --git a/packages/gitbook/src/json/encodeGlossary.js b/packages/gitbook/src/json/encodeGlossary.js new file mode 100644 index 0000000..6bdd683 --- /dev/null +++ b/packages/gitbook/src/json/encodeGlossary.js @@ -0,0 +1,21 @@ +const encodeFile = require('./encodeFile'); +const encodeGlossaryEntry = require('./encodeGlossaryEntry'); + +/** + Encode a glossary to JSON + + @param {Glossary} + @return {Object} +*/ +function encodeGlossary(glossary) { + const file = glossary.getFile(); + const entries = glossary.getEntries(); + + return { + file: encodeFile(file), + entries: entries + .map(encodeGlossaryEntry).toJS() + }; +} + +module.exports = encodeGlossary; diff --git a/packages/gitbook/src/json/encodeGlossaryEntry.js b/packages/gitbook/src/json/encodeGlossaryEntry.js new file mode 100644 index 0000000..d163f45 --- /dev/null +++ b/packages/gitbook/src/json/encodeGlossaryEntry.js @@ -0,0 +1,16 @@ + +/** + Encode a SummaryArticle to JSON + + @param {GlossaryEntry} + @return {Object} +*/ +function encodeGlossaryEntry(entry) { + return { + id: entry.getID(), + name: entry.getName(), + description: entry.getDescription() + }; +} + +module.exports = encodeGlossaryEntry; diff --git a/packages/gitbook/src/json/encodeLanguages.js b/packages/gitbook/src/json/encodeLanguages.js new file mode 100644 index 0000000..fc7487b --- /dev/null +++ b/packages/gitbook/src/json/encodeLanguages.js @@ -0,0 +1,26 @@ +const encodeFile = require('./encodeFile'); + +/** + Encode a languages listing to JSON + + @param {Languages} + @return {Object} +*/ +function encodeLanguages(languages) { + const file = languages.getFile(); + const list = languages.getList(); + + return { + file: encodeFile(file), + list: list + .valueSeq() + .map(function(lang) { + return { + id: lang.getID(), + title: lang.getTitle() + }; + }).toJS() + }; +} + +module.exports = encodeLanguages; diff --git a/packages/gitbook/src/json/encodeOutput.js b/packages/gitbook/src/json/encodeOutput.js new file mode 100644 index 0000000..31e5757 --- /dev/null +++ b/packages/gitbook/src/json/encodeOutput.js @@ -0,0 +1,25 @@ +const encodeBook = require('./encodeBook'); + +/** + * Encode an output to JSON + * + * @param {Output} + * @return {Object} + */ +function encodeOutputToJson(output) { + const book = output.getBook(); + const generator = output.getGenerator(); + const options = output.getOptions(); + + const result = encodeBook(book); + + result.output = { + name: generator + }; + + result.options = options.toJS(); + + return result; +} + +module.exports = encodeOutputToJson; diff --git a/packages/gitbook/src/json/encodeOutputWithPage.js b/packages/gitbook/src/json/encodeOutputWithPage.js new file mode 100644 index 0000000..58db070 --- /dev/null +++ b/packages/gitbook/src/json/encodeOutputWithPage.js @@ -0,0 +1,23 @@ +const encodeOutput = require('./encodeOutput'); +const encodePage = require('./encodePage'); +const encodeFile = require('./encodeFile'); + +/** + * Return a JSON representation of a book with a specific file + * + * @param {Book} output + * @param {Page} page + * @return {Object} + */ +function encodeOutputWithPage(output, page) { + const file = page.getFile(); + const book = output.getBook(); + + const result = encodeOutput(output); + result.page = encodePage(page, book.getSummary()); + result.file = encodeFile(file); + + return result; +} + +module.exports = encodeOutputWithPage; diff --git a/packages/gitbook/src/json/encodePage.js b/packages/gitbook/src/json/encodePage.js new file mode 100644 index 0000000..b20a40c --- /dev/null +++ b/packages/gitbook/src/json/encodePage.js @@ -0,0 +1,39 @@ +const encodeSummaryArticle = require('./encodeSummaryArticle'); + +/** + Return a JSON representation of a page + + @param {Page} page + @param {Summary} summary + @return {Object} +*/ +function encodePage(page, summary) { + const file = page.getFile(); + const attributes = page.getAttributes(); + const article = summary.getByPath(file.getPath()); + + const result = attributes.toJS(); + + if (article) { + result.title = article.getTitle(); + result.level = article.getLevel(); + result.depth = article.getDepth(); + + const nextArticle = summary.getNextArticle(article); + if (nextArticle) { + result.next = encodeSummaryArticle(nextArticle); + } + + const prevArticle = summary.getPrevArticle(article); + if (prevArticle) { + result.previous = encodeSummaryArticle(prevArticle); + } + } + + result.content = page.getContent(); + result.dir = page.getDir(); + + return result; +} + +module.exports = encodePage; diff --git a/packages/gitbook/src/json/encodeReadme.js b/packages/gitbook/src/json/encodeReadme.js new file mode 100644 index 0000000..cc71bcb --- /dev/null +++ b/packages/gitbook/src/json/encodeReadme.js @@ -0,0 +1,17 @@ +const encodeFile = require('./encodeFile'); + +/** + Encode a readme to JSON + + @param {Readme} + @return {Object} +*/ +function encodeReadme(readme) { + const file = readme.getFile(); + + return { + file: encodeFile(file) + }; +} + +module.exports = encodeReadme; diff --git a/packages/gitbook/src/json/encodeSummary.js b/packages/gitbook/src/json/encodeSummary.js new file mode 100644 index 0000000..9a07da4 --- /dev/null +++ b/packages/gitbook/src/json/encodeSummary.js @@ -0,0 +1,20 @@ +const encodeFile = require('./encodeFile'); +const encodeSummaryPart = require('./encodeSummaryPart'); + +/** + Encode a summary to JSON + + @param {Summary} + @return {Object} +*/ +function encodeSummary(summary) { + const file = summary.getFile(); + const parts = summary.getParts(); + + return { + file: encodeFile(file), + parts: parts.map(encodeSummaryPart).toJS() + }; +} + +module.exports = encodeSummary; diff --git a/packages/gitbook/src/json/encodeSummaryArticle.js b/packages/gitbook/src/json/encodeSummaryArticle.js new file mode 100644 index 0000000..0b9461c --- /dev/null +++ b/packages/gitbook/src/json/encodeSummaryArticle.js @@ -0,0 +1,28 @@ + +/** + Encode a SummaryArticle to JSON + + @param {SummaryArticle} + @return {Object} +*/ +function encodeSummaryArticle(article, recursive) { + let articles = undefined; + if (recursive !== false) { + articles = article.getArticles() + .map(encodeSummaryArticle) + .toJS(); + } + + return { + title: article.getTitle(), + level: article.getLevel(), + depth: article.getDepth(), + anchor: article.getAnchor(), + url: article.getUrl(), + path: article.getPath(), + ref: article.getRef(), + articles + }; +} + +module.exports = encodeSummaryArticle; diff --git a/packages/gitbook/src/json/encodeSummaryPart.js b/packages/gitbook/src/json/encodeSummaryPart.js new file mode 100644 index 0000000..eb16719 --- /dev/null +++ b/packages/gitbook/src/json/encodeSummaryPart.js @@ -0,0 +1,17 @@ +const encodeSummaryArticle = require('./encodeSummaryArticle'); + +/** + Encode a SummaryPart to JSON + + @param {SummaryPart} + @return {Object} +*/ +function encodeSummaryPart(part) { + return { + title: part.getTitle(), + articles: part.getArticles() + .map(encodeSummaryArticle).toJS() + }; +} + +module.exports = encodeSummaryPart; diff --git a/packages/gitbook/src/json/index.js b/packages/gitbook/src/json/index.js new file mode 100644 index 0000000..3b68f5e --- /dev/null +++ b/packages/gitbook/src/json/index.js @@ -0,0 +1,13 @@ + +module.exports = { + encodeOutput: require('./encodeOutput'), + encodeBookWithPage: require('./encodeBookWithPage'), + encodeOutputWithPage: require('./encodeOutputWithPage'), + encodeBook: require('./encodeBook'), + encodeFile: require('./encodeFile'), + encodePage: require('./encodePage'), + encodeSummary: require('./encodeSummary'), + encodeSummaryArticle: require('./encodeSummaryArticle'), + encodeReadme: require('./encodeReadme'), + encodeLanguages: require('./encodeLanguages') +}; |