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
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) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
8 var tokens = str.split(/(<%=|<%|%>)/);
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 "<%":
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
66 visitCode(context);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
67 break;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
68 case "<%=":
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
69 visitInline(context);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
70 break;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
71 default:
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
72 visitTextFragment(context);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
73 break;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
74 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
75 }
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 function visitInline(context) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
79 var code = ["$p.push("];
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
80 while (context.next()) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
81 if (context.token() == "%>")
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
82 break;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
83 code.push(context.token());
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
84 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
85 code.push(");");
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
86 context.pushCode(code.join(''));
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
87 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
88
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
89 function visitCode(context) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
90 var code = [];
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
91 while (context.next()) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
92 if (context.token() == "%>")
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
93 break;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
94 code.push(context.token());
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
95 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
96 context.pushCode(code.join(''));
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
97 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
98
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
99 function visitTextFragment(context) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
100 var i = context.pushData(context.token());
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
101 context.pushCode("$p.push($data["+i+"]);");
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
102 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
103
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
104 var compile = function (str) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
105 if (!str)
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
106 return function() { return "";};
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
107
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
108 var ctx = parseTemplate(str);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
109 visitTemplate(ctx);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
110 return ctx.compile();
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
111 };
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
112
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
113 var cache = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
114
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
115 compile.load = function (id, require, callback) {
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
116 var url = require.toUrl(id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
117 if (url in cache) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
118 callback(cache[url]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
119 } else {
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
120 request(url).then(compile).then(function (tc) {
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
121 callback(cache[url] = tc);
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
122 }, function (err) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
123 require.signal("error", [{
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
124 error: err,
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
125 src: 'implab/text/template-compile'
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
126 }]);
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
127 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
128 }
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 return compile;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
132 });