forked from leizongmin/express-liquid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
279 lines (248 loc) · 7.42 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
'use strict';
/*!
* Express-Liquid
*
* @param Lei Zongmin<leizongmin@gmail.com>
*/
var tinyliquid = require('tinyliquid');
var fs = require('fs');
var path = require('path');
/**
* TinyLiquid engine for the Express 3.x
*
* options
* includeFile: read file handler, format: function (filename, callback, enableCache)
* callback format: callback(err, content)
* resolveFilename: resolve the absolute file name, format: function (name, settings),
return: absolute file name
* context: base context object
* customTags: custom tags parser
*
* Notes:
* {% include "filename" %} the "filename" default to under the "views" path
*
* Layout:
* Not support layout, please use the "include" tag. Example:
* {% include "header" %}
* This is the body.
* {% include "footer" %}
*
* @param {Object} options
* @return {Function}
* @api public
*/
module.exports = exports = function (options) {
options = options || {};
var baseContext = options.context || new tinyliquid.Context();
var customTags = options.customTags || {};
var cache = {};
/**
* Get cache
*
* @param {String} filename
* @return {Function}
* @api public
*/
var getCache = function (filename) {
filename = resolveFilename(filename);
var ast = cache[filename];
if (typeof(ast) !== 'undefined') {
return ast;
} else {
return null;
}
};
/**
* Set cache
*
* @param {String} filename
* @param {Array} ast
* @param {Buffer} lines
* @return {Object}
* @api public
*/
var setCache = function (filename, ast, lines) {
var ret = cache[filename] = {ast: ast, lines: lines};
return ret;
};
/**
* Clear cache
*
* @param {String} filename
* @return {Function}
* @api public
*/
var clearCache = function (filename) {
filename = resolveFilename(filename);
var fn = cache[filename];
delete cache[filename];
return fn;
};
/**
* Resolve filename
*
* @param {String} name
* @param {Object} settings
* @return {String}
*/
var resolveFilename = function (name, settings) {
if (settings) {
var extname = settings['view engine'] || 'liquid';
var filename = path.resolve(settings.views, name);
if (!path.extname(filename)) filename += '.' + extname;
return filename;
} else {
return name;
}
};
if (typeof(options.resolveFilename) === 'function') resolveFilename = options.resolveFilename;
/**
* Read file
*
* @param {String} filename
* @param {Function} callback
* @param {Boolean} enableCache
*/
var readFile = function (filename, callback, enableCache) {
fs.readFile(filename, function (err, data) {
// watch file, if the file has changed, clear the cache
if (enableCache && !err) {
var w = fs.watch(filename, function (event) {
clearCache(filename);
w.close();
});
}
callback(err, data);
});
};
if (typeof(options.includeFile) === 'function') readFile = options.includeFile;
/**
* Compile template file
*
* @param {String} filename
* @param {Object} settings
* @param {Function} callback format: function (err, ast, filename, lines)
* @api private
*/
var compileFile = function (filename, settings, callback) {
var enableCache = !!settings.__express_liquid_cache;
// check cache
var tpl = getCache(filename);
if (enableCache && tpl !== null) return callback(null, tpl.ast, filename, tpl.lines);
// read file and parse
readFile(filename, function (err, text) {
if (err) return callback(err);
text = text.toString();
var ast = tinyliquid.parse(text, {customTags: customTags});
var lines = text.split(/\n/);
// save cache
if (enableCache) setCache(filename, ast, lines);
// current filename
ast = tinyliquid.insertFilename(ast, filename);
callback(null, ast, filename, lines);
}, enableCache);
};
// onclude handler
baseContext.onInclude(function (filename, callback) {
var parentFilename = this.getFilename();
// if the filename is start with "./" or "../", it means this is a relative file path
if (parentFilename && (filename.substr(0, 2) === './' || filename.substr(0, 3) === '../')) {
filename = resolveFilename(path.join(parentFilename, '../' + filename), this._express_settings);
} else {
filename = resolveFilename(filename, this._express_settings);
}
compileFile(filename, this._express_settings, callback);
});
/**
* Render template
*
* @param {Object} tpl
* @param {Object} context
* @param {Function} callback
*/
var render = function (tpl, context, callback) {
tinyliquid.run(tpl.ast, context, function (err) {
if (err) {
renderErrorPage(err, tpl, context, callback);
} else {
callback(null, context.clearBuffer());
}
});
};
// Friendly error page render
var errorPageRender = tinyliquid.compile(fs.readFileSync(path.resolve(__dirname, 'error_page.liquid'), 'utf8'));
/**
* Render friendly error page
*
* @param {Object} err
* @param {Object} tpl
* @param {Object} context
* @param {Function} callback
*/
var renderErrorPage = function (err, tpl, context, callback) {
var pos = context.getCurrentPosition();
var lines = [];
var length = tpl.lines.length;
var showCodeLine = function (line, highlight) {
var index = line - 1;
if (index >= 0 && index < length) {
var lineNum = line + '| ';
var text = lineNum + tpl.lines[index].trimRight();
lines.push({text: text, highlight: highlight});
return lineNum.length;
} else {
return 0;
}
};
showCodeLine(pos.line - 3);
showCodeLine(pos.line - 2);
showCodeLine(pos.line - 1);
var prefixLength = showCodeLine(pos.line, true);
var marks = '';
for (var i = 0, e = prefixLength + pos.column - 1; i < e; i++) marks += ' ';
marks += '^';
lines.push({text: marks, highlight: true});
showCodeLine(pos.line + 1);
showCodeLine(pos.line + 2);
showCodeLine(pos.line + 3);
var c = tinyliquid.newContext();
var stack = err instanceof Error ? err.stack.split(/\n/) : [err];
c.setLocals('lines', lines);
c.setLocals('error', stack[0]);
c.setLocals('stack', stack.slice(1).join('\n'));
errorPageRender(c, callback);
};
/**
* Return the engine
*
* @param {String} filename
* @param {Object} opts
* - {Object} settings
* - {Boolean} cache
* - {Object} context the tinyliquid.Context instance
* @param {Function} callback
* @api public
*/
var ret = function (filename, opts, callback) {
// init context
var context = opts.context || new tinyliquid.Context();
context.from(baseContext);
context._express_settings = opts.settings;
if (opts.cache) opts.settings.__express_liquid_cache = true;
// compile file and render
filename = resolveFilename(filename, opts.settings);
compileFile(filename, opts.settings, function (err, ast, filename, lines) {
if (err) return callback(err);
render({ast: ast, lines: lines}, context, callback);
});
};
// Cache operation
ret.cache = cache;
ret.getCache = getCache;
ret.clearCache = clearCache;
return ret;
};
// The tinyliquid
exports.tinyliquid = tinyliquid;
// Create a new context
exports.newContext = tinyliquid.newContext;