/
nunjucks.js
185 lines (144 loc) · 5.21 KB
/
nunjucks.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var glob = require('glob');
var path = require('path');
var fs = require('fs');
var util = require('util');
var _ = require('lodash');
var nunjucks = require('nunjucks');
var hljs = require('highlight.js');
module.exports = function (acetate) {
function customTagError (name, type, e) {
var error = acetate.util.parseException(e);
e.message = util.format('error in custom %s "%s" - %s:%d:%d', type, name, error.path, error.lineNo, error.colNo);
return e;
}
var Loader = nunjucks.Loader.extend({
getSource: function (name) {
acetate.debug('nunjucks', 'searching for templates named %s', name);
var matches = glob.sync(name + '.+(html|md|markdown)', {
cwd: path.join(acetate.src)
});
acetate.debug('nunjucks', 'found %d template(s) matching %s', matches.length, name);
if (matches && matches[0]) {
var fullpath = path.join(acetate.src, matches[0]);
acetate.debug('nunjucks', 'loading %s', fullpath);
return {
src: fs.readFileSync(fullpath, 'utf-8').split(/^([\s\S]+)^-{3}$/m)[0],
path: fullpath
};
} else {
acetate.error('nunjucks', 'could not find a template named %s', name);
return null;
}
}
});
var environment = new nunjucks.Environment(new Loader());
function helper (name, fn) {
function CustomTag () {
this.tags = [name];
this.parse = function (parser, nodes) {
// get the tag token
var token = parser.nextToken();
var args;
// parse the args and move after the block end. passing true
// as the second arg is required if there are no parentheses
args = parser.parseSignature(null, true);
// You must include this if there are no args to prevent errors
if (args.children.length === 0) {
args.addChild(new nodes.Literal(0, 0, ''));
}
parser.advanceAfterBlockEnd(token.value);
// See above for notes about CallExtension
return new nodes.CallExtension(this, 'run', args);
};
this.run = function () {
var args = Array.prototype.slice.call(arguments);
args[0] = args[0].ctx;
var result;
try {
result = fn.apply(this, args);
} catch (e) {
throw customTagError(name, 'helper', e);
}
return result;
};
}
environment.addExtension(name, new CustomTag());
}
function block (name, fn) {
function CustomBlock () {
this.tags = [name];
this.parse = function (parser, nodes) {
// get the tag token
var token = parser.nextToken();
// parse the args and move after the block end. passing true
// as the second arg is required if there are no parentheses
var args = parser.parseSignature(null, true);
parser.advanceAfterBlockEnd(token.value);
// parse the body
var body = parser.parseUntilBlocks('end' + name);
parser.advanceAfterBlockEnd();
// See above for notes about CallExtension
return new nodes.CallExtension(this, 'run', args, [body]);
};
this.run = function () {
var args = Array.prototype.slice.call(arguments);
var context = args.shift().ctx;
var body = (args.pop())();
var params = [context, body].concat(args); // body, context, args...
var result;
try {
result = fn.apply(this, params);
} catch (e) {
throw customTagError(name, 'block', e);
}
return result;
};
}
environment.addExtension(name, new CustomBlock());
}
function filter (name, fn) {
var wrapped = _.bind(function (value) {
var result;
try {
result = fn(value);
} catch (e) {
throw customTagError(name, 'filter', e);
}
return result;
}, this);
environment.addFilter(name, wrapped);
}
function global (key, value) {
environment.addGlobal(key, value);
}
block('highlight', function (context, body, lang) {
var highlighted = (lang) ? hljs.highlight(lang, body) : hljs.highlightAuto(body);
return new nunjucks.runtime.SafeString('<pre><code class="' + highlighted.language + '">' + highlighted.value.trim() + '</code></pre>');
});
block('markdown', function (context, body) {
return new nunjucks.runtime.SafeString(acetate.markdown.render(body));
});
helper('debug', function (context, variable) {
var output;
var template;
if (variable) {
acetate.info('debugger', '%s: %s is %s', context.src, variable, context[variable]);
template = '<script>if(console && console.info) {console.info("[Acetate] {{variable}}: ", "`"+({{context | safe}})["{{variable}}"]+"`");}</script>';
} else {
acetate.info('debugger', '%s: %s', context.src, JSON.stringify(context, null, 2));
template = '<script>if(console && console.info) {console.info("[Acetate] metadata: ", ({{context | safe}}));}</script>';
}
output = acetate.nunjucks.renderString(template, {
variable: variable,
context: JSON.stringify(context)
});
return new nunjucks.runtime.SafeString(output);
});
return {
nunjucks: environment,
helper: helper,
block: block,
filter: filter,
global: global
};
};