1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
var nunjucks = require('nunjucks');
var Immutable = require('immutable');
var TemplateEngine = Immutable.Record({
// List of {TemplateBlock}
blocks: Immutable.List(),
// List of Extension
extensions: Immutable.Map(),
// Map of filters: {String} name -> {Function} fn
filters: Immutable.Map(),
// Map of globals: {String} name -> {Mixed}
globals: Immutable.Map(),
// Context for filters / blocks
context: Object(),
// Nunjucks loader
loader: nunjucks.FileSystemLoader('views')
}, 'TemplateEngine');
TemplateEngine.prototype.getBlocks = function() {
return this.get('blocks');
};
TemplateEngine.prototype.getGlobals = function() {
return this.get('globals');
};
TemplateEngine.prototype.getFilters = function() {
return this.get('filters');
};
TemplateEngine.prototype.getShortcuts = function() {
return this.get('shortcuts');
};
TemplateEngine.prototype.getLoader = function() {
return this.get('loader');
};
TemplateEngine.prototype.getContext = function() {
return this.get('context');
};
TemplateEngine.prototype.getExtensions = function() {
return this.get('extensions');
};
/**
Return a block by its name (or undefined)
@param {String} name
@return {TemplateBlock}
*/
TemplateEngine.prototype.getBlock = function(name) {
var blocks = this.getBlocks();
return blocks.find(function(block) {
return block.getName() === name;
});
};
/**
Return a nunjucks environment from this configuration
@return {Nunjucks.Environment}
*/
TemplateEngine.prototype.toNunjucks = function() {
var that = this;
var loader = this.getLoader();
var blocks = this.getBlocks();
var filters = this.getFilters();
var globals = this.getGlobals();
var extensions = this.getExtensions();
var env = new nunjucks.Environment(
loader,
{
// Escaping is done after by the asciidoc/markdown parser
autoescape: false,
// Syntax
tags: {
blockStart: '{%',
blockEnd: '%}',
variableStart: '{{',
variableEnd: '}}',
commentStart: '{###',
commentEnd: '###}'
}
}
);
// Add filters
filters.forEach(function(filterFn, filterName) {
env.addFilter(filterName, that.bindToContext(filterFn));
});
// Add blocks
blocks.forEach(function(block) {
var extName = block.getExtensionName();
var Ext = block.toNunjucksExt();
env.addExtension(extName, new Ext());
});
// Add globals
globals.forEach(function(globalName, globalValue) {
env.addGlobal(globalName, globalValue);
});
// Add other extensions
extensions.forEach(function(ext, extName) {
env.addExtension(extName, ext);
});
return env;
};
/**
Bind a function to the context
@param {Function} fn
@return {Function}
*/
TemplateEngine.prototype.bindToContext = function(fn) {
return fn.bind(this.getContext());
};
/**
Create a template engine
@param {Object} def
@return {TemplateEngine}
*/
TemplateEngine.create = function(def) {
return new TemplateEngine({
blocks: Immutable.List(def.blocks || []),
extensions: Immutable.Map(def.extensions || {}),
filters: Immutable.Map(def.filters || {}),
globals: Immutable.Map(def.globals || {}),
context: def.context,
loader: def.loader
});
};
module.exports = TemplateEngine;
|