-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconvert_tex_to_html.py
More file actions
executable file
·210 lines (163 loc) · 6.78 KB
/
convert_tex_to_html.py
File metadata and controls
executable file
·210 lines (163 loc) · 6.78 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
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
#!/usr/bin/env python3
import string
import datetime
import argparse
from subprocess import Popen, PIPE
from collections import Counter
import lxml.html
import roman
a = argparse.ArgumentParser()
a.add_argument('-F', '--final', default=False, action='store_true', help='Remove draft watermarks')
a.add_argument('-d', '--date', default=[], nargs=1, help='Date string to use (yyyy-mm-dd)')
a.add_argument('-f', '--file', dest='texfile', type=argparse.FileType('r'), default='./constitution.tex')
a.add_argument('-t', '--template', dest='template', type=argparse.FileType('r'), default='./template.html')
a.add_argument('-T', '--title', default='Constitution')
a.add_argument('-P', '--parts', default=False, action='store_true')
a.add_argument('-toc', '--toc', default=False, action='store_true')
args = a.parse_args()
with open('resources/draft.png.base64') as f:
draft_style = """<style>
.page {
background: url(data:image/png;base64,%s) repeat !important;
background-size: 100%% !important;
}
</style>""" % f.read()
list_depth_tokens = ['1', 'a', 'i', 'A']
list_depth_style = {
'1': lambda x: str(x),
'a': lambda x: string.ascii_lowercase[x-1],
'i': lambda x: roman.toRoman(x).lower(),
'A': lambda x: string.ascii_uppercase[x-1]
}
def get_list_depth(node):
i = -1
if node.tag == "ol" or node.tag == "ul":
i += 1
parent = node.getparent()
while parent is not None:
if parent.tag == "ol" or parent.tag == "ul":
i += 1
parent = parent.getparent()
return i
def create_para_link(doc, node, id_attr=None):
a = doc.makeelement('a')
a.attrib['href'] = "#%s" % (id_attr or node.attrib['id'])
a.attrib['class'] = 'pilcrow'
a.text = "¶"
node.append(a)
def id_str(value, depth, separator):
if separator == ".":
return str(value)
return list_depth_style[list_depth_tokens[depth]](int(value))
def generate_id(counter, depth, separator=".", prefix="part-", suffix=""):
o = []
i = depth
while i >= 1:
o.append(counter[i])
i -= 1
if current_level == 0:
return prefix + roman.toRoman(articles[current_level]).lower()
elif args.parts:
return prefix + roman.toRoman(articles[0]).lower() + '-' + separator.join([id_str(x, n, separator) for n, x in enumerate(reversed(o))]) + suffix
else:
return separator.join([id_str(x, n, separator) for n, x in enumerate(reversed(o))]) + suffix
def generate_list_id(counter, depth, separator=")(", prefix="(", suffix=")"):
o = []
i = depth
while i >= 0:
o.append(counter[i])
i -= 1
return prefix + separator.join([id_str(x, n, separator) for n, x in enumerate(reversed(o))]) + suffix
def reset_counter_to(counter, depth):
for k in [k for k in counter.keys() if k > depth]:
del counter[k]
cmd = r"sed 's/\\part/\\chapter/' | pandoc -f latex -t html5 --section-divs --email-obfuscation=none"
process = Popen(cmd, shell=True, stdout=PIPE, stdin=args.texfile)
data = process.communicate()[0].decode()
text = args.template.read() % data
if len(args.date) > 0:
date = datetime.datetime.strptime(args.date[0], "%Y-%m-%d")
else:
date = datetime.datetime.now()
text = text.replace("{date}", "{0.day} {0:%B} {0:%Y}".format(date))
text = text.replace("{date_iso}", date.strftime("%Y-%m-%d"))
doc = lxml.html.fromstring(text)
if len(args.title) > 0:
text = text.replace("{title}", args.title)
else:
text = text.replace("{title}", "")
doc = lxml.html.fromstring(text)
if args.parts:
text = text.replace("/*!!", "")
text = text.replace("!!*/", "")
doc = lxml.html.fromstring(text)
if args.toc:
text = text.replace("{toc}", "Contents")
else:
text = text.replace('<h2 class="toc-heading">{toc}</h2>', '')
text = text.replace('<ol id="toc"></ul>', '')
doc = lxml.html.fromstring(text)
articles = Counter()
list_items = Counter()
last_id = ""
last_section = None
list_depth = -1
ready = False
if not args.final:
doc.head.append(lxml.html.fragment_fromstring(draft_style))
doc.body.cssselect(".title")[0].append(lxml.html.fragment_fromstring(
"<span style='color: red'> DRAFT</span>"))
# Sub in the logo
with open("resources/logo.png.base64") as f:
doc.body.cssselect(".logo img")[0].attrib['src'] = "data:image/png;base64," + f.read()
for node in doc.body.iter():
if not ready:
if node.tag == "hr":
ready = True
node.getparent().remove(node)
else: continue
# Catch sections (\part)
if node.tag == "section" and node.attrib['class'] != "footnotes":
last_section = node
current_level = int(node.attrib['class'][5:]) - 1
node.attrib.clear()
articles[current_level] += 1
last_id = node.attrib['id'] = generate_id(articles, current_level)
#node.attrib['class'] = 'article'
node.tag = "div"
reset_counter_to(articles, current_level)
if node.tag in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
create_para_link(doc, node, last_section.attrib['id'])
if node.tag == "h1":
node.attrib['class'] = 'part'
if args.toc and args.parts:
doc.body.cssselect("#toc")[0].append(lxml.html.fragment_fromstring("<li><p><a href='#" + last_section.attrib['id'] + "'>" + node.text + "</a></p></li>"))
if node.tag == "h2":
if args.toc and not args.parts:
doc.body.cssselect("#toc")[0].append(lxml.html.fragment_fromstring("<li><p><a href='#" + last_section.attrib['id'] + "'>" + node.text + "</a></p></li>"))
elif node.tag == "dt":
node.attrib['id'] = node.text.strip().lower().replace(" ", '-').replace(":", "")
create_para_link(doc, node, node.attrib['id'])
elif node.tag == "ol":
if "class" in dict() and node.getparent().attrib['class'] == "footnotes":
node.attrib['class'] = "footnote-list"
else:
list_depth = get_list_depth(node)
reset_counter_to(list_items, list_depth-1)
node.attrib['type'] = list_depth_tokens[list_depth]
elif node.tag == "ul":
list_depth = get_list_depth(node)
reset_counter_to(list_items, list_depth-1)
node.attrib['type'] = list_depth_tokens[list_depth]
elif node.tag == "li":
if node.getparent().getparent().tag == "section":
node.attrib['class'] = "footnote"
else:
list_depth = get_list_depth(node)
list_items[list_depth] += 1
node.attrib['id'] = last_id + generate_list_id(list_items, list_depth)
reset_counter_to(list_items, list_depth)
create_para_link(doc, node[0], node.attrib['id'])
if node.tag == "hr":
node.attrib['style'] = "display:none;"
print(lxml.html.tostring(doc, pretty_print=True, doctype="<!DOCTYPE html>").decode())