Add base rendering for template

This commit is contained in:
Samy Pessé
2016-04-25 11:58:55 +02:00
parent e1e4e7f011
commit f6e123f1ed
5 changed files with 62 additions and 7 deletions
+3 -2
View File
@@ -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();
};
+19 -1
View File
@@ -16,7 +16,7 @@ var TemplateBlock = Immutable.Record({
shortcuts: Immutable.List(),
post: null,
parse: true
});
}, 'TemplateBlock');
TemplateBlock.prototype.getName = function() {
return this.get('name');
@@ -250,6 +250,24 @@ TemplateBlock.indexBlockResult = function(blk) {
return key;
};
/**
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
+25 -1
View File
@@ -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;
+1 -1
View File
@@ -25,7 +25,7 @@ function createTemplateEngine(output) {
.map(function(plugin) {
return plugin.getBlocks();
})
.flatten();
.flatten(1);
// Extend with default
blocks = defaultBlocks.concat(blocks);
+14 -2
View File
@@ -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);
});