-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtemplate.js
More file actions
83 lines (73 loc) · 2.19 KB
/
template.js
File metadata and controls
83 lines (73 loc) · 2.19 KB
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
const path = require('path');
const markdown = require('markdown').markdown;
const nunjucks = require('nunjucks');
const util = require('util');
const dataurl = require('dataurl');
exports.buildEnvironment = function buildEnvironment(options) {
var themeDir = options.themeDir;
var staticMiddleware = options.staticMiddleware;
var paths = [__dirname + '/views'];
if (themeDir) paths.splice(0, 0, path.join(themeDir, 'views'));
var loaders = paths.map(function(path) {
return new nunjucks.FileSystemLoader(path);
});
var env = new nunjucks.Environment(loaders);
env.express = function(app) {
nunjucks.Environment.prototype.express.apply(this, arguments);
if (themeDir) {
var staticDir = path.join(themeDir, 'public');
app.use(staticMiddleware(staticDir));
}
app.use(staticMiddleware(path.join(__dirname, 'public')));
};
env.addFilter('undef', function (thing) {
return thing || '';
});
env.addFilter('activize', function (actual, expect) {
if (expect === actual)
return 'class="active"';
return '';
});
env.addFilter('markdown', function (string) {
return markdown.toHTML(string);
});
env.addFilter('imageForBadge', function (badge) {
if (!badge || !badge.relativeUrl)
return '';
return util.format(
'<img src="%s" class="badge-class">',
badge.relativeUrl('image'));
});
env.addFilter('dataurl', function (buffer, type) {
return dataurl.convert({
data: buffer || Buffer(0),
mimetype: type
});
});
env.addFilter('stupidSafe', function (html) {
return (
html
.replace(/</g, '<')
.replace(/>/g, '>')
);
});
env.addFilter('list', function (list, prop, sep) {
return list.map(util.prop(prop)).join(sep);
});
env.addFilter('userHome', function (access) {
return {
super: '/admin',
issuer: '/issuer'
}[access];
});
env.addFilter('dateInput', function (date) {
return date && date.toISOString().split('T')[0] || '';
});
env.addFilter('selected', function (value, expected) {
if ((value === expected) ||
(Array.isArray(value) && ~value.indexOf(expected)))
return 'selected';
return '';
});
return env;
};