diff options
-rw-r--r-- | docs/variables.md | 3 | ||||
-rw-r--r-- | lib/book.js | 12 | ||||
-rw-r--r-- | lib/gitbook.js | 14 | ||||
-rw-r--r-- | lib/output/json.js | 10 |
4 files changed, 33 insertions, 6 deletions
diff --git a/docs/variables.md b/docs/variables.md index 35c7dd7..a0fc64e 100644 --- a/docs/variables.md +++ b/docs/variables.md @@ -15,12 +15,15 @@ The following is a reference of the available data during book's parsing and the | Variable | Description | | -------- | ----------- | +| `book.title` | Title specified in the `book.json` (or detected from the README) | +| `book.description` | Description specified in the `book.json` (or detected from the README) | | `book.[CONFIGURATION_DATA]` | All the `variables` set via the `book.json` are available through the book variable. | ### GitBook Variables | Variable | Description | | -------- | ----------- | +| `gitbook.time` | The current time (when you run the `gitbook` command). | | `gitbook.version` | Version of GitBook used to generate the book | ### File Variables diff --git a/lib/book.js b/lib/book.js index 173e9cc..7928f24 100644 --- a/lib/book.js +++ b/lib/book.js @@ -92,6 +92,18 @@ function Book(opts) { _.bindAll(this); } +// Return templating context for the book +Book.prototype.getContext = function() { + var variables = this.config.get('variables', {}); + + return { + book: _.extend({ + title: this.config.get('title'), + description: this.config.get('description') + }, variables) + }; +}; + // Parse and prepare the configuration, fail if invalid Book.prototype.prepareConfig = function() { return this.config.load(); diff --git a/lib/gitbook.js b/lib/gitbook.js index cbdcaed..54513c1 100644 --- a/lib/gitbook.js +++ b/lib/gitbook.js @@ -4,6 +4,8 @@ var pkg = require('../package.json'); var VERSION = pkg.version; var VERSION_STABLE = VERSION.replace(/\-(\S+)/g, ''); +var START_TIME = new Date(); + // Verify that this gitbook version satisfies a requirement // We can't directly use samver.satisfies since it will break all plugins when gitbook version is a prerelease (beta, alpha) function satisfies(condition) { @@ -14,8 +16,18 @@ function satisfies(condition) { return semver.satisfies(VERSION_STABLE, condition); } +// Return templating/json context for gitbook itself +function getContext() { + return { + gitbook: { + version: pkg.version, + time: START_TIME + } + }; +} module.exports = { version: pkg.version, - satisfies: satisfies + satisfies: satisfies, + getContext: getContext }; diff --git a/lib/output/json.js b/lib/output/json.js index 9e6cb3c..c4317c9 100644 --- a/lib/output/json.js +++ b/lib/output/json.js @@ -18,11 +18,11 @@ JSONOutput.prototype.onPage = function(page) { // Write as json .then(function() { - var json = _.extend(page.getContext(), { - gitbook: { - version: gitbook.version - } - }); + var json = _.extend( + page.getContext(), + gitbook.getContext(), + that.book.getContext() + ); return that.writeFile( page.withExtension('.json'), |