-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_html.py
298 lines (204 loc) · 6.35 KB
/
parse_html.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
import glob
import types
import os
from lxml import html
from lxml.html import Element, HTMLParser, tostring
from weasyprint import HTML
MUBU_DIR = 'mubu\\'
HTML_DIR = 'html\\'
PDF_DIR = 'pdf\\'
PRINT_CSS = '../css/print.css'
MUBU_CSS = '../css/mubu.css'
mubuList = []
level = 0
hLevel = 0
pLevel = 0
leading = ''
hLeading = ''
pLeading = ''
htmlFile = None
MUST_COLOR = True
MUST_BOLD = False
MUST_DIGITAL = True
MUST_STEM = False
DIGITALS = '零〇一二三四五六七八九十百壹贰叁肆伍陆柒捌玖拾佰0123456789①②③④⑤⑥⑦⑧⑨⑩'
STEMS = '甲乙丙丁戊己庚辛壬癸子丑寅卯辰巳午未申酉戌亥'
IGNORED_CLASSES = ['bullet', 'bullet-dot']
REMOVE_ATTRS = ['style', 'target', 'spellcheck', 'rel']
def parse(path: str):
global level
global leading
global hLevel
global hLeading
global pLevel
global pLeading
level = 0
hLevel = 1
pLevel = 0
leading = ''
hLeading = ''
pLeading = ''
global htmlFile
htmlPath = path.replace(MUBU_DIR, HTML_DIR)
htmlFile = open(htmlPath, 'w', encoding='utf-8')
htmlFile.write('<!DOCTYPE html>\n<html>\n<head>\n<meta charset="utf-8"/>\n')
htmlFile.write(f'<link rel="stylesheet" href="{PRINT_CSS}" type="text/css"/>\n')
htmlFile.write(f'<link rel="stylesheet" href="{MUBU_CSS}" type="text/css"/>\n')
print('parse: ', path)
parser = HTMLParser(encoding='utf-8')
tree = html.parse(path, parser)
root = tree.getroot()
# /html/body
body = root.find('body')
if body is None:
print('no "html/body" node.')
return
# /html/body/title
titles = body.cssselect('div.title')
if len(titles) == 0:
print('no "/html/body/title" node.')
return
title = titles[0]
print('title: ', title.text)
htmlFile.write(f'<title>{title.text}</title>\n</head>\n<body>\n')
# /html/body/ul
ul = body.find('ul')
if ul is None:
print('no "/html/body/ul" node.')
return
travelNode(ul)
travelHead(ul)
htmlFile.write('</body>\n</html>\n')
htmlFile.close()
def travelNode(node: Element):
global level
global leading
if node is None:
return
bindMethods(node)
node.cleanBullet()
node.cleanAttrs()
# print(leading, node.tag, node.attrib)
level += 1
leading += ' '
childs = node.findall('*')
for child in childs:
travelNode(child)
level -= 1
leading = leading[:-1]
def travelHead(ul: Element):
global hLevel
global hLeading
global pLevel
global pLeading
if ul is None:
return
lis = ul.findall('li')
for li in lis:
if li is None:
continue
contents = li.xpath('div[contains(@class, "content")]')
if len(contents) == 0:
continue
content = contents[0]
span = content.find('span')
isHeading = checkHeading(li, span)
if isHeading:
print(f'{hLeading}<h{hLevel}>{inner(content)}</h{hLevel}>')
htmlFile.write(f'{hLeading}<h{hLevel}>{inner(content)}</h{hLevel}>\n')
notes = li.xpath('div[contains(@class, "note")]')
if len(notes) > 0:
note = notes[0]
htmlFile.write(f'{hLeading}{outer(note)}\n')
childrens = li.xpath('div[contains(@class, "children")]')
if len(childrens) > 0:
children = childrens[0]
ul = children.find('ul')
if ul:
hLevel += 1
hLeading += ' '
travelHead(ul)
hLevel -= 1
hLeading = hLeading[:-2]
else:
htmlFile.write(f'{hLeading}{outer(content)}\n')
notes = li.xpath('div[contains(@class, "note")]')
if len(notes) > 0:
note = notes[0]
htmlFile.write(f'{hLeading}{outer(note)}\n')
childrens = li.xpath('div[contains(@class, "children")]')
if len(childrens) > 0:
children = childrens[0]
htmlFile.write(f'{hLeading}{outer(children)}\n')
def inner(node: Element) -> str:
if node is None:
return ''
inner = ''
for child in node:
inner += tostring(child, encoding='unicode').strip()
return inner
def outer(node: Element) -> str:
if node is None:
return ''
return tostring(node, encoding='unicode').strip()
def bindMethods(node: Element):
node.cleanBullet = types.MethodType(cleanBullet, node)
node.cleanAttrs = types.MethodType(cleanAttrs, node)
def cleanBullet(self: Element):
bullets = self.xpath("div[contains(@class, 'bullet')]")
for bullet in bullets:
self.remove(bullet)
def cleanAttrs(self: Element):
for attr in REMOVE_ATTRS:
if attr in self.attrib:
self.attrib.pop(attr)
def checkHeading(li: Element, span: Element) -> bool:
if li is None:
return False
for attr in li.classes:
if attr.startswith('heading'):
return True
hasColor = False
hasBold = False
hasDigital = False
hasStem = False
if span is None:
return False
if 'bold' in span.classes:
hasBold = True
for klass in span.classes:
if klass.startswith('text-color'):
hasColor = True
if len(span.text) > 0 and span.text[0] in DIGITALS\
or len(span.text) > 1 and span.text[1] in DIGITALS:
hasDigital = True
if len(span.text) > 0 and span.text[0] in STEMS:
hasStem = True
if MUST_COLOR and not hasColor\
or MUST_BOLD and not hasBold\
or MUST_DIGITAL and not hasDigital\
or MUST_STEM and not hasStem:
return False
return True
def getMubuList():
global mubuList
mubuList = glob.glob(f'{MUBU_DIR}*.htm*')
def topdf(path):
htmlPath = path.replace(MUBU_DIR, HTML_DIR)
pdfPath = path.replace(MUBU_DIR, PDF_DIR)
pdfPath = pdfPath.replace('.html', '.pdf')
print('convert to: ', pdfPath)
HTML(htmlPath).write_pdf(pdfPath)
def main():
global htmlFile
getMubuList()
if not os.path.exists(HTML_DIR):
os.makedirs(HTML_DIR, exist_ok=True)
if not os.path.exists(PDF_DIR):
os.makedirs(PDF_DIR, exist_ok=True)
for mubu in mubuList:
parse(mubu)
topdf(mubu)
print('done!')
if __name__ == "__main__":
main()