summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-04-25 11:58:55 +0200
committerSamy Pessé <samypesse@gmail.com>2016-04-25 11:58:55 +0200
commitf6e123f1ed36019a2ec5da1f97b27d22352b689a (patch)
tree326670b5cf1ff0cbdfbcd35d53f12fb858f874b2
parente1e4e7f01177d968e63f0b3a1830eda1adacb56b (diff)
downloadgitbook-f6e123f1ed36019a2ec5da1f97b27d22352b689a.zip
gitbook-f6e123f1ed36019a2ec5da1f97b27d22352b689a.tar.gz
gitbook-f6e123f1ed36019a2ec5da1f97b27d22352b689a.tar.bz2
Add base rendering for template
-rw-r--r--lib/models/plugin.js5
-rw-r--r--lib/models/templateBlock.js20
-rw-r--r--lib/models/templateEngine.js26
-rw-r--r--lib/output/createTemplateEngine.js2
-rw-r--r--lib/output/generatePage.js16
5 files changed, 62 insertions, 7 deletions
diff --git a/lib/models/plugin.js b/lib/models/plugin.js
index 6891d7d..ed7ad82 100644
--- a/lib/models/plugin.js
+++ b/lib/models/plugin.js
@@ -87,12 +87,13 @@ Plugin.prototype.getFilters = function() {
*/
Plugin.prototype.getBlocks = function() {
var blocks = this.getContent().get('blocks');
+ blocks = blocks || Immutable.Map();
return blocks
.map(function(block, blockName) {
- block.name = blockName;
- return new TemplateBlock(block);
+ return TemplateBlock.create(blockName, block);
})
+ .valueSeq()
.toList();
};
diff --git a/lib/models/templateBlock.js b/lib/models/templateBlock.js
index ec7dc7c..cce636e 100644
--- a/lib/models/templateBlock.js
+++ b/lib/models/templateBlock.js
@@ -16,7 +16,7 @@ var TemplateBlock = Immutable.Record({
shortcuts: Immutable.List(),
post: null,
parse: true
-});
+}, 'TemplateBlock');
TemplateBlock.prototype.getName = function() {
return this.get('name');
@@ -251,6 +251,24 @@ TemplateBlock.indexBlockResult = function(blk) {
};
/**
+ Create a template block from a function or an object
+
+ @param {String} blockName
+ @param {Object} block
+ @return {TemplateBlock}
+*/
+TemplateBlock.create = function(blockName, block) {
+ if (is.fn(block)) {
+ block = new Immutable.Map({
+ process: block
+ });
+ }
+
+ block = block.set('name', blockName);
+ return new TemplateBlock(block);
+};
+
+/**
Extract kwargs from an arguments array
@param {Array} args
diff --git a/lib/models/templateEngine.js b/lib/models/templateEngine.js
index 1f93879..aaa70fd 100644
--- a/lib/models/templateEngine.js
+++ b/lib/models/templateEngine.js
@@ -1,6 +1,8 @@
var nunjucks = require('nunjucks');
var Immutable = require('immutable');
+var Promise = require('../utils/promise');
+
var TemplateEngine = Immutable.Record({
// List of {TemplateBlock}
blocks: Immutable.List(),
@@ -16,7 +18,7 @@ var TemplateEngine = Immutable.Record({
// Nunjucks loader
loader: nunjucks.FileSystemLoader('views')
-});
+}, 'TemplateEngine');
TemplateEngine.prototype.getBlocks = function() {
return this.get('blocks');
@@ -103,4 +105,26 @@ TemplateEngine.prototype.bindToContext = function(fn) {
return fn.bind(this.getContext());
};
+/**
+ Render a template
+
+ @param {String} filePath
+ @param {String} content
+ @param {Object} ctx
+ @return {Promise<String>}
+*/
+TemplateEngine.prototype.render = function renderTemplate(filePath, content, context) {
+ context = context || {};
+ var env = this.toNunjucks();
+
+ return Promise.nfcall(
+ env.renderString.bind(env),
+ content,
+ context,
+ {
+ path: filePath
+ }
+ );
+};
+
module.exports = TemplateEngine;
diff --git a/lib/output/createTemplateEngine.js b/lib/output/createTemplateEngine.js
index 27dbeba..fbf39d3 100644
--- a/lib/output/createTemplateEngine.js
+++ b/lib/output/createTemplateEngine.js
@@ -25,7 +25,7 @@ function createTemplateEngine(output) {
.map(function(plugin) {
return plugin.getBlocks();
})
- .flatten();
+ .flatten(1);
// Extend with default
blocks = defaultBlocks.concat(blocks);
diff --git a/lib/output/generatePage.js b/lib/output/generatePage.js
index 6fe0514..072c327 100644
--- a/lib/output/generatePage.js
+++ b/lib/output/generatePage.js
@@ -2,6 +2,7 @@ var Promise = require('../utils/promise');
var error = require('../utils/error');
var Parse = require('../parse');
+var createTemplateEngine = require('./createTemplateEngine');
/**
Prepare and generate HTML for a page
@@ -26,9 +27,20 @@ function generatePage(output, page) {
}
return Promise(resultPage.getContent())
+
+ // Escape code blocks with raw tags
.then(parser.page.prepare)
- .then(parser.page)
- .get('content')
+
+ // Render templating syntax
+ .then(function(content) {
+ var engine = createTemplateEngine(output);
+ return engine.render(filePath, content);
+ })
+
+ // Render page using parser (markdown -> HTML)
+ .then(parser.page).get('content')
+
+ // Return new page
.then(function(content) {
return resultPage.set('content', content);
});