diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-17 16:20:45 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-17 16:20:45 +0100 |
commit | 4e7cb9b23f9234fcbcdc1d151501c1a7a7ea0028 (patch) | |
tree | 6717ebb00890852ce4aeb68a056dc949405b29ea /lib/output | |
parent | 5fdee2cb5e846303b5bd4f10552b1d60201c3cb7 (diff) | |
download | gitbook-4e7cb9b23f9234fcbcdc1d151501c1a7a7ea0028.zip gitbook-4e7cb9b23f9234fcbcdc1d151501c1a7a7ea0028.tar.gz gitbook-4e7cb9b23f9234fcbcdc1d151501c1a7a7ea0028.tar.bz2 |
Load template engine for website output using theme configuration
Diffstat (limited to 'lib/output')
-rw-r--r-- | lib/output/conrefs.js | 4 | ||||
-rw-r--r-- | lib/output/ebook.js | 9 | ||||
-rw-r--r-- | lib/output/folder.js | 2 | ||||
-rw-r--r-- | lib/output/json.js | 1 | ||||
-rw-r--r-- | lib/output/website.js | 98 | ||||
-rw-r--r-- | lib/output/website/index.js | 14 | ||||
-rw-r--r-- | lib/output/website/theme.js | 6 |
7 files changed, 108 insertions, 26 deletions
diff --git a/lib/output/conrefs.js b/lib/output/conrefs.js index f1cd123..af37de8 100644 --- a/lib/output/conrefs.js +++ b/lib/output/conrefs.js @@ -1,7 +1,7 @@ var path = require('path'); var util = require('util'); -var FolderOutput = require('./folder')(); +var folderOutput = require('./folder'); var Git = require('../utils/git'); var fs = require('../utils/fs'); var pathUtil = require('../utils/path'); @@ -11,7 +11,7 @@ Mixin for output to resolve git conrefs */ module.exports = function conrefsLoader(Base) { - Base = Base || FolderOutput; + Base = folderOutput(Base); function ConrefsLoader() { Base.apply(this, arguments); diff --git a/lib/output/ebook.js b/lib/output/ebook.js index 010f3d7..7e7a66d 100644 --- a/lib/output/ebook.js +++ b/lib/output/ebook.js @@ -1,9 +1,14 @@ +var util = require('util'); + var WebsiteOutput = require('./website'); var assetsInliner = require('./assets-inliner'); -var EbookOutput = assetsInliner(WebsiteOutput); +function EbookOutput() { + WebsiteOutput.apply(this, arguments); +} +util.inherits(EbookOutput, WebsiteOutput); EbookOutput.prototype.name = 'ebook'; -module.exports = EbookOutput; +module.exports = assetsInliner(EbookOutput); diff --git a/lib/output/folder.js b/lib/output/folder.js index 05843cd..5a51346 100644 --- a/lib/output/folder.js +++ b/lib/output/folder.js @@ -34,7 +34,7 @@ module.exports = function folderOutput(Base) { return Promise() .then(function() { - return Base.prototype.prepare.apply(that); + return FolderOutput.super_.prototype.prepare.apply(that); }) // Cleanup output folder diff --git a/lib/output/json.js b/lib/output/json.js index 5c9e1f5..5805579 100644 --- a/lib/output/json.js +++ b/lib/output/json.js @@ -1,4 +1,3 @@ -var _ = require('lodash'); var conrefsLoader = require('./conrefs'); var JSONOutput = conrefsLoader(); diff --git a/lib/output/website.js b/lib/output/website.js new file mode 100644 index 0000000..41395e6 --- /dev/null +++ b/lib/output/website.js @@ -0,0 +1,98 @@ +var _ = require('lodash'); +var path = require('path'); +var util = require('util'); +var nunjucks = require('nunjucks'); + +var Promise = require('../utils/promise'); +var conrefsLoader = require('./conrefs'); +var Output = require('./base'); + +// Tranform a theme ID into a plugin +function themeID(plugin) { + return 'theme-' + plugin; +} + +function WebsiteOutput() { + Output.apply(this, arguments); + + // Nunjucks environment + this.env; + + // Plugin instance for the main theme + this.theme; + + // Plugin instance for the default theme + this.defaultTheme; +} +util.inherits(WebsiteOutput, Output); + +// Name of the generator +// It's being used as a prefix for templates +WebsiteOutput.prototype.name = 'website'; + +// Load and setup the theme +WebsiteOutput.prototype.prepare = function() { + var that = this; + + return Promise() + .then(function() { + return WebsiteOutput.super_.prototype.prepare.apply(that); + }) + + .then(function() { + var themeName = that.book.config.get('theme'); + that.theme = that.plugins.get(themeID(themeName)); + that.themeDefault = that.plugins.get(themeID('default')); + + if (!that.theme) { + throw new Error('Theme "' + themeName + '" is not installed, add "' + themeID(themeName) + '" to your "book.json"'); + } + + var searchPaths = _.chain([ + // The book itself can contains a "_layouts" folder + that.book.root, + + // Installed plugin (it can be identical to themeDefault.root) + that.theme.root, + + // Is default theme still installed + that.themeDefault? that.themeDefault.root : null + ]) + .compact() + .uniq() + .value(); + + that.env = new nunjucks.Environment(new nunjucks.FileSystemLoader(searchPaths)); + }); +}; + +// Write a page (parsable file) +WebsiteOutput.prototype.onPage = function(page) { + var that = this; + + // Render the page template with the same context as the json output + return this.render('page', this.getPageContext(page)) + + // Write the HTML file + .then(function(html) { + return that.writeFile( + page.withExtension('.html'), + html + ); + }); +}; + +// ----- Utilities ---- + +// Render a template using nunjucks +// Templates are stored in `_layouts` folders +WebsiteOutput.prototype.render = function(tpl, context) { + return Promise.nfcall(this.env.render.bind(this.env), this.templateName(tpl), context); +}; + +// Return a complete name for a template +WebsiteOutput.prototype.templateName = function(name) { + return path.join('_layouts', this.name, name+'.html'); +}; + +module.exports = conrefsLoader(WebsiteOutput); diff --git a/lib/output/website/index.js b/lib/output/website/index.js deleted file mode 100644 index 6166ccc..0000000 --- a/lib/output/website/index.js +++ /dev/null @@ -1,14 +0,0 @@ -var conrefsLoader = require('../conrefs'); - -var Theme = require('./theme'); - -var WebsiteOutput = conrefsLoader(); - -WebsiteOutput.prototype.name = 'website'; - -// Write a page (parsable file) -WebsiteOutput.prototype.onPage = function(page) { - -}; - -module.exports = WebsiteOutput; diff --git a/lib/output/website/theme.js b/lib/output/website/theme.js deleted file mode 100644 index 1cc2891..0000000 --- a/lib/output/website/theme.js +++ /dev/null @@ -1,6 +0,0 @@ - -function Theme() { - -} - -module.exports = Theme; |