-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakeskin.js
200 lines (159 loc) · 4.33 KB
/
snakeskin.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
module.exports = exports = require('./build/snakeskin.min');
var path = require('path');
var fs = require('fs'),
exists = fs.existsSync || path.existsSync;
var cache = {};
function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}
/**
* Вернуть true, если заданный файл шаблонов соответствует скомпилированному
* по временной метке
*
* @param {string} source - путь к исходному файлу
* @param {string} result - путь к скомпилированному файлу
* @param {(string|boolean|null)=} [opt_key] - ключ параметров компиляции
* @return {boolean}
*/
exports.check = function (source, result, opt_key) {
if (!exists(result)) {
return false;
}
var label = fs.statSync(source).mtime,
code = fs.readFileSync(result).toString();
var resLabel = /label <([\d]+)>/.exec(code);
if (!resLabel || opt_key === null) {
return false;
}
if (opt_key) {
var key = /key <(.*?)>/.exec(code);
if (!key || key[1] != opt_key) {
return false;
}
}
return label.valueOf() == resLabel[1];
};
/**
* Скомпилировать заданный файл и вернуть ссылку на полученный объект
* или false, если произошла ошибка при компиляции
*
* @param {string} src - путь к файлу шаблонов
*
* @param {Object=} [opt_params] - дополнительные параметры компиляции
* @see Snakeskin.compile
*
* @return {(!Object|boolean)}
*/
exports.compileFile = function (src, opt_params) {var this$0 = this;
var p = opt_params || {};
p.commonJS = true;
var cacheEnabled = p.cache !== false;
var cacheKey = this.compile(null, p, {cacheKey: true}),
fromCache = cacheEnabled &&
cache[cacheKey] &&
cache[cacheKey][src];
if (fromCache) {
var tmp = fromCache;
if (p.words) {
if (!tmp.words) {
fromCache = false;
} else {
p.words = clone(tmp.words);
}
}
if (p.debug) {
if (!tmp.debug) {
fromCache = false;
} else {
p.debug = clone(tmp.debug);
}
}
if (fromCache) {
return tmp.tpls;
}
}
var source = fs.readFileSync(src).toString(),
resSrc = (("" + src) + ".js");
var tpls,
res = true;
var compile = function() {
res = this$0.compile(source, p, {file: src});
if (res !== false) {
fs.writeFileSync(resSrc, res);
}
};
if (cacheEnabled) {
if (!this.check(src, resSrc, cacheKey)) {
compile();
}
} else {
compile();
}
if (res !== false) {
tpls = require(resSrc);
if (cacheKey) {
cache[cacheKey] = cache[cacheKey] || {};
cache[cacheKey][src] = {
tpls: tpls,
debug: p.debug,
words: p.words
};
}
if (tpls.init) {
tpls.init(this);
}
return tpls;
}
return false;
};
/**
* Скомпилировать заданный файл и вернуть ссылку на главный шаблон (функцию)
*
* @param {string} src - путь к файлу шаблонов
*
* @param {Object=} [opt_params] - дополнительные параметры компиляции
* @see Snakeskin.compile
*
* @param {?string=} [opt_tplName] - имя главного шаблона
* @return {Function}
*/
exports.execFile = function (src, opt_params, opt_tplName) {
var tpls = this.compileFile(src, opt_params),
tpl;
if (!tpls) {
return null;
}
if (opt_tplName) {
tpl = tpls[opt_tplName];
} else {
tpl = tpls[path.basename(src, path.extname(src))] || tpls.main || tpls[Object.keys(tpls)[0]];
}
return tpl || null;
};
/**
* Скомпилировать заданный текст и вернуть ссылку на главный шаблон (функцию)
*
* @param {string} txt - исходный текст
*
* @param {Object=} [opt_params] - дополнительные параметры компиляции
* @see Snakeskin.compile
*
* @param {?string=} [opt_tplName] - имя главного шаблона
* @return {Function}
*/
exports.exec = function (txt, opt_params, opt_tplName) {
var tpls = {},
tpl;
opt_params = opt_params || {};
opt_params.context = tpls;
this.compile(txt, opt_params);
if (!tpls) {
return null;
}
if (opt_tplName) {
tpl = tpls[opt_tplName];
} else {
tpl = tpls.main || tpls[Object.keys(tpls)[0]];
}
return tpl || null;
};