-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
174 lines (135 loc) · 2.99 KB
/
index.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
/*!
* swig
* Copyright(c) 2015 Fangdun Cai
* MIT Licensed
*/
'use strict';
/**
* Module dependences.
*/
const debug = require('debug')('koa:swig');
const mixin = require('utils-merge');
const thenify = require('thenify');
const path = require('path');
const swig = require('swig-templates');
const extname = path.extname;
const resolve = path.resolve;
/**
* Expose `render`, `swig`.
*/
exports = module.exports = renderer;
exports.swig = swig;
/**
* Default render settings.
*/
const defaultSettings = {
autoescape: true,
root: 'views',
cache: 'memory',
ext: 'html',
writeBody: true
/*
locals: {},
filters: {}.
tags: {},
extensions: {}
*/
};
// Generator `renderFile`
const renderFile = thenify(swig.renderFile);
function renderer(settings) {
// merge default settings
var sets = Object.create(defaultSettings);
// merge settings
if (settings) {
mixin(sets, settings);
}
settings = sets;
var root = settings.root;
var locals = settings.locals || {};
var cache = settings.cache;
var defaults = {
autoescape: settings.autoescape,
cache: cache,
locals: locals
};
if (settings.varControls) {
defaults.varControls = settings.varControls;
}
if (settings.tagControls) {
defaults.tagControls = settings.tagControls;
}
if (settings.cmtControls) {
defaults.cmtControls = settings.cmtControls;
}
swig.setDefaults(defaults);
// swig custom filters
setFilters(swig, settings.filters);
// swig custom tags
setTags(swig, settings.tags);
// add extensions for custom tags
setExtensions(swig, settings.extensions);
swig.setLocals = setLocals;
swig.getLocals = getLocals;
return render;
function* render(view, options) {
// default extname
var e = extname(view);
if (!e) {
e = '.' + settings.ext;
view += e;
}
// resolve
view = resolve(root, view);
// merge ctx.state
var opts = this.state || {};
// merge ctx.flash, for `koa-flash` or `koa-connect-flash`
mixin(opts, {
flash: this.flash,
cache: cache
});
// merge settings.locals
mixin(opts, locals);
// merge options
mixin(opts, options || {});
debug('render %s %j', view, opts);
var html = yield renderFile(view, opts);
/* jshint validthis:true */
if (settings.writeBody === true) {
this.body = html;
}
return html;
}
function setLocals(args) {
mixin(locals,args);
}
function getLocals(key) {
return locals[key];
}
}
/**
* Add filters for Swig
*/
function setFilters(swig, filters) {
for (var name in filters) {
swig.setFilter(name, filters[name]);
}
}
/**
* Add tags for Swig
*/
function setTags(swig, tags) {
var name, tag;
for (name in tags) {
tag = tags[name];
swig.setTag(name, tag.parse, tag.compile, tag.ends, tag.blockLevel);
}
}
/**
* Add extensions for Swig
*/
function setExtensions(swig, extensions) {
for (var name in extensions) {
swig.setExtension(name, extensions[name]);
}
}