-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
62 lines (45 loc) · 1.46 KB
/
utils.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
from html.parser import HTMLParser
class Encrepter(HTMLParser):
def __init__(self):
super(Encrepter, self).__init__()
self.tags = []
self.content = []
def print(self, line):
self.content.append(line)
def finalize(self):
return ''.join(self.content)
def handle_starttag(self, tag, attrs):
# print("Encountered a start tag:", tag, attrs)
# Handle img alt here
self.tags.append((tag, attrs))
self.print('<NOP>')
def handle_endtag(self, tag):
self.tags.append(tag)
self.print('</NOP>')
def handle_data(self, data):
self.print(data)
class Decrepter(HTMLParser):
def __init__(self, tags):
super(Decrepter, self).__init__()
self.tags = tags
self.cur_tag = 0
self.content = []
def print(self, line):
self.content.append(line)
def finalize(self):
return ''.join(self.content)
def next_tag(self):
output = self.tags[self.cur_tag]
self.cur_tag += 1
return output
def handle_starttag(self, tag, attrs):
# print("Encountered a start tag:", tag, attrs)
tag, attrs = self.next_tag()
contents = [tag]
contents.extend(f'{k}="{v}"' for k, v in attrs)
self.print('<' + ' '.join(contents) + '>')
def handle_endtag(self, tag):
tag = self.next_tag()
self.print(f'</{tag}>')
def handle_data(self, data):
self.print(data)