diff options
Diffstat (limited to 'lib/api')
-rw-r--r-- | lib/api/decodeConfig.js | 17 | ||||
-rw-r--r-- | lib/api/decodeGlobal.js | 22 | ||||
-rw-r--r-- | lib/api/decodePage.js | 44 | ||||
-rw-r--r-- | lib/api/deprecate.js | 122 | ||||
-rw-r--r-- | lib/api/encodeConfig.js | 36 | ||||
-rw-r--r-- | lib/api/encodeGlobal.js | 257 | ||||
-rw-r--r-- | lib/api/encodeNavigation.js | 64 | ||||
-rw-r--r-- | lib/api/encodePage.js | 39 | ||||
-rw-r--r-- | lib/api/encodeProgress.js | 63 | ||||
-rw-r--r-- | lib/api/encodeSummary.js | 51 | ||||
-rw-r--r-- | lib/api/index.js | 8 |
11 files changed, 0 insertions, 723 deletions
diff --git a/lib/api/decodeConfig.js b/lib/api/decodeConfig.js deleted file mode 100644 index 5e00df5..0000000 --- a/lib/api/decodeConfig.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - Decode changes from a JS API to a config object - - @param {Config} config - @param {Object} result: result from API - @return {Config} -*/ -function decodeGlobal(config, result) { - var values = result.values; - - delete values.generator; - delete values.output; - - return config.updateValues(values); -} - -module.exports = decodeGlobal; diff --git a/lib/api/decodeGlobal.js b/lib/api/decodeGlobal.js deleted file mode 100644 index 118afb2..0000000 --- a/lib/api/decodeGlobal.js +++ /dev/null @@ -1,22 +0,0 @@ -var decodeConfig = require('./decodeConfig'); - -/** - Decode changes from a JS API to a output object. - Only the configuration can be edited by plugin's hooks - - @param {Output} output - @param {Object} result: result from API - @return {Output} -*/ -function decodeGlobal(output, result) { - var book = output.getBook(); - var config = book.getConfig(); - - // Update config - config = decodeConfig(config, result.config); - book = book.set('config', config); - - return output.set('book', book); -} - -module.exports = decodeGlobal; diff --git a/lib/api/decodePage.js b/lib/api/decodePage.js deleted file mode 100644 index c85dd1b..0000000 --- a/lib/api/decodePage.js +++ /dev/null @@ -1,44 +0,0 @@ -var deprecate = require('./deprecate'); - -/** - Decode changes from a JS API to a page object. - Only the content can be edited by plugin's hooks. - - @param {Output} output - @param {Page} page: page instance to edit - @param {Object} result: result from API - @return {Page} -*/ -function decodePage(output, page, result) { - var originalContent = page.getContent(); - - // No returned value - // Existing content will be used - if (!result) { - return page; - } - - deprecate.disable('page.sections'); - - // GitBook 3 - // Use returned page.content if different from original content - if (result.content != originalContent) { - page = page.set('content', result.content); - } - - // GitBook 2 compatibility - // Finally, use page.sections - else if (result.sections) { - page = page.set('content', - result.sections.map(function(section) { - return section.content; - }).join('\n') - ); - } - - deprecate.enable('page.sections'); - - return page; -} - -module.exports = decodePage; diff --git a/lib/api/deprecate.js b/lib/api/deprecate.js deleted file mode 100644 index 7a93a91..0000000 --- a/lib/api/deprecate.js +++ /dev/null @@ -1,122 +0,0 @@ -var is = require('is'); -var objectPath = require('object-path'); - -var logged = {}; -var disabled = {}; - -/** - Log a deprecated notice - - @param {Book|Output} book - @param {String} key - @param {String} message -*/ -function logNotice(book, key, message) { - if (logged[key] || disabled[key]) return; - - logged[key] = true; - - var logger = book.getLogger(); - logger.warn.ln(message); -} - -/** - Deprecate a function - - @param {Book|Output} book - @param {String} key: unique identitifer for the deprecated - @param {Function} fn - @param {String} msg: message to print when called - @return {Function} -*/ -function deprecateMethod(book, key, fn, msg) { - return function() { - logNotice(book, key, msg); - - return fn.apply(this, arguments); - }; -} - -/** - Deprecate a property of an object - - @param {Book|Output} book - @param {String} key: unique identitifer for the deprecated - @param {Object} instance - @param {String|Function} property - @param {String} msg: message to print when called - @return {Function} -*/ -function deprecateField(book, key, instance, property, value, msg) { - var store = undefined; - - var prepare = function() { - if (!is.undefined(store)) return; - - if (is.fn(value)) store = value(); - else store = value; - }; - - var getter = function(){ - prepare(); - - logNotice(book, key, msg); - return store; - }; - var setter = function(v) { - prepare(); - - logNotice(book, key, msg); - store = v; - return store; - }; - - Object.defineProperty(instance, property, { - get: getter, - set: setter, - enumerable: true, - configurable: true - }); -} - -/** - Enable a deprecation - - @param {String} key: unique identitifer -*/ -function enableDeprecation(key) { - disabled[key] = false; -} - -/** - Disable a deprecation - - @param {String} key: unique identitifer -*/ -function disableDeprecation(key) { - disabled[key] = true; -} - -/** - Deprecate a method in favor of another one - - @param {Book} book - @param {String} key - @param {Object} instance - @param {String} oldName - @param {String} newName -*/ -function deprecateRenamedMethod(book, key, instance, oldName, newName, msg) { - msg = msg || ('"' + oldName + '" is deprecated, use "' + newName + '()" instead'); - var fn = objectPath.get(instance, newName); - - instance[oldName] = deprecateMethod(book, key, fn, msg); -} - -module.exports = { - method: deprecateMethod, - renamedMethod: deprecateRenamedMethod, - field: deprecateField, - enable: enableDeprecation, - disable: disableDeprecation -}; diff --git a/lib/api/encodeConfig.js b/lib/api/encodeConfig.js deleted file mode 100644 index 2a05528..0000000 --- a/lib/api/encodeConfig.js +++ /dev/null @@ -1,36 +0,0 @@ -var objectPath = require('object-path'); -var deprecate = require('./deprecate'); - -/** - Encode a config object into a JS config api - - @param {Output} output - @param {Config} config - @return {Object} -*/ -function encodeConfig(output, config) { - var result = { - values: config.getValues().toJS(), - - get: function(key, defaultValue) { - return objectPath.get(result.values, key, defaultValue); - }, - - set: function(key, value) { - return objectPath.set(result.values, key, value); - } - }; - - deprecate.field(output, 'config.options', result, 'options', - result.values, '"config.options" property is deprecated, use "config.get(key)" instead'); - - deprecate.field(output, 'config.options.generator', result.values, 'generator', - output.getGenerator(), '"options.generator" property is deprecated, use "output.name" instead'); - - deprecate.field(output, 'config.options.generator', result.values, 'output', - output.getRoot(), '"options.output" property is deprecated, use "output.root()" instead'); - - return result; -} - -module.exports = encodeConfig; diff --git a/lib/api/encodeGlobal.js b/lib/api/encodeGlobal.js deleted file mode 100644 index a366526..0000000 --- a/lib/api/encodeGlobal.js +++ /dev/null @@ -1,257 +0,0 @@ -var path = require('path'); -var Promise = require('../utils/promise'); -var PathUtils = require('../utils/path'); -var fs = require('../utils/fs'); - -var Plugins = require('../plugins'); -var deprecate = require('./deprecate'); -var fileToURL = require('../output/helper/fileToURL'); -var defaultBlocks = require('../constants/defaultBlocks'); -var gitbook = require('../gitbook'); -var parsers = require('../parsers'); - -var encodeConfig = require('./encodeConfig'); -var encodeSummary = require('./encodeSummary'); -var encodeNavigation = require('./encodeNavigation'); -var encodePage = require('./encodePage'); - -/** - Encode a global context into a JS object - It's the context for page's hook, etc - - @param {Output} output - @return {Object} -*/ -function encodeGlobal(output) { - var book = output.getBook(); - var bookFS = book.getContentFS(); - var logger = output.getLogger(); - var outputFolder = output.getRoot(); - var plugins = output.getPlugins(); - var blocks = Plugins.listBlocks(plugins); - - var result = { - log: logger, - config: encodeConfig(output, book.getConfig()), - summary: encodeSummary(output, book.getSummary()), - - /** - Check if the book is a multilingual book - - @return {Boolean} - */ - isMultilingual: function() { - return book.isMultilingual(); - }, - - /** - Check if the book is a language book for a multilingual book - - @return {Boolean} - */ - isLanguageBook: function() { - return book.isLanguageBook(); - }, - - /** - Read a file from the book - - @param {String} fileName - @return {Promise<Buffer>} - */ - readFile: function(fileName) { - return bookFS.read(fileName); - }, - - /** - Read a file from the book as a string - - @param {String} fileName - @return {Promise<String>} - */ - readFileAsString: function(fileName) { - return bookFS.readAsString(fileName); - }, - - /** - Resolve a file from the book root - - @param {String} fileName - @return {String} - */ - resolve: function(fileName) { - return path.resolve(book.getContentRoot(), fileName); - }, - - /** - Resolve a page by it path - - @param {String} filePath - @return {String} - */ - getPageByPath: function(filePath) { - var page = output.getPage(filePath); - if (!page) return undefined; - - return encodePage(output, page); - }, - - /** - Render a block of text (markdown/asciidoc) - - @param {String} type - @param {String} text - @return {Promise<String>} - */ - renderBlock: function(type, text) { - var parser = parsers.get(type); - - return parser.parsePage(text) - .get('content'); - }, - - /** - Render an inline text (markdown/asciidoc) - - @param {String} type - @param {String} text - @return {Promise<String>} - */ - renderInline: function(type, text) { - var parser = parsers.get(type); - - return parser.parseInline(text) - .get('content'); - }, - - template: { - /** - Apply a templating block and returns its result - - @param {String} name - @param {Object} blockData - @return {Promise|Object} - */ - applyBlock: function(name, blockData) { - var block = blocks.get(name) || defaultBlocks.get(name); - return Promise(block.applyBlock(blockData, 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; - }, - - /** - Resolve a file from the output root - - @param {String} fileName - @return {String} - */ - resolve: function(fileName) { - return path.resolve(outputFolder, fileName); - }, - - /** - Convert a filepath into an url - @return {String} - */ - toURL: function(filePath) { - return fileToURL(output, filePath); - }, - - /** - Check that a file exists. - - @param {String} fileName - @return {Promise} - */ - hasFile: function(fileName, content) { - return Promise() - .then(function() { - var filePath = PathUtils.resolveInRoot(outputFolder, fileName); - - return fs.exists(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() { - var filePath = PathUtils.resolveInRoot(outputFolder, fileName); - - return fs.ensureFile(filePath) - .then(function() { - return fs.writeFile(filePath, content); - }); - }); - }, - - /** - Copy a file to the output folder - It creates the required folder. - - @param {String} inputFile - @param {String} outputFile - @param {Buffer} content - @return {Promise} - */ - copyFile: function(inputFile, outputFile, content) { - return Promise() - .then(function() { - var outputFilePath = PathUtils.resolveInRoot(outputFolder, outputFile); - - return fs.ensureFile(outputFilePath) - .then(function() { - return fs.copy(inputFile, outputFilePath); - }); - }); - } - }, - - gitbook: { - version: gitbook.version - } - }; - - // Deprecated properties - - deprecate.renamedMethod(output, 'this.isSubBook', result, 'isSubBook', 'isLanguageBook'); - deprecate.renamedMethod(output, 'this.contentLink', result, 'contentLink', 'output.toURL'); - - deprecate.field(output, 'this.generator', result, 'generator', - output.getGenerator(), '"this.generator" property is deprecated, use "this.output.name" instead'); - - deprecate.field(output, 'this.navigation', result, 'navigation', function() { - return encodeNavigation(output); - }, '"navigation" property is deprecated'); - - deprecate.field(output, 'this.book', result, 'book', - result, '"book" property is deprecated, use "this" directly instead'); - - deprecate.field(output, 'this.options', result, 'options', - result.config.values, '"options" property is deprecated, use config.get(key) instead'); - - return result; -} - -module.exports = encodeGlobal; diff --git a/lib/api/encodeNavigation.js b/lib/api/encodeNavigation.js deleted file mode 100644 index 8e329a1..0000000 --- a/lib/api/encodeNavigation.js +++ /dev/null @@ -1,64 +0,0 @@ -var Immutable = require('immutable'); - -/** - Encode an article for next/prev - - @param {Map<String:Page>} - @param {Article} - @return {Object} -*/ -function encodeArticle(pages, article) { - var articlePath = article.getPath(); - - return { - path: articlePath, - title: article.getTitle(), - level: article.getLevel(), - exists: (articlePath && pages.has(articlePath)), - external: article.isExternal() - }; -} - -/** - this.navigation is a deprecated property from GitBook v2 - - @param {Output} - @return {Object} -*/ -function encodeNavigation(output) { - var book = output.getBook(); - var pages = output.getPages(); - var summary = book.getSummary(); - var articles = summary.getArticlesAsList(); - - - var navigation = articles - .map(function(article, i) { - var ref = article.getRef(); - if (!ref) { - return undefined; - } - - var prev = articles.get(i - 1); - var next = articles.get(i + 1); - - return [ - ref, - { - index: i, - title: article.getTitle(), - introduction: (i === 0), - prev: prev? encodeArticle(pages, prev) : undefined, - next: next? encodeArticle(pages, next) : undefined, - level: article.getLevel() - } - ]; - }) - .filter(function(e) { - return Boolean(e); - }); - - return Immutable.Map(navigation).toJS(); -} - -module.exports = encodeNavigation; diff --git a/lib/api/encodePage.js b/lib/api/encodePage.js deleted file mode 100644 index 379d3d5..0000000 --- a/lib/api/encodePage.js +++ /dev/null @@ -1,39 +0,0 @@ -var JSONUtils = require('../json'); -var deprecate = require('./deprecate'); -var encodeProgress = require('./encodeProgress'); - -/** - Encode a page in a context to a JS API - - @param {Output} output - @param {Page} page - @return {Object} -*/ -function encodePage(output, page) { - var book = output.getBook(); - var summary = book.getSummary(); - var fs = book.getContentFS(); - var file = page.getFile(); - - // JS Page is based on the JSON output - var result = JSONUtils.encodePage(page, summary); - - result.type = file.getType(); - result.path = file.getPath(); - result.rawPath = fs.resolve(result.path); - - deprecate.field(output, 'page.progress', result, 'progress', function() { - return encodeProgress(output, page); - }, '"page.progress" property is deprecated'); - - deprecate.field(output, 'page.sections', result, 'sections', [ - { - content: result.content, - type: 'normal' - } - ], '"sections" property is deprecated, use page.content instead'); - - return result; -} - -module.exports = encodePage; diff --git a/lib/api/encodeProgress.js b/lib/api/encodeProgress.js deleted file mode 100644 index afa0341..0000000 --- a/lib/api/encodeProgress.js +++ /dev/null @@ -1,63 +0,0 @@ -var Immutable = require('immutable'); -var encodeNavigation = require('./encodeNavigation'); - -/** - page.progress is a deprecated property from GitBook v2 - - @param {Output} - @param {Page} - @return {Object} -*/ -function encodeProgress(output, page) { - var current = page.getPath(); - var navigation = encodeNavigation(output); - navigation = Immutable.Map(navigation); - - var n = navigation.size; - var percent = 0, prevPercent = 0, currentChapter = null; - var done = true; - - var chapters = navigation - .map(function(nav, chapterPath) { - nav.path = chapterPath; - return nav; - }) - .valueSeq() - .sortBy(function(nav) { - return nav.index; - }) - .map(function(nav, i) { - // Calcul percent - nav.percent = (i * 100) / Math.max((n - 1), 1); - - // Is it done - nav.done = done; - if (nav.path == current) { - currentChapter = nav; - percent = nav.percent; - done = false; - } else if (done) { - prevPercent = nav.percent; - } - - return nav; - }) - .toJS(); - - return { - // Previous percent - prevPercent: prevPercent, - - // Current percent - percent: percent, - - // List of chapter with progress - chapters: chapters, - - // Current chapter - current: currentChapter - }; -} - -module.exports = encodeProgress; - diff --git a/lib/api/encodeSummary.js b/lib/api/encodeSummary.js deleted file mode 100644 index 0d66ded..0000000 --- a/lib/api/encodeSummary.js +++ /dev/null @@ -1,51 +0,0 @@ -var encodeSummaryArticle = require('../json/encodeSummaryArticle'); - -/** - Encode summary to provide an API to plugin - - @param {Output} output - @param {Config} config - @return {Object} -*/ -function encodeSummary(output, summary) { - var result = { - /** - Iterate over the summary, it stops when the "iter" returns false - - @param {Function} iter - */ - walk: function (iter) { - summary.getArticle(function(article) { - var jsonArticle = encodeSummaryArticle(article, false); - - return iter(jsonArticle); - }); - }, - - /** - Get an article by its level - - @param {String} level - @return {Object} - */ - getArticleByLevel: function(level) { - var article = summary.getByLevel(level); - return (article? encodeSummaryArticle(article) : undefined); - }, - - /** - Get an article by its path - - @param {String} level - @return {Object} - */ - getArticleByPath: function(level) { - var article = summary.getByPath(level); - return (article? encodeSummaryArticle(article) : undefined); - } - }; - - return result; -} - -module.exports = encodeSummary; diff --git a/lib/api/index.js b/lib/api/index.js deleted file mode 100644 index 5e67525..0000000 --- a/lib/api/index.js +++ /dev/null @@ -1,8 +0,0 @@ - -module.exports = { - encodePage: require('./encodePage'), - decodePage: require('./decodePage'), - - encodeGlobal: require('./encodeGlobal'), - decodeGlobal: require('./decodeGlobal') -}; |