summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-04-25 15:36:18 +0200
committerSamy Pessé <samypesse@gmail.com>2016-04-25 15:36:18 +0200
commit0c2dc06cb721a437480d5a10611511b8f84b00a8 (patch)
treefd944c625c0eebd1f5f39770d58abe8aa8b5d447 /lib
parent4552ccd7d76a1bf958481e24a695dd3b68e6c232 (diff)
downloadgitbook-0c2dc06cb721a437480d5a10611511b8f84b00a8.zip
gitbook-0c2dc06cb721a437480d5a10611511b8f84b00a8.tar.gz
gitbook-0c2dc06cb721a437480d5a10611511b8f84b00a8.tar.bz2
Fix resolution of theme's templates
Diffstat (limited to 'lib')
-rw-r--r--lib/constants/templatesFolder.js2
-rw-r--r--lib/output/index.js1
-rw-r--r--lib/output/website/createTemplateEngine.js17
-rw-r--r--lib/output/website/index.js2
-rw-r--r--lib/output/website/onPage.js4
-rw-r--r--lib/output/website/options.js5
-rw-r--r--lib/templating/render.js2
-rw-r--r--lib/templating/renderFile.js37
8 files changed, 52 insertions, 18 deletions
diff --git a/lib/constants/templatesFolder.js b/lib/constants/templatesFolder.js
new file mode 100644
index 0000000..aad6a72
--- /dev/null
+++ b/lib/constants/templatesFolder.js
@@ -0,0 +1,2 @@
+
+module.exports = '_layouts';
diff --git a/lib/output/index.js b/lib/output/index.js
index d353490..9112666 100644
--- a/lib/output/index.js
+++ b/lib/output/index.js
@@ -1,5 +1,6 @@
module.exports = {
generate: require('./generateBook'),
JSONGenerator: require('./json'),
+ WebsiteGenerator: require('./website'),
EbookGenerator: require('./ebook')
};
diff --git a/lib/output/website/createTemplateEngine.js b/lib/output/website/createTemplateEngine.js
index 1a70667..b90c3ad 100644
--- a/lib/output/website/createTemplateEngine.js
+++ b/lib/output/website/createTemplateEngine.js
@@ -1,8 +1,19 @@
+var path = require('path');
+
+var TEMPLATES_FOLDER = require('../../constants/templatesFolder');
+
var Templating = require('../../templating');
var TemplateEngine = require('../../models/templateEngine');
var listSearchPaths = require('./listSearchPaths');
/**
+ Directory for a theme with the templates
+*/
+function templateFolder(dir) {
+ return path.join(dir, TEMPLATES_FOLDER);
+}
+
+/**
Create templating engine to render themes
@param {Output}
@@ -10,7 +21,11 @@ var listSearchPaths = require('./listSearchPaths');
*/
function createTemplateEngine(output) {
var searchPaths = listSearchPaths(output);
- var loader = new Templating.ThemesLoader(searchPaths);
+
+ // Search paths for templates
+ var tplSearchPaths = searchPaths.map(templateFolder);
+
+ var loader = new Templating.ThemesLoader(tplSearchPaths);
return new TemplateEngine({
loader: loader
diff --git a/lib/output/website/index.js b/lib/output/website/index.js
index e24c127..bb01ed6 100644
--- a/lib/output/website/index.js
+++ b/lib/output/website/index.js
@@ -1,6 +1,6 @@
module.exports = {
- name: 'json',
+ name: 'website',
Options: require('./options'),
onPage: require('./onPage')
};
diff --git a/lib/output/website/onPage.js b/lib/output/website/onPage.js
index 47274b3..b6856b3 100644
--- a/lib/output/website/onPage.js
+++ b/lib/output/website/onPage.js
@@ -12,6 +12,8 @@ var createTemplateEngine = require('./createTemplateEngine');
@param {Page} page
*/
function onPage(output, page) {
+ var options = output.getOptions();
+ var prefix = options.get('prefix');
var engine = createTemplateEngine(output);
return Modifiers.modifyHTML(page, getModifiers(output, page))
@@ -20,7 +22,7 @@ function onPage(output, page) {
var context = JSONUtils.encodeBookWithPage(output.getBook(), resultPage);
// Render the theme
- return Templating.renderFile(engine, 'website/page.html', context)
+ return Templating.renderFile(engine, prefix + '/page.html', context)
// Write it to the disk
.then(function(html) {
diff --git a/lib/output/website/options.js b/lib/output/website/options.js
index 79167b1..eca8a8d 100644
--- a/lib/output/website/options.js
+++ b/lib/output/website/options.js
@@ -2,7 +2,10 @@ var Immutable = require('immutable');
var Options = Immutable.Record({
// Root folder for the output
- root: String()
+ root: String(),
+
+ // Prefix for generation
+ prefix: String('website')
});
module.exports = Options;
diff --git a/lib/templating/render.js b/lib/templating/render.js
index 87d5096..bf21cfe 100644
--- a/lib/templating/render.js
+++ b/lib/templating/render.js
@@ -8,7 +8,7 @@ var replaceShortcuts = require('./replaceShortcuts');
@param {TemplateEngine} engine
@param {String} filePath
@param {String} content
- @param {Object} ctx
+ @param {Object} context
@return {Promise<String>}
*/
function renderTemplate(engine, filePath, content, context) {
diff --git a/lib/templating/renderFile.js b/lib/templating/renderFile.js
index 13a9c5b..9b74e5b 100644
--- a/lib/templating/renderFile.js
+++ b/lib/templating/renderFile.js
@@ -1,27 +1,38 @@
var Promise = require('../utils/promise');
-
-var replaceShortcuts = require('./replaceShortcuts');
+var error = require('../utils/error');
+var render = require('./render');
/**
Render a template
@param {TemplateEngine} engine
@param {String} filePath
- @param {String} content
- @param {Object} ctx
+ @param {Object} context
@return {Promise<String>}
*/
-function renderTemplateFile(engine, filePath, content, context) {
- context = context || {};
- var env = engine.toNunjucks();
+function renderTemplateFile(engine, filePath, context) {
+ var loader = engine.getLoader();
+
+ return Promise()
+ .then(function() {
+ if (!loader.async) {
+ return loader.getSource(filePath);
+ }
+
+ var deferred = Promise.defer();
+ loader.getSource(filePath, deferred.makeNodeResolver());
+ return deferred.promise;
+ })
+ .then(function(result) {
+ if (!result) {
+ throw error.TemplateError(new Error('Not found'), {
+ filename: filePath
+ });
+ }
- content = replaceShortcuts(engine, filePath, content);
+ return render(engine, result.path, result.src, context);
+ });
- return Promise.nfcall(
- env.render.bind(env),
- content,
- context
- );
}
module.exports = renderTemplateFile;