-
Notifications
You must be signed in to change notification settings - Fork 0
/
snejok.js
163 lines (142 loc) · 5.21 KB
/
snejok.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var Snejok = (function() {
return function (entry, confs) {
this.entry = entry;
this.data = confs;
this.template = confs.template;
var that = this;
var entryEl = document.querySelector(this.entry);
var view = this.template;
function propsSubst() {
const key = index => Object.keys(that.data)[index];
for (let i = 0; i < Object.keys(that.data).length; i++) {
view = view.replace(new RegExp('\\{\\*' + key(i) + '\\*\\}', 'g'), that.data[key(i)]);
}
}
function forArray() {
var allLoopArr = view.match(/\{\*for [\s\S]*?endfor\*\}/gm);
if (allLoopArr === null) return;
var splitArrays = [];
allLoopArr = allLoopArr.map(function(i) {
return i.replace(['{*endfor*}'], '').replace('{*for', '')
});
for (let i = 0; i < allLoopArr.length; i++) {
var newArr = [];
newArr.push(allLoopArr[i].trim().split(/\*\}$/gm));
splitArrays.push(newArr);
}
for (let i = 0; i < splitArrays.length; i++) {
var curList = splitArrays[i][0][0].replace(/\s*/g,'');
var curHTML = splitArrays[i][0][1];
var dataList = that.data[curList];
var newHTML = '';
for (let j = 0; j < (dataList ? dataList.length : null); j++) {
var currentItemInList = dataList[j];
newHTML += curHTML.replace('{*}', currentItemInList).replace('{$}', j + 1);
}
view = view.replace(
new RegExp('\\{\\*for ' + curList + '[\\s\\S]*?endfor\\*\\}', 'g'),
newHTML
);
}
}
function forObj() {
var splitArrays = [];
var allLoopArr = view.match(/\{\*obj [\s\S]*?endobj\*\}/gm);
if (allLoopArr === null) return;
var removeEnds = allLoopArr.map(function(i) {
return i.replace(['{*endobj*}'], '').replace('{*obj', '');
});
for (let i = 0; i < removeEnds.length; i++) {
var newArr = [];
newArr.push(removeEnds[i].trim().split(/\*\}$/gm));
splitArrays.push(newArr);
}
for (let i = 0; i < splitArrays.length; i++) {
var curList = splitArrays[i][0][0].replace(/\s*/g,'');
var curHTML = splitArrays[i][0][1];
var dataList = that.data[curList];
var newHTML = '';
var pattern = new RegExp(
'\\{\\*obj ' + curList + '[\\s\\S]*?endobj\\*\\}', 'g'
);
for (let j = 0; j < (dataList ? Object.keys(dataList).length : null); j++) {
var currentItemInList = dataList[j];
var val = dataList[Object.keys(dataList)[j]];
var prop = Object.keys(dataList)[j];
newHTML += curHTML
.replace('{*val*}', val)
.replace('{*prop*}', prop)
.replace('{$}', j);
}
view = view.replace(pattern, newHTML);
}
}
function ifElse() {
var splitArrays = [];
var allLoopArr = view.match(/\{\*if [\s\S]*?endif\*\}/gm);
if (allLoopArr === null) return;
allLoopArr = allLoopArr.map(function(i) {
return i.replace('{*endif*}', '').replace('{*if', '');
});
for (let i = 0; i < allLoopArr.length; i++) {
var newArr = [];
newArr.push(allLoopArr[i].trim().split(/\*\}$/gm));
splitArrays.push(newArr);
}
for (let i = 0; i < splitArrays.length; i++) {
var curList = splitArrays[i][0][0].replace(/\s*/g,'');
if (that.data[curList]) {
var delThis = ['{*endif*}', '{*if ', '*}', curList];
for (let i = 0; i < delThis.length; i++) {
view = view.replace(delThis[i], '');
}
} else {
view = view.replace(new RegExp('\\{\\*if ' + curList + '[\\s\\S]*?endif\\*\\}', 'g'), '');
}
}
}
function expression() {
var operators = ['+', '-', '*', '%', '(', ')'];
var splitArrays = [];
var allLoopArr = view.match(/\{\:[\s\S]*?\:\}/gm);
if (allLoopArr === null) return;
var removeEnds = allLoopArr.map(function(i) {
return i.replace(':}', '').replace('{:', '');
});
var firstArr = [];
for (let i = 0; i < removeEnds.length; i++) {
firstArr.push(removeEnds[i]);
for (let j = 0; j < Object.keys(that.data).length; j++) {
var val = Object.keys(that.data)[j];
if (typeof that.data[val] === 'number' ) {
removeEnds[i] = removeEnds[i].replace(val, that.data[val]);
}
}
splitArrays.push([removeEnds[i].trim()]);
}
for (let i = 0; i < firstArr.length; i++) {
var fn = new Function('return ' + splitArrays[i][0])();
var exp = splitArrays[i][0];
for (let j = 0; j < operators.length; j++) {
var newRegExp = new RegExp('\\' + operators[j], 'g');
firstArr[i] = firstArr[i].replace(newRegExp, '\\' + operators[j]);
}
var pattern = new RegExp('\\{\\: ' + firstArr[i].trim() + ' \\:\\}', 'g');
view = view.replace(pattern, fn);
}
}
function comments() {
var pattern = new RegExp('\\{\\#[\\s\\S]*?\\#\\}', 'g');
view = view.replace(pattern, '');
}
(function() {
expression();
comments();
propsSubst();
forArray();
forObj();
ifElse();
entryEl.innerHTML = view;
})();
}
})();