-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhtml_fix.py
More file actions
68 lines (58 loc) · 1.75 KB
/
html_fix.py
File metadata and controls
68 lines (58 loc) · 1.75 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
import os
marker_string = "<div class="notification warning notification-gentle-alert"
merge_string1 = 'class="seq_contents tex2jax_ignore asciimath2jax_ignore">'
merge_string2 = '(function (require) {'
delta = 65
DATA_PATH = "./data/"
RES_PATH = "./result/"
def clear_problems(filename):
"""
Removes bad sectors from html
:param filename:
:return:
"""
lines = []
with open(DATA_PATH + filename, 'r') as f:
try:
lines = f.readlines()
for i in range(len(lines)):
if marker_string in lines[i]:
for j in range(i, i + delta):
lines[j] = ""
except UnicodeDecodeError:
print(filename)
return None
f = open(RES_PATH + "FIX" + filename, 'w')
f.writelines(lines)
f.close()
def merge_fixed():
files = os.listdir(RES_PATH)
init_f = files[0]
del files[0]
f = open(RES_PATH + init_f, 'r')
lines = f.readlines()
start_i = get_line_index(lines, merge_string2)[-2] - 2
f_begin = lines[:start_i]
f_end = lines[start_i:]
for en in files:
with open(RES_PATH + en, 'r') as ff:
lines = ff.readlines()
s_ind = get_line_index(lines, merge_string1)[0] + 1
e_ind = get_line_index(lines, merge_string2)[-2] - 1
lines = lines[s_ind:e_ind]
f_begin += lines
f.close()
f = open(RES_PATH + 'sum.html', 'w')
f.writelines(f_begin + f_end)
f.close()
def get_line_index(lines, pattern):
ind = []
for i, line in enumerate(lines):
if pattern in line:
ind.append(i)
return ind
if __name__ == "__main__":
files = os.listdir(DATA_PATH)
for en in files:
clear_problems(en)
merge_fixed()