diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-12-22 10:18:38 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-22 10:18:38 +0100 |
commit | 194ebc3da9641ff96f083f9d8ab43c2d27944f9a (patch) | |
tree | c50988f32ccf18df93ae7ab40be78e9459642818 /packages/gitbook/src/json | |
parent | 64ccb6b00b4b63fa0e516d4e35351275b34f8c07 (diff) | |
parent | 16af264360e48e8a833e9efa9ab8d194574dbc70 (diff) | |
download | gitbook-194ebc3da9641ff96f083f9d8ab43c2d27944f9a.zip gitbook-194ebc3da9641ff96f083f9d8ab43c2d27944f9a.tar.gz gitbook-194ebc3da9641ff96f083f9d8ab43c2d27944f9a.tar.bz2 |
Merge pull request #1543 from GitbookIO/dream
React for rendering website with plugins
Diffstat (limited to 'packages/gitbook/src/json')
-rw-r--r-- | packages/gitbook/src/json/encodeFile.js | 23 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeGlossary.js | 22 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeGlossaryEntry.js | 16 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeLanguages.js | 29 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodePage.js | 41 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeReadme.js | 18 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeState.js | 42 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeSummary.js | 23 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeSummaryArticle.js | 30 | ||||
-rw-r--r-- | packages/gitbook/src/json/encodeSummaryPart.js | 19 | ||||
-rw-r--r-- | packages/gitbook/src/json/index.js | 10 |
11 files changed, 273 insertions, 0 deletions
diff --git a/packages/gitbook/src/json/encodeFile.js b/packages/gitbook/src/json/encodeFile.js new file mode 100644 index 0000000..2295ac1 --- /dev/null +++ b/packages/gitbook/src/json/encodeFile.js @@ -0,0 +1,23 @@ + +/** + * Return a JSON representation of a file + * + * @param {File} file + * @param {URIIndex} urls + * @return {JSON} json + */ +function encodeFileToJson(file, urls) { + const filePath = file.getPath(); + if (!filePath) { + return undefined; + } + + return { + path: filePath, + mtime: file.getMTime(), + type: file.getType(), + url: urls.resolveToURL(filePath) + }; +} + +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..d82bb62 --- /dev/null +++ b/packages/gitbook/src/json/encodeGlossary.js @@ -0,0 +1,22 @@ +const encodeFile = require('./encodeFile'); +const encodeGlossaryEntry = require('./encodeGlossaryEntry'); + +/** + * Encode a glossary to JSON + * + * @param {Glossary} glossary + * @param {URIIndex} urls + * @return {JSON} json + */ +function encodeGlossary(glossary, urls) { + const file = glossary.getFile(); + const entries = glossary.getEntries(); + + return { + file: encodeFile(file, urls), + 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..52e13c3 --- /dev/null +++ b/packages/gitbook/src/json/encodeGlossaryEntry.js @@ -0,0 +1,16 @@ + +/** + * Encode a SummaryArticle to JSON + * + * @param {GlossaryEntry} entry + * @return {JSON} json + */ +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..809cfb2 --- /dev/null +++ b/packages/gitbook/src/json/encodeLanguages.js @@ -0,0 +1,29 @@ +const encodeFile = require('./encodeFile'); + +/** + * Encode a languages listing to JSON + * + * @param {Languages} languages + * @param {String} currentLanguage + * @param {URIIndex} urls + * @return {JSON} json +*/ +function encodeLanguages(languages, currentLanguage, urls) { + const file = languages.getFile(); + const list = languages.getList(); + + return { + file: encodeFile(file, urls), + current: currentLanguage, + list: list + .valueSeq() + .map(function(lang) { + return { + id: lang.getID(), + title: lang.getTitle() + }; + }).toJS() + }; +} + +module.exports = encodeLanguages; diff --git a/packages/gitbook/src/json/encodePage.js b/packages/gitbook/src/json/encodePage.js new file mode 100644 index 0000000..0671721 --- /dev/null +++ b/packages/gitbook/src/json/encodePage.js @@ -0,0 +1,41 @@ +const encodeSummaryArticle = require('./encodeSummaryArticle'); + +/** + * Return a JSON representation of a page. + * + * @param {Page} page + * @param {Summary} summary + * @param {URIIndex} urls + * @return {JSON} json + */ +function encodePage(page, summary, urls) { + const file = page.getFile(); + const attributes = page.getAttributes(); + const article = summary.getByPath(file.getPath()); + + const result = { + content: page.getContent(), + dir: page.getDir(), + attributes: 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, urls, false); + } + + const prevArticle = summary.getPrevArticle(article); + if (prevArticle) { + result.previous = encodeSummaryArticle(prevArticle, urls, false); + } + } + + 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..dff81cf --- /dev/null +++ b/packages/gitbook/src/json/encodeReadme.js @@ -0,0 +1,18 @@ +const encodeFile = require('./encodeFile'); + +/** + * Encode a readme to JSON. + * + * @param {Readme} readme + * @param {URIIndex} urls + * @return {JSON} json + */ +function encodeReadme(readme, urls) { + const file = readme.getFile(); + + return { + file: encodeFile(file, urls) + }; +} + +module.exports = encodeReadme; diff --git a/packages/gitbook/src/json/encodeState.js b/packages/gitbook/src/json/encodeState.js new file mode 100644 index 0000000..faac972 --- /dev/null +++ b/packages/gitbook/src/json/encodeState.js @@ -0,0 +1,42 @@ +const gitbook = require('../gitbook'); +const encodeSummary = require('./encodeSummary'); +const encodeGlossary = require('./encodeGlossary'); +const encodeReadme = require('./encodeReadme'); +const encodeLanguages = require('./encodeLanguages'); +const encodePage = require('./encodePage'); +const encodeFile = require('./encodeFile'); + +/** + * Encode context to JSON from an output instance. + * This JSON representation is used as initial state for the redux store. + * + * @param {Output} output + * @param {Page} page? + * @return {JSON} + */ +function encodeStateToJSON(output, page) { + const book = output.getBook(); + const urls = output.getURLIndex(); + + return { + output: { + name: output.getGenerator() + }, + gitbook: { + version: gitbook.version, + time: gitbook.START_TIME + }, + + summary: encodeSummary(book.getSummary(), urls), + glossary: encodeGlossary(book.getGlossary(), urls), + readme: encodeReadme(book.getReadme(), urls), + config: book.getConfig().getValues().toJS(), + languages: book.isMultilingual() ? + encodeLanguages(book.getLanguages(), book.getLanguage(), urls) : undefined, + + page: page ? encodePage(page, book.getSummary(), urls) : undefined, + file: page ? encodeFile(page.getFile(), urls) : undefined + }; +} + +module.exports = encodeStateToJSON; diff --git a/packages/gitbook/src/json/encodeSummary.js b/packages/gitbook/src/json/encodeSummary.js new file mode 100644 index 0000000..8380379 --- /dev/null +++ b/packages/gitbook/src/json/encodeSummary.js @@ -0,0 +1,23 @@ +const encodeFile = require('./encodeFile'); +const encodeSummaryPart = require('./encodeSummaryPart'); + +/** + * Encode a summary to JSON + * + * @param {Summary} summary + * @param {URIIndex} urls + * @return {Object} + */ +function encodeSummary(summary, urls) { + const file = summary.getFile(); + const parts = summary.getParts(); + + return { + file: encodeFile(file, urls), + parts: parts + .map(part => encodeSummaryPart(part, urls)) + .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..0fb6368 --- /dev/null +++ b/packages/gitbook/src/json/encodeSummaryArticle.js @@ -0,0 +1,30 @@ + +/** + * Encode a SummaryArticle to JSON + * + * @param {SummaryArticle} article + * @param {URIIndex} urls + * @param {Boolean} recursive + * @return {Object} + */ +function encodeSummaryArticle(article, urls, recursive) { + let articles = undefined; + if (recursive !== false) { + articles = article.getArticles() + .map(innerArticle => encodeSummaryArticle(innerArticle, urls, recursive)) + .toJS(); + } + + return { + title: article.getTitle(), + level: article.getLevel(), + depth: article.getDepth(), + anchor: article.getAnchor(), + url: urls.resolveToURL(article.getPath() || 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..fbcdc4c --- /dev/null +++ b/packages/gitbook/src/json/encodeSummaryPart.js @@ -0,0 +1,19 @@ +const encodeSummaryArticle = require('./encodeSummaryArticle'); + +/** + * Encode a SummaryPart to JSON. + * + * @param {SummaryPart} part + * @param {URIIndex} urls + * @return {JSON} json + */ +function encodeSummaryPart(part, urls) { + return { + title: part.getTitle(), + articles: part.getArticles() + .map(article => encodeSummaryArticle(article, urls)) + .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..49ab195 --- /dev/null +++ b/packages/gitbook/src/json/index.js @@ -0,0 +1,10 @@ + +module.exports = { + encodeState: require('./encodeState'), + encodeFile: require('./encodeFile'), + encodePage: require('./encodePage'), + encodeSummary: require('./encodeSummary'), + encodeSummaryArticle: require('./encodeSummaryArticle'), + encodeReadme: require('./encodeReadme'), + encodeLanguages: require('./encodeLanguages') +}; |