-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_parse.py
351 lines (337 loc) · 15.1 KB
/
html_parse.py
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from html.parser import HTMLParser
import sys
DEBUG = False
class CustomHTMLParser(HTMLParser):
start = False
tag_struct = []
frame_stack = []
stack = []
decompiled_code = ["window = globals()", "globalThis = globals()"]
indent = 0
ret_count = 0
def wrap_indent(self, s):
return " " * self.indent + s
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
self.tag_struct.append(tag)
if tag == "main":
self.start = True
self.stack.append([])
if self.start:
# print("Encounter a start tag", tag)
self.frame_stack.append([])
if attrs != []:
attrmap = {}
for attr in attrs:
(k, v) = attr
attrmap[k] = v
# check for id. we add a comment line specifically for id in order to navigate
if "id" in attrmap and tag != "dfn":
code = self.wrap_indent(f"# LOC: {attrmap['id']}")
if DEBUG:
print(code)
self.decompiled_code.append(code)
match tag:
case "data":
assert "value" in attrmap
self.frame_stack[-1].append(int(attrmap["value"]))
case "dfn":
assert "id" in attrmap
fn_name = attrmap["id"]
code = self.wrap_indent(f"def {fn_name}(*args):")
self.indent += 1
self.stack.append([])
if DEBUG:
print(code)
self.decompiled_code.append(code)
case "var":
assert len(attrs) == 1
assert "title" in attrmap
current_stack = self.stack[-1]
code = ""
varname = attrmap["title"]
if len(current_stack) == 0:
code = self.wrap_indent(f"(*lst, {varname}) = lst")
else:
code = self.wrap_indent(f"{varname} = {current_stack[-1]}")
current_stack.pop()
if DEBUG:
print(code)
self.decompiled_code.append(code)
case "a":
assert len(attrs) >= 1
assert "href" in attrmap
if attrmap["href"].startswith("javascript:"):
assert attrmap["href"].endswith("()")
fn_name = attrmap["href"][11:-2]
if "target" in attrmap:
assert attrmap["target"] == "_top"
current_stack = self.stack[-1]
assert len(current_stack) >= 1
current_frame = self.frame_stack[-1]
# put into frame stack as args are not confirmed
current_frame.append(f"{current_stack[-1]}.{fn_name}")
current_stack.pop()
else:
current_frame = self.frame_stack[-1]
current_frame.append(f"{fn_name}")
# create a new stack for <a>
self.stack.append([])
else:
assert attrmap["href"].startswith("#")
loc = attrmap["href"][1:]
code = self.wrap_indent(f"goto {loc}")
if DEBUG:
print(code)
self.decompiled_code.append(code)
case "cite":
# this should not happen, should be just id
assert len(attrs) == 1
assert attrs[0][0] == "id"
case _:
print("Unhandled attr", tag, attrs)
input()
else:
match tag:
case "i":
# if
current_stack = self.stack[-1]
assert len(current_stack) >= 1
code = self.wrap_indent(f"if {current_stack[-1]}:")
if DEBUG:
print(code)
current_stack.pop()
self.decompiled_code.append(code)
# duplicate a stack for the inside if
self.stack.append([i for i in current_stack])
self.indent += 1
case "table":
# object, create a new stack = [keyArray, valArray] for it
self.stack.append([[], []])
case "ol":
# table, create a new stack val array
self.stack.append([])
case _:
# assume no attrs dont need to handle
return
def handle_endtag(self, tag: str) -> None:
if self.start:
current_frame = self.frame_stack[-1]
current_stack = self.stack[-1]
match tag:
case "cite":
assert len(current_frame) == 1
current_stack.append(current_frame[0])
case "samp":
assert len(current_stack) >= 2
assert len(current_frame) == 1
prop = current_frame[0]
val = current_stack[-1]
obj = current_stack[-2]
code = self.wrap_indent(f'{obj}["{prop}"] = {val}')
if DEBUG:
print(code)
self.decompiled_code.append(code)
current_stack.pop()
current_stack.pop()
case "data":
current_stack.append(current_frame[0])
case "var":
assert len(current_frame) == 0
case "s":
assert len(current_frame) <= 1
val = "" if len(current_frame) == 0 else current_frame[0]
current_stack.append(f'"{val}"')
case "a":
assert len(current_frame) <= 1
if len(current_frame) == 1:
fn_name = current_frame[0]
args = ", ".join(list(map(str, current_stack)))
code = self.wrap_indent(f"_result{self.ret_count} = {fn_name}({args})")
if DEBUG:
print(code)
self.decompiled_code.append(code)
self.stack.pop()
self.stack[-1].append(f"_result{self.ret_count}")
self.ret_count += 1
else:
# this is the goto case, for now disregard it
pass
case "i":
# clean up the duplicated stack
self.stack.pop()
self.indent -= 1
case "rp":
assert len(current_frame) == 1
prop = current_frame[0]
current_stack[-1] = f"{current_stack[-1]}.{prop}"
case "rt":
code = ""
if len(current_stack) >= 1:
value = current_stack[-1]
code = self.wrap_indent(f"return {value}")
else:
code = self.wrap_indent(f"return")
if DEBUG:
print(code)
self.decompiled_code.append(code)
# I dont need to clean up stack, as I can cleanup it in end of dfn block
case "dfn":
self.indent -= 1
self.stack.pop()
case "bdi":
assert len(current_frame) == 0
assert len(current_stack) >= 1
val = current_stack[-1]
if " " in val:
val = f"({val})"
current_stack[-1] = f"not {val}"
case "tr":
# tr should be irrelevant
pass
case "table":
assert len(current_stack) == 2
(keyArr, valArr) = current_stack
obj = {}
for i in range(len(keyArr)):
obj[keyArr[i]] = valArr[i]
self.stack.pop()
self.stack[-1].append(obj)
case "ol":
arr = [i for i in current_stack]
self.stack.pop()
self.stack[-1].append(arr)
case "dd":
assert len(current_stack) >= 2
operand2 = current_stack[-1]
operand1 = current_stack[-2]
if type(operand1) == str and " " in operand1:
operand1 = f"({operand1})"
if type(operand2) == str and " " in operand2:
operand2 = f"({operand2})"
current_stack.pop()
current_stack.pop()
current_stack.append(f"{operand1} + {operand2}")
case "sub":
assert len(current_stack) >= 2
operand2 = current_stack[-1]
operand1 = current_stack[-2]
if type(operand1) == str and " " in operand1:
operand1 = f"({operand1})"
if type(operand2) == str and " " in operand2:
operand2 = f"({operand2})"
current_stack.pop()
current_stack.pop()
current_stack.append(f"{operand1} - {operand2}")
case "ul":
assert len(current_stack) >= 2
operand2 = current_stack[-1]
if type(operand2) == str and " " in operand2:
operand2 = f"({operand2})"
current_stack.pop()
current_stack[-1] = f"{current_stack[-1]} * {operand2}"
case "address":
assert len(current_stack) >= 2
idx = current_stack[-1]
arr = current_stack[-2]
current_stack.pop()
current_stack.pop()
current_stack.append(f"{arr}[{idx}]")
case "ins":
assert len(current_stack) >= 3
val = current_stack[-1]
idx = current_stack[-2]
arr = current_stack[-3]
current_stack.pop()
current_stack.pop()
current_stack.pop()
code = self.wrap_indent(f"{arr}[{idx}] = {val}")
if DEBUG:
print(code)
self.decompiled_code.append(code)
case "small":
assert len(current_stack) >= 2
operand2 = current_stack[-1]
operand1 = current_stack[-2]
if type(operand1) == str and " " in operand1:
operand1 = f"({operand1})"
if type(operand2) == str and " " in operand2:
operand2 = f"({operand2})"
current_stack.pop()
current_stack.pop()
current_stack.append(f"{operand1} < {operand2}")
case "big":
assert len(current_stack) >= 2
operand2 = current_stack[-1]
operand1 = current_stack[-2]
if type(operand1) == str and " " in operand1:
operand1 = f"({operand1})"
if type(operand2) == str and " " in operand2:
operand2 = f"({operand2})"
current_stack.pop()
current_stack.pop()
current_stack.append(f"{operand1} > {operand2}")
case "em":
assert len(current_stack) >= 2
operand2 = current_stack[-1]
operand1 = current_stack[-2]
if type(operand1) == str and " " in operand1:
operand1 = f"({operand1})"
if type(operand2) == str and " " in operand2:
operand2 = f"({operand2})"
current_stack.pop()
current_stack.pop()
current_stack.append(f"{operand1} == {operand2}")
case "b":
assert len(current_stack) >= 2
operand2 = current_stack[-1]
operand1 = current_stack[-2]
if type(operand1) == str and " " in operand1:
operand1 = f"({operand1})"
if type(operand2) == str and " " in operand2:
operand2 = f"({operand2})"
current_stack.pop()
current_stack.pop()
current_stack.append(f"{operand1} and {operand2}")
case "bdo":
assert len(current_stack) >= 2
operand2 = current_stack[-1]
operand1 = current_stack[-2]
if type(operand1) == str and " " in operand1:
operand1 = f"({operand1})"
if type(operand2) == str and " " in operand2:
operand2 = f"({operand2})"
current_stack.pop()
current_stack.pop()
current_stack.append(f"{operand1} or {operand2}")
case "dt":
assert len(current_stack) >= 1
current_stack.append(current_stack[-1])
case "main":
pass
case "li":
# should not relevant
pass
case _:
print("tag unimplemented", tag)
input()
self.frame_stack.pop()
assert self.tag_struct[-1] == tag
self.tag_struct.pop()
if tag == "main":
self.start = False
def handle_startendtag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
if self.start:
print("startend tag unimplemented", tag)
def handle_data(self, data: str) -> None:
if data.strip() == "":
return
if self.start:
self.frame_stack[-1].append(data.strip())
def decompile(self) -> str:
return "\n".join(self.decompiled_code)
f = open(sys.argv[1], 'r')
raw = f.read()
f.close()
parser = CustomHTMLParser()
parser.feed(raw)
print(parser.decompile())