annotate src/implab/text/template-compile.js @ 21:7c22fc01fcec

rewritten ./text/template-compile
author cin
date Thu, 30 Nov 2017 11:01:22 +0300
parents 23be39fd3851
children f750c89976d3
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(
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
2 ["dojo/request", "./format"],
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
3 function (request, format) {
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) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
54 if (console && console.error) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
55 console.error(text);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
56 console.log(data);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
57 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
58 }
0
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 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
63 function visitTemplate(context) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
64 while (context.next()) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
65 switch (context.token()) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
66 case "<%":
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 "<%=":
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
70 visitInline(context);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
71 break;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
72 default:
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
73 visitTextFragment(context);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
74 break;
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
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
79 function visitInline(context) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
80 var code = ["$p.push("];
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
81 while (context.next()) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
82 if (context.token() == "%>")
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
83 break;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
84 code.push(context.token());
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
85 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
86 code.push(");");
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
87 context.pushCode(code.join(''));
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
88 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
89
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
90 function visitCode(context) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
91 var code = [];
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
92 while (context.next()) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
93 if (context.token() == "%>")
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
94 break;
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
95 code.push(context.token());
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
96 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
97 context.pushCode(code.join(''));
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
98 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
99
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
100 function visitTextFragment(context) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
101 var i = context.pushData(context.token());
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
102 context.pushCode("$p.push($data["+i+"]);");
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
103 }
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
104
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
105 var compile = function (str) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
106 if (!str)
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
107 return function() { return "";};
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
108
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
109 var ctx = parseTemplate(str);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
110 visitTemplate(ctx);
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
111 return ctx.compile();
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
112 };
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
113
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
114 var cache = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
115
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
116 compile.load = function (id, require, callback) {
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
117 var url = require.toUrl(id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
118 if (url in cache) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
119 callback(cache[url]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
120 } else {
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
121 request(url).then(compile).then(function (tc) {
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
122 callback(cache[url] = tc);
21
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
123 }, function (err) {
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
124 require.signal("error", [{
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
125 error: err,
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
126 src: 'implab/text/template-compile'
7c22fc01fcec rewritten ./text/template-compile
cin
parents: 12
diff changeset
127 }]);
0
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
132 return compile;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
133 });