-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
143 lines (119 loc) · 3.18 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
'use strict';
var url = require('url');
var path = require('path');
var fs = require('fs');
var nunjucks = require('nunjucks');
var _ = require('lodash');
//
//
//
var debug = false;
var log = function() {
if (debug === true || debug === 'console') {
console.log.apply(console, arguments);
}
};
//
//
function CustomFileLoader(opts) {
this.baseDir = opts.baseDir || '';
this.modulesPath = opts.modules;
this.ext = opts.ext;
this.currentPath = opts.currentPath;
if (this.currentPath === '/') {
this.currentPath = '';
}
if (path.extname(this.currentPath) !== '') {
var pos = this.currentPath.lastIndexOf('/');
this.currentPath = this.currentPath.substring(0, pos);
}
}
//
//
CustomFileLoader.prototype.getSource = function(name) {
var original = name;
var currentPath = path.join(this.baseDir, this.currentPath);
// remove current path from name (if present)
if (name.indexOf(currentPath) === 0) {
name = name.substr(currentPath.length + 1);
}
// distinguish between absolute and relative names
if (name.charAt(0) === '/') {
name = path.join(this.baseDir, name);
} else {
name = path.join(currentPath, name);
}
// add missing extension
var ext = path.extname(name);
if (ext === '') {
name += this.ext;
}
log('get source', original, name);
return {
src: fs.readFileSync(name).toString(),
path: name,
noCache: true
};
};
//
//
module.exports = function(opt) {
opt = opt || {};
debug = opt.debug || false;
if ([true, 'console', 'browser'].indexOf(debug) === -1) {
debug = false;
}
var ext = opt.ext || '.html';
var context = opt.context || {};
var baseDir = opt.baseDir || __dirname;
var currentPath = opt.currentPath || baseDir;
var bsURL = '';
var modules = opt.modulesDir || '';
// allow custom nunjucks filter
var filters = opt.filters || {};
return function(req, res, next) {
var reqPath = url.parse(req.url).pathname;
var file = (reqPath === '/') ? ('/index' + ext) : reqPath;
var pathname = path.join(baseDir, file);
if (path.extname(pathname) === ext && fs.existsSync(pathname)) {
context.query = url.parse(req.url, true).query;
context.filename = pathname;
context.ajax = req.headers['x-requested-with'] === 'XMLHttpRequest';
var env = new nunjucks.Environment(new CustomFileLoader({
baseDir: baseDir,
modules: modules,
ext: ext,
currentPath: reqPath
}), {
watch: false,
// TODO add configurable tags
tags: {
blockStart: '<%',
blockEnd: '%>',
commentStart: '<#',
commentEnd: '#>'
}
});
for (var filterName in filters) {
env.addFilter(filterName, filters[filterName]);
}
env.render(pathname, context, function(err, result) {
if (err) {
log(err);
log(err.stack);
res.writeHead(500);
if (debug === true || debug === 'browser') {
res.write(err.stack);
}
res.end();
return;
}
res.setHeader('Content-Type', 'text/html');
res.write(result);
res.end();
});
} else {
next();
}
};
};