-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathetic.js
executable file
·57 lines (56 loc) · 1.32 KB
/
etic.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
exports.etic = function (str, data, css){
var cssList = '',
cssDel = '<link rel="stylesheet" href="/$.css">'
css.map(function(v, i) {
cssList += cssDel.replace(/\$/,v);
});
str = str.replace(/<%#([^<]*)#%>/g,function(){
var strTmp = libFs.readFileSync('./www/view/' + arguments[1].trim(), "utf8");
return strTmp;
}).replace(/<\/head>/,cssList + '</head>');
var i = 0,
jsStack = [],
jsLink = '<script>';
str = str.replace(/<%[^<]*(fml.use\(\'[\s\S]*'\))[^<]*%>/g,function(){
jsStack[i++] = arguments[1];
return '';
});
jsStack.map(function(v, i){
jsLink += v;
jsLink += ';';
});
jsLink += '</script>';
var cache = {};
if (!cache[str] ){
var tpl = str
tpl = tpl
.replace(/[\r\t\n]/g, " ")
.split("<\%").join("\t")
.replace(/((^|\%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)\%>/g, "',$1,'")
.split("\t").join("');")
.split("\%>").join("p.push('")
.split("\r").join("\\'")
try{
cache[str] = new Function("",
"var p=[];p.push('" + tpl + "');return p.join('');");
}catch(e){
if (console){
console.log(e)
console.log(tpl)
}
}
}
var fn = cache[str]
if (data){
try{
return fn.apply( data ).replace(/<\/body>/g,jsLink + '</body>')
}catch(e){
if (console){
console.log(e)
console.log(data)
}
}
}else
return fn().replace(/<\/body>/g,jsLink + '</body>')
};