-
Notifications
You must be signed in to change notification settings - Fork 48
/
post-process-html.py
executable file
·249 lines (199 loc) · 9.35 KB
/
post-process-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
#!/usr/bin/env python
import re
import sys
from bs4 import BeautifulSoup, Comment, Doctype, Tag, NavigableString
# soup = BeautifulSoup(open(sys.argv[1]), "html.parser")
soup = BeautifulSoup(open(sys.argv[1]), "html5lib")
def combine_citation_links(soup):
for e in soup.find_all("span", attrs={"class": "cite"}):
# find_all will create a list, that also contains stuff
# we already processed, i.e., without parent
if e.parent is None:
continue
if e.previous_sibling == ',\xa0':
# within a list, use normal spaces to avoid typesetting issues
e.previous_sibling.replace_with(", ")
a_elem = e.next_element
# also ignore just year links, we might just have fixed the space before
if a_elem.contents[0].isnumeric():
# though, we should still unwrap them
e.replace_with(a_elem)
continue
# Process text like '<a>Author</a> [<a>Year</a>]'
if (e.next_sibling == '\xa0[' and e.next_sibling and
e.next_sibling.next_sibling and
e.next_sibling.next_sibling.next_sibling):
space_bracket = e.next_sibling
year_node_span = e.next_sibling.next_sibling
year_node_a = year_node_span.next_element
following_bracket_and_more = e.next_sibling.next_sibling.next_sibling
if str(following_bracket_and_more)[0] == ']':
assert a_elem.attrs['href'] == year_node_a.attrs['href'],\
"should be always the same, because we want to merge those two <a> tags"
year = str(year_node_a.contents[0])
a_elem.contents[0].replace_with(a_elem.contents[0] + '\xa0[' + year + "]")
space_bracket.extract()
year_node_span.extract()
year_node_a.extract()
# remove closing bracket
following_bracket_and_more.replace_with(
str(following_bracket_and_more)[1:])
e.replace_with(a_elem)
continue
# transform 'Author [<a>Year</a>, <a>Year2</a>]'
e.replace_with(a_elem.contents[0]) # remove link from author
continue
# Process text like '[<a>Author</a>, <a>Year</a>]'
# This also includes situations with multiple references, that are
# listed with author+year.
if (e.next_sibling == ',\xa0' and
isinstance(e.next_sibling.next_sibling, Tag)):
possible_span = e.next_sibling.next_sibling
if possible_span.name == 'span' and possible_span.attrs.get('class') == ['cite']:
a_elem2 = possible_span.next_element
if a_elem2.name == 'a' and a_elem2.attrs.get('href') == a_elem.attrs['href']:
comma_space_node = e.next_sibling
year_node_span = e.next_sibling.next_sibling
year_node_a = year_node_span.next_element
year_node = year_node_a.contents[0]
if (year_node_a.next_sibling and
year_node_a.next_sibling.next_sibling and
year_node_a.next_sibling.next_sibling.name == "a" and
year_node_a.next_sibling.next_sibling.contents[0].isnumeric()):
# looks like we got multiple years with the same author in
# succession, remove the link on the author to avoid confusion
prev = e.previous_sibling
if prev == ',\xa0':
prev.replace_with(", ")
elif (isinstance(prev, NavigableString) and
str(prev)[-1] == "["):
prev.replace_with(s[:-2] + "\N{THIN SPACE}[")
e.replace_with(e.contents[0])
else:
comma_space_node.extract()
year_node_span.extract()
a_elem.append(comma_space_node)
a_elem.append(year_node)
# if we are the first in the list, make sure the bracket is
# directly connected with a   to the element before
if isinstance(e.previous_sibling, NavigableString):
s = str(e.previous_sibling)
if s[-1] == "[" and s[-2].isspace():
e.previous_sibling.replace_with(s[:-2] + "\N{THIN SPACE}[")
# at the end, unwrap the <a> from the <span class='cite'>, we don't really need it
e.replace_with(a_elem)
def remove_font_spans(soup):
for e in soup.find_all("span", {'class' : re.compile("^LinLibertine") }):
e.replace_with(e.contents[0])
for e in soup.find_all("span", {'class' : re.compile("^LinBiolinum") }):
e.replace_with(e.contents[0])
for e in soup.find_all("span", {'class' : re.compile("^cm") }):
e.replace_with(e.contents[0])
for e in soup.find_all("span", {'class' : re.compile("^pcrr") }):
e.replace_with(e.contents[0])
for e in soup.find_all("span", {'class' : re.compile("^ptmr") }):
e.replace_with(e.contents[0])
def remove_following_newline(e):
sibling = e.next_sibling
if sibling.string == '\n':
sibling.extract()
def remove_unwanted_meta(soup):
for e in soup.find_all("meta"):
if e.has_attr('name') and e['name'] in ['date', 'src', 'originator',
'generator']:
remove_following_newline(e)
e.extract()
def remove_tex4ht_comments(soup):
for e in soup.find_all(string=lambda text:isinstance(text, Doctype)):
remove_following_newline(e)
e.extract()
for e in soup.find_all(string=lambda text:isinstance(text, Comment)):
if 'fn-in,' in e.string or e.string.startswith('?xml') or e.string.startswith('http://www.w3.org/TR/xhtml1'):
remove_following_newline(e)
e.extract()
else:
print(e)
def remove_superfluous_id_after_footnote(soup):
for e in soup.find_all("sup", {'class' : 'textsuperscript'}):
sib = e.next_sibling
if sib.name == "a" and sib.has_attr('id') and sib.has_attr('name') and sib['id'] == sib['name']:
sib.extract()
def transform_header(soap):
head = soup.find("div", {"class": "maketitle"})
if not head or head.contents[1].name != "h2" or head.contents[3].name != "div":
return # the format of the header is not currently supported
if head.contents[0] == "\n":
head.contents[0].extract()
title_h2 = head.contents[0].extract()
title = title_h2.getText()
if head.contents[0] == "\n":
head.contents[0].extract()
author_div = head.contents[0].extract()
authors = author_div.string
title_h1 = soup.new_tag("h1")
title_h1.string = title
title_tag = soup.find("title")
title_tag.string = title
meta_author = soup.new_tag("meta")
meta_author.attrs['name'] = "author"
meta_author.attrs['content'] = authors
head_tag = soup.find("head")
head_tag.append(meta_author)
header = soup.new_tag("header")
header.append(title_h1)
header.append(author_div)
while len(head.contents) > 0:
e = head.contents[0].extract()
header.append(e)
head.replace_with(header)
# move abstract, if available
abstract = soup.find("div", {"class": "abstract"})
if abstract:
header.append(abstract.extract())
ab_t = abstract.find("div", {"class": "center"})
if ab_t and ab_t.p.string == "Abstract":
ab_t.extract()
ab_t = soup.new_tag("h3")
ab_t.string = "Abstract"
abstract.insert(0, ab_t)
def wrap_body_in_article(soup):
body = soup.find("body")
article = soup.new_tag("article")
# can't loop over .contents directly because it is changing
while len(body.contents) > 0:
article.append(body.contents[0].extract())
body.append(article)
def add_line_numbers_to_listings(soup):
for f in soup.find_all("figure"):
div_ln = f.find("div", {"class": "linenumbers"})
if div_ln:
code = f.find("code")
lines = code.find_all("a") # find by labels, might be brittle
ln_nums = " ".join([str(i) for i in range(1, len(lines) + 1)])
div_ln.string = ln_nums
def in_code_remove_span_making_text_black(soup):
for c in soup.find_all('code'):
for e in c.find_all("span", attrs={"style": "color:#000000"}):
e.replace_with(e.next_element)
def remove_url_class(soup):
for a in soup.find_all("a", {"class": "url"}):
del a.attrs['class']
wrap_body_in_article(soup)
remove_tex4ht_comments(soup)
remove_unwanted_meta(soup)
remove_superfluous_id_after_footnote(soup)
combine_citation_links(soup)
remove_font_spans(soup)
transform_header(soup)
add_line_numbers_to_listings(soup)
in_code_remove_span_making_text_black(soup)
remove_url_class(soup)
result = soup.encode(encoding="utf-8", formatter="html").decode()
result = result.replace('ffi', 'ffi')
result = result.replace('ff', 'ff')
result = result.replace('ffl', 'ffl')
result = result.replace('fj', 'fj')
result = result.replace('fl', 'fl')
result = result.replace('fi', 'fi')
result = result.replace('!!TEXTBACKSLASH!!', '\\')
print(result)