diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cli/helper.js | 1 | ||||
-rw-r--r-- | lib/config/default.js | 8 | ||||
-rw-r--r-- | lib/config/plugins.js | 2 | ||||
-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 |
10 files changed, 112 insertions, 33 deletions
diff --git a/lib/cli/helper.js b/lib/cli/helper.js index 61b9957..e8a049f 100644 --- a/lib/cli/helper.js +++ b/lib/cli/helper.js @@ -36,7 +36,6 @@ var FORMATS = { // the root of the book is the first argument (or current directory) function bookCmd(fn) { return function(args, kwargs) { - var input = args[0] || process.cwd(); var book = new Book({ fs: new NodeFS(), diff --git a/lib/config/default.js b/lib/config/default.js index a35f1a5..1695e11 100644 --- a/lib/config/default.js +++ b/lib/config/default.js @@ -20,6 +20,9 @@ module.exports = { 'summary': 'SUMMARY.md' }, + // Theme for output + 'theme': 'default', + // CSS Styles 'styles': { 'website': 'styles/website.css', @@ -39,11 +42,6 @@ module.exports = { // Variables for templating 'variables': {}, - // Set another theme with your own layout - // It's recommended to use plugins or add more options for default theme, though - // See https://github.com/GitbookIO/gitbook/issues/209 - 'theme': path.resolve(__dirname, '../theme'), - // Links in template (null: default, false: remove, string: new value) 'links': { // Custom links at top of sidebar diff --git a/lib/config/plugins.js b/lib/config/plugins.js index 43eafdf..c75cd55 100644 --- a/lib/config/plugins.js +++ b/lib/config/plugins.js @@ -1,7 +1,7 @@ var _ = require('lodash'); // Default plugins added to each books -var DEFAULT_PLUGINS = ['highlight', 'search', 'sharing', 'fontsettings']; +var DEFAULT_PLUGINS = ['highlight', 'search', 'sharing', 'fontsettings', 'theme-default']; // Return true if a plugin is a default plugin function isDefaultPlugin(name, version) { 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; |