This repository was archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (35 loc) · 1.34 KB
/
Copy pathindex.js
File metadata and controls
45 lines (35 loc) · 1.34 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
var ejs = require('ejs');
var path = require('path');
var createTemplateName = function (basePath, parentPath, filePath) {
var extensionRegex = /(\.[a-z]+)+$/;
var absolutePath = path.join(basePath, parentPath);
var normalizedAbsolutePath = absolutePath.lastIndexOf('/') == absolutePath.length - 1
? absolutePath
: absolutePath + '/';
var templateName = filePath.replace(normalizedAbsolutePath, '')
.replace(extensionRegex, '');
return templateName;
};
var createEjsPreprocessor = function(logger, basePath, ejsOptions) {
var log = logger.create('preprocessor.ejs');
return function(content, file, done) {
var processed = null;
var templateName = createTemplateName(basePath, ejsOptions.parentPath, file.originalPath);
log.debug('Processing "%s".', templateName);
try {
processed = "\
(function() {\
this.JST || (this.JST = {});\
this.JST['" + templateName +
"'] = " + ejs.compile(content, {client: true}) +
"}).call(this);";
} catch (e) {
log.error('%s\n at %s', e.message, content);
}
done(processed);
};
};
createEjsPreprocessor.$inject = ['logger', 'config.basePath', 'config.ejsOptions'];
module.exports = {
'preprocessor:ejs': ['factory', createEjsPreprocessor]
};