-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtemplates.js
More file actions
154 lines (128 loc) · 3.13 KB
/
Copy pathtemplates.js
File metadata and controls
154 lines (128 loc) · 3.13 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
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
// Total.js Templates
// The MIT License
// Copyright 2023 (c) Peter Širka <petersirka@gmail.com>
exports.render = function(body, model, $) {
return new Promise(function(resolve, reject) {
let cache = F.temporary.templates;
let id = 'Ttemplate' + HASH(body) + ($ ? $.language : '');
let data = cache[id];
if (data) {
try {
resolve(data.template({ value: model || data.model }, null, data.helpers));
} catch (e) {
if ($ && $.invalid)
$.invalid(e);
else
reject(e);
}
return;
}
if (body.indexOf('{{') === -1) {
// URL address or filename
data = cache[id];
let start = body.substring(0, 7);
// http://
// https:/
if (start === 'http://' || start === 'https:/') {
// download template
let opt = {};
opt.url = body;
opt.method = 'GET';
opt.callback = function(err, response) {
if (err) {
if ($ && $.invalid)
$.invalid(err);
else
reject(err);
return;
}
data = parse(response.body, $);
cache[id] = data;
try {
resolve(data.template({ value: model || data.model }, null, data.helpers));
} catch (e) {
if ($ && $.invalid)
$.invalid(e);
else
reject(e);
}
};
REQUEST(opt);
return;
} else {
if (body[0] === '~') {
// absolute path
body = body.substring(1);
} else if (body[0] === '#') {
// plugins
body = PATH.plugins(body.substring(1));
if (!body.includes('.html'))
body += '.html';
} else {
body = F.path.templates(body);
if (!body.includes('.html'))
body += '.html';
}
F.Fs.readFile(body, function(err, response) {
if (err) {
if ($ && $.invalid)
$.invalid(err);
else
reject(err);
return;
}
data = parse(response.toString('utf8'), $);
if (!DEBUG)
cache[id] = data;
try {
resolve(data.template({ value: model || data.model }, null, data.helpers));
} catch (e) {
if ($ && $.invalid)
$.invalid(e);
else
reject(e);
}
});
return;
}
}
data = cache[id] = parse(body, $);
try {
resolve(data.template({ value: model || data.model }, null, data.helpers));
} catch (e) {
if ($ && $.invalid)
$.invalid(e);
else
reject(e);
}
});
};
function parse(body, $) {
if ($ && $.language != null)
body = F.translate($.language, body);
let helpers = {};
let model = EMPTYOBJECT;
let strhelpers = '';
let beg = body.indexOf('<scr' + 'ipt>');
let end;
// helpers
if (beg !== -1) {
end = body.indexOf('</scr' + 'ipt>', beg + 8);
strhelpers = body.substring(beg + 8, end).trim();
body = body.substring(0, beg) + body.substring(end + 9);
}
// model
beg = body.indexOf('<scr' + 'ipt type="text/json">');
if (beg !== -1) {
end = body.indexOf('</scr' + 'ipt>', beg + 8);
model = body.substring(beg + 25, end).trim().parseJSON(true);
body = body.substring(0, beg) + body.substring(end + 9);
}
if (strhelpers)
new Function('Thelpers', strhelpers)(helpers);
let output = {};
output.helpers = helpers;
output.template = Tangular.compile(body);
output.model = model;
return output;
}