-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.py
40 lines (31 loc) · 1.01 KB
/
helper.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
import re
import urwid
from html.parser import HTMLParser
# https://stackoverflow.com/questions/753052/strip-html-from-strings-in-python
class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.strict = False
self.convert_charrefs = False
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
class Helper:
@staticmethod
def parse_comment(html):
"""Return urwid.Text formatted string."""
# Replace HTML breaks with \n
html = html.replace("<br>", '\n')
html = html.replace(">", '>')
html = html.replace(""", '"')
s = MLStripper()
s.feed(html)
html = s.get_data()
html_list = re.split(r'\n', html)
for index, line in enumerate(html_list):
html_list[index] += "\n"
if re.search('>', line): # Green-texting
html_list[index] = ('quote', line + "\n")
return html_list