This repository has been archived by the owner on Jan 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathengine.js
184 lines (153 loc) Β· 7.42 KB
/
engine.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
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*\
β Copyright (C) 2016 PayPal β
β β
βhh ,'""`. β
β / _ _ \ Licensed under the Apache License, Version 2.0 (the "License"); β
β |(@)(@)| you may not use this file except in compliance with the License. β
β ) __ ( You may obtain a copy of the License at β
β /,'))((`.\ β
β(( (( )) )) http://www.apache.org/licenses/LICENSE-2.0 β
β `\ `)(' /' β
β β
β Unless required by applicable law or agreed to in writing, software β
β distributed under the License is distributed on an "AS IS" BASIS, β
β WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. β
β See the License for the specific language governing permissions and β
β limitations under the License. β
\*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
'use strict';
var path = require('path'),
utils = require('./utils'),
Brook = require('./brook'),
reqwire = require('./reqwire');
var VError = require('verror');
var aproba = require('aproba');
var isAbsolute = require('path-is-absolute');
var fs = require('fs');
var debug = require('debuglog')('adaro');
var freshy = require('freshy').freshy;
exports.create = function (type, config) {
var engine;
config = config || {};
config.cache = (config.cache === undefined) ? true : !!config.cache;
var dust = freshy('dustjs-linkedin', function (dust) {
for (var k in config) {
dust.config[k] = config[k];
}
// Implement our loader
dust.onLoad = function (name, options, cb) {
if (isAbsolute(name)) {
if (options.name && dust.cache[options.name]) {
// We use the dust cache here, but under the requested name, not the resolved one to keep cache keying uniform.
if (!dust.isTemplateFn(dust.cache[options.name])) {
return cb(new VError("cached value for '%s' was not a template function", options.name));
}
return cb(null, dust.cache[options.name]);
}
debug("loading '%s' directly as it is an absolute path", name);
read(null, name, options.name);
} else if (options.view && options.view.lookup && options.view.lookup.length >= 3) {
debug("looking up '%s' using view engine", name);
var file = name;
if (file.slice(-type.length) !== type) {
file += '.' + type;
}
options.view.lookup(file, options.locals, function (err, filename) {
read(err, filename, name);
});
} else {
debug("looking up '%s' using internal methods", name);
var f = name;
if (path.extname(name) !== options.ext) {
f += options.ext;
}
// Fixme: handle multiple directories.
var candidate = path.resolve(options.views, f);
fs.stat(candidate, function (err, stat) {
if (err) {
return cb(err);
}
read(err, candidate, name);
});
}
function read(err, file, name) {
if (err) {
return cb(err);
}
fs.readFile(file, 'utf-8', function (err, data) {
debug("loaded '%s' as '%s'", file, name || file);
if (err) {
return cb(err);
}
if (type === 'js') {
cb(null, dust.loadSource(data));
} else if (type === 'dust') {
try {
cb(null, dust.loadSource(dust.compile(data, name || file)));
} catch (e) {
cb(e);
}
}
});
}
};
// Load referenced helpers
if (Array.isArray(config.helpers)) {
config.helpers.forEach(function (module) {
// allow configuration of helpers like:
// "helpers": ["./lib/abc", { "name": "./lib/test", "arguments": { "debug": true } }]
if (typeof module === 'function') {
return module(dust);
}
if (typeof module === 'string') {
reqwire.init(module, dust);
}
else if (module.hasOwnProperty('name')) {
reqwire.init(module.name, dust, module.arguments);
}
});
}
});
engine = function engine(file, options, callback) {
aproba('SOF', arguments);
if (!this) {
throw new Error("engine must be called with a View as context");
}
if (!file) {
return callback(new Error("no template specified"));
}
var ext = this.ext || path.extname(file);
var name = this.name || file;
var nameNoExt = name.slice(-ext.length) === ext ? name.slice(0, -ext.length) : name;
var renderOptions = {
view: this, // so partials and content can use this object to do lookups
views: this.root, // so that in the absence of an asynchronous lookup view, we can do our own resolution
name: this.name, // so that the initial render and further renders can use the same style of cache keying
ext: ext, // so that partial renders can use the same extension we are configured with,
locals: options // so partials can get the full information passed to render
};
if (options.renderOptions) {
for (var k in options.renderOptions) {
renderOptions[k] = options.renderOptions[k];
}
}
var context = dust.context({}, renderOptions).push(options);
// set the root context's template name.
context.templateName = nameNoExt;
if (config.stream) {
var stream = dust.stream(file, context);
return callback(null, new Brook(stream));
} else {
return dust.render(file, context, function (err, data) {
if (err) {
callback(new VError(err, 'Problem rendering dust template "%s"', file));
} else {
callback(null, data);
}
});
}
};
engine.settings = utils.deepClone(config);
engine.dust = dust;
return engine;
};