Load template engine for website output using theme configuration

This commit is contained in:
Samy Pessé
2016-02-17 16:20:45 +01:00
parent 5fdee2cb5e
commit 4e7cb9b23f
11 changed files with 113 additions and 34 deletions
-1
View File
@@ -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(),
+3 -5
View File
@@ -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
+1 -1
View File
@@ -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) {
+2 -2
View File
@@ -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);
+7 -2
View File
@@ -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);
+1 -1
View File
@@ -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
-1
View File
@@ -1,4 +1,3 @@
var _ = require('lodash');
var conrefsLoader = require('./conrefs');
var JSONOutput = conrefsLoader();
+98
View File
@@ -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);
-14
View File
@@ -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;
-6
View File
@@ -1,6 +0,0 @@
function Theme() {
}
module.exports = Theme;
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "gitbook",
"version": "2.6.7",
"version": "3.0.0-pre.0",
"homepage": "https://www.gitbook.com",
"description": "Library and cmd utility to generate GitBooks",
"main": "lib/index.js",