annotate core/src/js/text/template-compile.js @ 30:2dfba21cd879

sync
author cin
date Wed, 27 Jun 2018 02:46:14 +0300
parents acdcdf1a8d21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
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
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
4
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
5 // разбивает строку шаблона на токены, возвращает контекст для
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
6 // дальнейшей обработки в visitTemplate
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
7 var parseTemplate = function (str) {
26
fb23f3c854df added new format for tamplates
nickolay
parents: 24
diff changeset
8 var tokens = str.split(/(<%=|\[%=|<%|\[%|%\]|%>)/);
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
9 var pos = -1;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
10 var data = [],
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
11 code = [];
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
13 return {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
14 next: function () {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
15 pos++;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
16 return pos < tokens.length;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
17 },
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
18 token: function () {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
19 return tokens[pos];
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
20 },
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
21 pushData: function () {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
22 var i = data.length;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
23 data.push.apply(data, arguments);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
24 return i;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
25 },
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
26 pushCode : function() {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
27 var i = code.length;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
28 code.push.apply(code, arguments);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
29 return i;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
30 },
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
31 compile: function () {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
32 var text = "var $p = [];\n" +
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
33 "var print = function(){\n" +
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
34 " $p.push(format.apply(null,arguments));\n" +
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
35 "};\n" +
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
36 // Introduce the data as local variables using with(){}
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
37 "with(obj){\n" +
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
38 code.join("\n") +
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
39 "}\n" +
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
40 "return $p.join('');";
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
41
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
42 try {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
43 var compiled = new Function("obj, format, $data", text);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
44 /**
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
45 * Функция форматирования по шаблону
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
46 *
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
47 * @type{Function}
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
48 * @param{Object} obj объект с параметрами для подстановки
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
49 */
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
50 return function (obj) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
51 return compiled(obj || {}, format, data);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
52 };
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
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
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
57 }
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
60 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
62 function visitTemplate(context) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
63 while (context.next()) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
64 switch (context.token()) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
65 case "<%":
26
fb23f3c854df added new format for tamplates
nickolay
parents: 24
diff changeset
66 case "[%":
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
67 visitCode(context);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
68 break;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
69 case "<%=":
26
fb23f3c854df added new format for tamplates
nickolay
parents: 24
diff changeset
70 case "[%=":
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
71 visitInline(context);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
72 break;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
73 default:
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
74 visitTextFragment(context);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
75 break;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
76 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
77 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
78 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
79
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
80 function visitInline(context) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
81 var code = ["$p.push("];
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
82 while (context.next()) {
26
fb23f3c854df added new format for tamplates
nickolay
parents: 24
diff changeset
83 if (context.token() == "%>" || context.token() == "%]")
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
84 break;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
85 code.push(context.token());
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
86 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
87 code.push(");");
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
88 context.pushCode(code.join(''));
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
89 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
90
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
91 function visitCode(context) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
92 var code = [];
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
93 while (context.next()) {
26
fb23f3c854df added new format for tamplates
nickolay
parents: 24
diff changeset
94 if (context.token() == "%>" || context.token() == "%]")
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
95 break;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
96 code.push(context.token());
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
97 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
98 context.pushCode(code.join(''));
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
99 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
100
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
101 function visitTextFragment(context) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
102 var i = context.pushData(context.token());
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
103 context.pushCode("$p.push($data["+i+"]);");
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
104 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
105
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
106 var compile = function (str) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
107 if (!str)
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
108 return function() { return "";};
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
109
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
110 var ctx = parseTemplate(str);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
111 visitTemplate(ctx);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
112 return ctx.compile();
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
113 };
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
114
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
115 var cache = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
116
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
117 compile.load = function (id, require, callback) {
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
118 var url = require.toUrl(id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
119 if (url in cache) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
120 callback(cache[url]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
121 } else {
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
122 request(url).then(compile).then(function (tc) {
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
123 callback(cache[url] = tc);
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
124 }, function (err) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
125 require.signal("error", [{
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
126 error: err,
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
127 src: 'implab/text/template-compile'
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
128 }]);
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
129 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
130 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
131 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
132
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
133 return compile;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
134 });