summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/api/decodeConfig.js15
-rw-r--r--lib/api/decodeGlobal.js21
-rw-r--r--lib/api/decodePage.js10
-rw-r--r--lib/api/deprecate.js67
-rw-r--r--lib/api/encodeConfig.js30
-rw-r--r--lib/api/encodeGlobal.js46
-rw-r--r--lib/api/encodePage.js20
-rw-r--r--lib/api/index.js5
-rw-r--r--lib/output/callHook.js12
-rw-r--r--lib/output/callPageHook.js6
-rw-r--r--lib/output/generateBook.js4
-rw-r--r--lib/utils/error.js22
12 files changed, 227 insertions, 31 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')
};
diff --git a/lib/output/callHook.js b/lib/output/callHook.js
index 8d34bdc..1ff1a41 100644
--- a/lib/output/callHook.js
+++ b/lib/output/callHook.js
@@ -1,4 +1,5 @@
var Promise = require('../utils/promise');
+var Api = require('../api');
function defaultGetArgument() {
return undefined;
@@ -26,7 +27,13 @@ function callHook(name, getArgument, handleResult, output) {
logger.debug.ln('calling hook "' + name + '"');
+ // Create the JS context for plugins
+ var context = Api.encodeGlobal(output);
+
+ // Get the arguments
return Promise(getArgument(output))
+
+ // Call the hooks in serie
.then(function(arg) {
return Promise.reduce(plugins, function(prev, plugin) {
var hook = plugin.getHook(name);
@@ -34,10 +41,13 @@ function callHook(name, getArgument, handleResult, output) {
return prev;
}
- return hook(prev);
+ return hook.call(context, prev);
}, arg);
})
+
+ // Handle final result
.then(function(result) {
+ output = Api.decodeGlobal(output, context);
return handleResult(output, result);
});
}
diff --git a/lib/output/callPageHook.js b/lib/output/callPageHook.js
index 6f6bcbb..0582da9 100644
--- a/lib/output/callPageHook.js
+++ b/lib/output/callPageHook.js
@@ -12,9 +12,7 @@ var Promise = require('../utils/promise');
@return {Promise<Page>}
*/
function callPageHook(name, output, page) {
- return Promise(page);
-
- /*return callHook(
+ return callHook(
name,
function(out) {
@@ -26,7 +24,7 @@ function callPageHook(name, output, page) {
},
output
- );*/
+ );
}
module.exports = callPageHook;
diff --git a/lib/output/generateBook.js b/lib/output/generateBook.js
index 1fe75fd..ab07e41 100644
--- a/lib/output/generateBook.js
+++ b/lib/output/generateBook.js
@@ -105,7 +105,7 @@ function generateBook(generator, book, options) {
return generator.onFinish(output);
})
- /*.then(callHook.bind(null,
+ .then(callHook.bind(null,
'finish',
function(output) {
return {};
@@ -114,7 +114,7 @@ function generateBook(generator, book, options) {
return output;
}
)
- )*/
+ )
.then(function(output) {
var logger = output.getLogger();
diff --git a/lib/utils/error.js b/lib/utils/error.js
index d34c17e..7686779 100644
--- a/lib/utils/error.js
+++ b/lib/utils/error.js
@@ -1,15 +1,12 @@
-var _ = require('lodash');
+var is = require('is');
+
var TypedError = require('error/typed');
var WrappedError = require('error/wrapped');
-var deprecated = require('deprecated');
-
-var Logger = require('./logger');
-var log = new Logger();
// Enforce as an Error object, and cleanup message
function enforce(err) {
- if (_.isString(err)) err = new Error(err);
+ if (is.string(err)) err = new Error(err);
err.message = err.message.replace(/^Error: /, '');
return err;
@@ -84,14 +81,6 @@ var EbookError = WrappedError({
stdout: ''
});
-// Deprecate methods/fields
-function deprecateMethod(fn, msg) {
- return deprecated.method(msg, log.warn.ln, fn);
-}
-function deprecateField(obj, prop, value, msg) {
- return deprecated.field(msg, log.warn.ln, obj, prop, value);
-}
-
module.exports = {
enforce: enforce,
@@ -106,8 +95,5 @@ module.exports = {
TemplateError: TemplateError,
PluginError: PluginError,
ConfigurationError: ConfigurationError,
- EbookError: EbookError,
-
- deprecateMethod: deprecateMethod,
- deprecateField: deprecateField
+ EbookError: EbookError
};