Mercurial > pub > ImplabJs
annotate src/implab/text/template-compile.js @ 25:c2a7d21175ce
Added css injection plugin, implab/dom/css!<css-path>
author | cin |
---|---|
date | Fri, 08 Dec 2017 04:31:43 +0300 |
parents | f750c89976d3 |
children | fb23f3c854df |
rev | line source |
---|---|
0 | 1 define( |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
21
diff
changeset
|
2 ["dojo/request", "./format", "../log/trace!"], |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
21
diff
changeset
|
3 function (request, format, trace) { |
21 | 4 |
5 // разбивает строку шаблона на токены, возвращает контекст для | |
6 // дальнейшей обработки в visitTemplate | |
7 var parseTemplate = function (str) { | |
8 var tokens = str.split(/(<%=|<%|%>)/); | |
9 var pos = -1; | |
10 var data = [], | |
11 code = []; | |
0 | 12 |
21 | 13 return { |
14 next: function () { | |
15 pos++; | |
16 return pos < tokens.length; | |
17 }, | |
18 token: function () { | |
19 return tokens[pos]; | |
20 }, | |
21 pushData: function () { | |
22 var i = data.length; | |
23 data.push.apply(data, arguments); | |
24 return i; | |
25 }, | |
26 pushCode : function() { | |
27 var i = code.length; | |
28 code.push.apply(code, arguments); | |
29 return i; | |
30 }, | |
31 compile: function () { | |
32 var text = "var $p = [];\n" + | |
33 "var print = function(){\n" + | |
34 " $p.push(format.apply(null,arguments));\n" + | |
35 "};\n" + | |
36 // Introduce the data as local variables using with(){} | |
37 "with(obj){\n" + | |
38 code.join("\n") + | |
39 "}\n" + | |
40 "return $p.join('');"; | |
41 | |
42 try { | |
43 var compiled = new Function("obj, format, $data", text); | |
44 /** | |
45 * Функция форматирования по шаблону | |
46 * | |
47 * @type{Function} | |
48 * @param{Object} obj объект с параметрами для подстановки | |
49 */ | |
50 return function (obj) { | |
51 return compiled(obj || {}, format, data); | |
52 }; | |
53 } catch (e) { | |
24
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
21
diff
changeset
|
54 trace.error([e]); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
21
diff
changeset
|
55 trace.log([text, data]); |
f750c89976d3
Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents:
21
diff
changeset
|
56 throw e; |
21 | 57 } |
0 | 58 } |
59 } | |
60 }; | |
61 | |
21 | 62 function visitTemplate(context) { |
63 while (context.next()) { | |
64 switch (context.token()) { | |
65 case "<%": | |
66 visitCode(context); | |
67 break; | |
68 case "<%=": | |
69 visitInline(context); | |
70 break; | |
71 default: | |
72 visitTextFragment(context); | |
73 break; | |
74 } | |
75 } | |
76 } | |
77 | |
78 function visitInline(context) { | |
79 var code = ["$p.push("]; | |
80 while (context.next()) { | |
81 if (context.token() == "%>") | |
82 break; | |
83 code.push(context.token()); | |
84 } | |
85 code.push(");"); | |
86 context.pushCode(code.join('')); | |
87 } | |
88 | |
89 function visitCode(context) { | |
90 var code = []; | |
91 while (context.next()) { | |
92 if (context.token() == "%>") | |
93 break; | |
94 code.push(context.token()); | |
95 } | |
96 context.pushCode(code.join('')); | |
97 } | |
98 | |
99 function visitTextFragment(context) { | |
100 var i = context.pushData(context.token()); | |
101 context.pushCode("$p.push($data["+i+"]);"); | |
102 } | |
103 | |
104 var compile = function (str) { | |
105 if (!str) | |
106 return function() { return "";}; | |
107 | |
108 var ctx = parseTemplate(str); | |
109 visitTemplate(ctx); | |
110 return ctx.compile(); | |
111 }; | |
112 | |
0 | 113 var cache = {}; |
114 | |
21 | 115 compile.load = function (id, require, callback) { |
0 | 116 var url = require.toUrl(id); |
117 if (url in cache) { | |
118 callback(cache[url]); | |
119 } else { | |
21 | 120 request(url).then(compile).then(function (tc) { |
0 | 121 callback(cache[url] = tc); |
21 | 122 }, function (err) { |
123 require.signal("error", [{ | |
124 error: err, | |
125 src: 'implab/text/template-compile' | |
126 }]); | |
0 | 127 }); |
128 } | |
129 }; | |
130 | |
131 return compile; | |
132 }); |