diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-04-26 17:02:59 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-04-26 17:02:59 +0200 |
commit | 8bac111acf121adf03cc2dff09bd0a1ce8cea19b (patch) | |
tree | e568a585c6ac78b05ff86044fb589edbd778b43d /lib/api | |
parent | 7a46d3b4977bcd1e115324880a409e88032899a7 (diff) | |
download | gitbook-8bac111acf121adf03cc2dff09bd0a1ce8cea19b.zip gitbook-8bac111acf121adf03cc2dff09bd0a1ce8cea19b.tar.gz gitbook-8bac111acf121adf03cc2dff09bd0a1ce8cea19b.tar.bz2 |
Add base for plugin JS api
Diffstat (limited to 'lib/api')
-rw-r--r-- | lib/api/decodeConfig.js | 15 | ||||
-rw-r--r-- | lib/api/decodeGlobal.js | 21 | ||||
-rw-r--r-- | lib/api/decodePage.js | 10 | ||||
-rw-r--r-- | lib/api/deprecate.js | 67 | ||||
-rw-r--r-- | lib/api/encodeConfig.js | 30 | ||||
-rw-r--r-- | lib/api/encodeGlobal.js | 46 | ||||
-rw-r--r-- | lib/api/encodePage.js | 20 | ||||
-rw-r--r-- | lib/api/index.js | 5 |
8 files changed, 208 insertions, 6 deletions
diff --git a/lib/api/decodeConfig.js b/lib/api/decodeConfig.js new file mode 100644 index 0000000..886ba45 --- /dev/null +++ b/lib/api/decodeConfig.js @@ -0,0 +1,15 @@ +var Config = require('../models/config'); + +/** + 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; + return Config.updateValues(config, values); +} + +module.exports = decodeGlobal; diff --git a/lib/api/decodeGlobal.js b/lib/api/decodeGlobal.js new file mode 100644 index 0000000..1b0b135 --- /dev/null +++ b/lib/api/decodeGlobal.js @@ -0,0 +1,21 @@ +var decodeConfig = require('./decodeConfig'); + +/** + Decode changes from a JS API to a output object + + @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 index b9941c1..5cce56c 100644 --- a/lib/api/decodePage.js +++ b/lib/api/decodePage.js @@ -1,15 +1,15 @@ /** - Decode changes from a JS API to a page boject + Decode changes from a JS API to a page object @param {Output} output - @param {Page} page - @param {Object} result + @param {Page} page: page instance to edit + @param {Object} result: result from API @return {Page} */ function decodePage(output, page, result) { - - + // todo + return page; } module.exports = decodePage; diff --git a/lib/api/deprecate.js b/lib/api/deprecate.js new file mode 100644 index 0000000..b890777 --- /dev/null +++ b/lib/api/deprecate.js @@ -0,0 +1,67 @@ +var logged = {}; + +/** + Log a deprecated notice + + @param {Book|Output} book + @param {String} key + @param {String} message +*/ +function logNotice(book, key, message) { + if (logged[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} property + @param {String} msg: message to print when called + @return {Function} +*/ +function deprecateField(book, key, instance, property, value, msg) { + var getter = function(){ + logNotice(book, key, msg); + return value; + }; + var setter = function(v) { + logNotice(book, key, msg); + value = v; + return value; + }; + + Object.defineProperty(instance, property, { + get: getter, + set: setter, + enumerable: true + }); +} + +module.exports = { + method: deprecateMethod, + field: deprecateField +}; diff --git a/lib/api/encodeConfig.js b/lib/api/encodeConfig.js new file mode 100644 index 0000000..d83f81a --- /dev/null +++ b/lib/api/encodeConfig.js @@ -0,0 +1,30 @@ +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'); + + return result; +} + +module.exports = encodeConfig; diff --git a/lib/api/encodeGlobal.js b/lib/api/encodeGlobal.js new file mode 100644 index 0000000..b2dd77a --- /dev/null +++ b/lib/api/encodeGlobal.js @@ -0,0 +1,46 @@ +var path = require('path'); + +var fs = require('../utils/fs'); + +var deprecate = require('./deprecate'); +var encodeConfig = require('./encodeConfig'); + +/** + 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 logger = output.getLogger(); + + var outputFolder = output.getOptions().get('root'); + + var result = { + log: logger, + config: encodeConfig(output, book.getConfig()) + }; + + result.output = { + name: 'website', + + toURL: function(s) { + return s; + }, + + writeFile: function(fileName, content) { + var filePath = path.join(outputFolder, fileName); + return fs.writeFile(filePath, content); + } + }; + + result.isLanguageBook = function() { + return false; + }; + + return result; +} + +module.exports = encodeGlobal; diff --git a/lib/api/encodePage.js b/lib/api/encodePage.js index 9ec55bc..98d7409 100644 --- a/lib/api/encodePage.js +++ b/lib/api/encodePage.js @@ -1,3 +1,5 @@ +var JSONUtils = require('../json'); +var deprecate = require('./deprecate'); /** Encode a page in a context to a JS API @@ -7,8 +9,26 @@ @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.section', 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/index.js b/lib/api/index.js index 213ef1a..5e67525 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -1,5 +1,8 @@ module.exports = { encodePage: require('./encodePage'), - decodePage: require('./decodePage') + decodePage: require('./decodePage'), + + encodeGlobal: require('./encodeGlobal'), + decodeGlobal: require('./decodeGlobal') }; |