-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml.nut
More file actions
288 lines (228 loc) · 6.32 KB
/
xml.nut
File metadata and controls
288 lines (228 loc) · 6.32 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
::XML<- {
version = "0.1.0"
Errors = {
OK = 0,
UNEXPECTED_TAG_OPEN = 1,
UNEXPECTED_TAG_CLOSE = 2,
}
ErrorMessages = {
[0] = "OK",
[1] = "Unexpected tag open",
[2] = "Unexpected tag close",
}
}
local TAG_OPEN_REGEX = regexp("<[\\w\\-]+(?:\\s+|\\n+)?(?:(?:[\\w\\d\\-]+=\".+\"(?:\\s+|\\n+)?)+)?\\/?>")
local TAG_NAME_REGEX = regexp("<([a-zA-Z0-9_]+)");
local TAG_PROPS_REGEX = regexp("(\\w+)=\"([^\"]+)\"");
local TAG_CLOSE_REGEX = regexp("</[\\w\\-]+>");
local COMMENT_REGEX = regexp("<!--(?:\\s+|\\n).+(?:\\s+|\\n)-->");
function XML::Open(filename) {
local fileData = FileToString(filename) || "";
return Parse(fileData);
}
function XML::CreateElement(type) {
local element = {
type = type,
parent = null,
children = [],
props = {},
metadata = {}
innerText = "",
}
return element;
}
function XML::FindElementByType(element, type, deep = false) {
foreach (child in element.children) {
if (child.type == type) {
return child;
}
if (deep) {
local found = FindElementByType(child, type, true);
if (found) {
return found;
}
}
}
return null;
}
function XML::LogError(...) {
local message = "";
foreach (part in vargv) {
message += part + " ";
}
if (message.len() > 0) {
error("XML Warning: " + message + "\n");
}
}
function XML::LogWarning(...) {
local message = "";
foreach (part in vargv) {
message += part + " ";
}
if (message.len() > 0) {
error("XML Warning: " + message + "\n");
}
}
function XML::QuerySelector(element, selector) {
local isClass = (selector.len() > 0 && selector.slice(0, 1) == ".");
local isId = (selector.len() > 0 && selector.slice(0, 1) == "#");
if (typeof selector != "string") {
error("Selector must be a string");
return null;
}
foreach (child in element.children) {
local found = false;
if (isClass) {
if ("class" in child.props) {
local classList = split(child.props["class"], " ");
foreach (className in classList) {
if (className == selector.slice(1)) {
found = true;
break;
}
}
}
} else if (isId) {
if ("id" in child.props && child.props["id"] == selector.slice(1)) {
found = true;
}
} else {
// Tag name selector
if (child.type == selector) {
found = true;
}
}
if (found) return child;
// Search deeper
local result = QuerySelector(child, selector);
if (result) return result;
}
return null;
}
function XML::AddChild(parent, child) {
child.parent = parent;
parent.children.append(child);
}
function XML::Replace(source, what, by, replaceAll = false) {
if (source.len() < what.len()) return source;
local idx = source.find(what);
if (idx == null) return source;
if (!replaceAll) {
return source.slice(0, idx) + by + source.slice(idx + what.len());
}
local result = "";
local lastIdx = 0;
while (idx != null) {
result += source.slice(lastIdx, idx) + by;
lastIdx = idx + what.len();
idx = source.find(what, lastIdx);
}
result += source.slice(lastIdx);
return result;
}
function XML::PushError(message) {
Print("XMLerror: " + message);
}
function XML::ProcessTag(tagData, parent) {
local captureData = TAG_NAME_REGEX.capture(tagData);
local tagName = tagData.slice(captureData[1].begin, captureData[1].end);
local element = CreateElement(tagName);
if (parent) {
AddChild(parent, element);
}
local lastCaptured = 0;
local propsCapture = TAG_PROPS_REGEX.capture(tagData);
while (propsCapture) {
local propName = tagData.slice(propsCapture[1].begin, propsCapture[1].end);
local propValue = tagData.slice(propsCapture[2].begin, propsCapture[2].end);
element.props[propName] <- propValue;
lastCaptured = propsCapture[0].end;
propsCapture = TAG_PROPS_REGEX.capture(tagData, lastCaptured);
}
return element;
}
function XML::LogTable(table, indent = 0, processed = []) {
local indentStr = " ";
for (local i = 0; i < indent; i++) {
indentStr += " ";
}
local superIndentStr = indentStr;
printl(indentStr + "{");
processed.push(table);
indentStr += " ";
foreach (key, value in table) {
if (typeof value == "table") {
if (processed.find(value) != null) {
printl(indentStr + key + ": { circular reference }");
continue;
}
if (value.keys().len() == 0) {
printl(indentStr + key + ": { empty }");
continue;
}
print(indentStr + key + ": ");
LogTable(value, indent + 1, processed);
} else if (typeof value == "array") {
if (value.len() == 0) {
printl(indentStr + key + ": [ empty ]");
continue;
}
printl(indentStr + key + ": [");
for (local i = 0; i < value.len(); i++) {
local item = value[i];
if (typeof item == "table" || typeof item == "array") {
LogTable(item, indent + 1, processed);
} else {
if (typeof item == "string") {
item = "\"" + item + "\"";
}
printl(indentStr + " " + item);
}
}
printl(indentStr + "]");
} else {
if (typeof value == "string") {
value = "\"" + value + "\"";
}
printl(indentStr + key + ": " + value);
}
}
printl(superIndentStr + "}");
}
function XML::Parse(fileData) {
local data = "";
local currentTag = CreateElement("root");
foreach (char in fileData) {
char = format("%c", char);
data += char;
if (char == ">") {
local commentCapture = COMMENT_REGEX.capture(data);
local tagCapture = TAG_OPEN_REGEX.capture(data);
local tagCloseCapture = TAG_CLOSE_REGEX.capture(data);
local isCaptured = commentCapture || tagCapture || tagCloseCapture;
if (commentCapture) {
// Comment, ignore content
} else if (tagCapture) {
local tagStr = data.slice(tagCapture[0].begin, tagCapture[0].end);
local rawText = data.slice(0, tagCapture[0].begin);
currentTag.innerText += rawText;
currentTag = ProcessTag(tagStr, currentTag);
local isSelfClosing = endswith(tagStr, "/>");
if (isSelfClosing && currentTag.parent) {
currentTag = currentTag.parent;
}
} else if (tagCloseCapture) {
local tagStr = data.slice(tagCloseCapture[0].begin, tagCloseCapture[0].end);
currentTag.innerText += data.slice(0, tagCloseCapture[0].begin);
currentTag.innerText = strip(currentTag.innerText);
if (currentTag.parent) {
currentTag = currentTag.parent;
} else {
LogWarning("Unexpected closing tag without parent: " + data.slice(tagCloseCapture[0].begin, tagCloseCapture[0].end));
}
}
data = isCaptured ? "" : data;
}
}
return currentTag;
}