-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlwriter.py
More file actions
439 lines (412 loc) · 17.2 KB
/
htmlwriter.py
File metadata and controls
439 lines (412 loc) · 17.2 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
'''Package htmlwriter:
This package implements objects that eat pages.StructuredPages and turns them
into HTML.'''
import html
import re
import sys
import elements
import pages
import toc
def htmlformat(string, newlines=True):
'''htmlformat(string): Replace placeholders and special characters.
Parameters:
- string: str, the string to format
- newlines: bool, optional (default: True), should newlines be augmented
with <br>
'''
string = string.replace("| \x03X", "| \x03X")
string = string.replace('\x03', '□')
string = string.replace('\x06', '')
string = string.replace('\x07', '')
string = html.escape(string)
if newlines:
string = string.replace('\n', "<br>\n")
string = re.sub("\x1bfootnote(.*?)\x1b",
r'<a href="#footnote\g<1>" class="footnote">\g<1>)</a>',
string)
string = re.sub("\x1blinkhttp(.*?)\x1b",
r'<a href="http\g<1>">http\g<1></a>', string)
string = re.sub(f"(annex\\s)\x1blink({toc.ANNEXREGEX})\x1b",
r'<a href="#\g<2>">\g<1>\g<2></a>', string)
string = re.sub(f"(clause\\s)\x1blink({toc.CHAPTERREGEX})\x1b",
r'<a href="#\g<2>">\g<1>\g<2></a>', string)
string = re.sub("\x1blink(.*?)\x1b", r'<a href="#\g<1>">\g<1></a>', string)
# Match opt for grammars, except in M.2p1 "adopt"
string = re.sub(r'''(?<!ad)\Bopt\b''', r'<sub>opt</sub>', string)
return string
class Tag:
'''Attributes:
- tag: str, the type of tag
- attributes: str or None, the attributes on the tag
- contents: str or Tag or list of str or Tag, the contents of the tag'''
def __init__(self, tag, contents=None):
'''tag is split into self.tag and self.attributes'''
split = tag.split(maxsplit=1)
if len(split) == 2:
self.tag = split[0]
self.attributes = split[1]
else:
self.tag = tag
self.attributes = None
self.contents = list()
if contents is not None:
if type(contents) is list:
for content in contents:
self.add(content)
else:
self.add(contents)
def add(self, content):
'''add(self, content): Add to the tags contents.
Parameters:
- content: str or Tag, the content to add'''
if type(content) is str:
self.contents += content.strip('\n').split('\n')
elif isinstance(content, Tag):
self.contents.append(content)
else:
raise ValueError(
f"Wrong content type: {content.__class__.__name__}")
def __repr__(self):
res = self.tag
if self.attributes is not None:
res += " " + self.attributes
if self.contents:
res += ':'
for c in self.contents:
r = repr(c)
for line in r.strip('\n').split('\n'):
res += "\n " + line
return res
def tohtml(self):
'''tohtml(self): Turn the Tag into an HTML string'''
res = '<' + self.tag
if self.attributes is not None:
res += ' ' + self.attributes
res += ">"
if self.attributes and self.attributes[-1] == '/':
# self closing, no-content tag
return res + '\n'
for content in self.contents:
if isinstance(content, Tag):
string = content.tohtml()
else:
string = content
for line in string.strip('\n').split('\n'):
res += "\n " + line
if self.tag == "pre":
res += "</" + self.tag + ">\n"
else:
res += "\n</" + self.tag + ">\n"
return res
def footnotetohtml(footnoteid, elems):
'''footnotetohtml(footnoteid, elems): Turn a footnote into a HTML tag.
Return a Tag.'''
aside = Tag("aside", f'<a href="#footnote{footnoteid}">{footnoteid})</a>')
div = Tag(f'div id="footnote{footnoteid}" class="footnotetext"', aside)
for elem in elems:
if type(elem) is elements.Paragraph:
div.add(Tag("p", htmlformat(elem.content)))
elif type(elem) is elements.Code:
lines = [htmlformat(l, False) for l in elem.lines]
div.add(Tag("pre", lines))
else:
raise ValueError(
f"Unknown type in footnotes: {elem.__class__.__name__}")
return div
def eatfootnotes(root, footnotes):
'''eatfootnotes(root, footnotes): Eat footnotes.
Turn a footnote dict into HTML tags.'''
for footnote in sorted(footnotes.keys()):
root.add(footnotetohtml(footnote, footnotes[footnote]))
def eatStructuredPage(root, page, donefootnotes):
'''eatStructuredPage(root, page, donefootnotes): Eat a StructuredPage.
Parameters:
- root: Tag, the tag to which generated tags will be added
- page: StructuredPage, the page to turn to HTML tags
- donefootnotes: set of int, the set of footnotes that have already been
dumped, and should not be dumped again
Read all the elements of the page and turn them into HTML tags.
This function adds entries to donefootnotes.
'''
tagstack = list()
key = None # the last key seen
if isinstance(page, pages.CoverPage):
root.add(Tag("h1", page.title))
todumpfootnotes = set()
def dumpfootnotes():
nonlocal tagstack, donefootnotes, todumpfootnotes
for footnote in sorted(todumpfootnotes - donefootnotes):
root.add(footnotetohtml(footnote, page.footnotes[footnote]))
tagstack = []
donefootnotes.update(todumpfootnotes)
todumpfootnotes = set()
for i, elem in enumerate(page.elements):
if isinstance(elem, elements.Text) or isinstance(elem, elements.Code):
todumpfootnotes.update(elem.footnotes)
if type(elem) is elements.Paragraph:
# If:
# paragraph is sandwidched between unorderedlistitems
# and the previous is deeper that the next
if (tagstack
and i < len(page.elements) - 1
and tagstack[-1].tag == "li"
and (type(page.elements[i - 1])
== type(page.elements[i + 1])
== elements.UnorderedListItem)
and (page.elements[i - 1].level
> page.elements[i + 1].level)):
# Paragraph belongs to the parent li of the previous li
tagstack.pop() # pop li, top is ul
tagstack.pop() # pop ul, top is li
p = Tag("p", htmlformat(elem.content))
tagstack[-1].contents.append(p)
tagstack.append(p)
continue
p = Tag("p", htmlformat(elem.content))
tagstack = [p]
root.add(p)
continue
if type(elem) is elements.NumberedParagraph:
parkey = f"{key}.p{elem.number}"
p = Tag(f'p id="{parkey}"')
aside = Tag("aside", f'<a href="#{parkey}">{elem.number}</a>')
p.contents.append(aside)
p.contents.append(htmlformat(elem.content))
tagstack = [p]
root.add(p)
continue
if type(elem) in (elements.NoteParagraph, elements.NoteNumberParagraph,
elements.ExampleParagraph,
elements.ExampleNumberParagraph):
if type(elem) is elements.NoteParagraph:
start = "note"
number = None
if type(elem) is elements.NoteNumberParagraph:
start = "note"
number = elem.notenumber
if type(elem) is elements.ExampleParagraph:
start = "example"
number = None
if type(elem) is elements.ExampleNumberParagraph:
start = "example"
number = elem.examplenumber
parkey = f"{key}.p{elem.number}"
p = Tag(f'p id="{parkey}"')
aside = Tag("aside", f'<a href="#{parkey}">{elem.number}</a>')
p.contents.append(aside)
startcontent = start.upper()
if number is not None:
startcontent += ' ' + str(number)
starttag = Tag(f'span class="{start}start"', startcontent)
p.contents.append(starttag)
if elem.content:
content = Tag(f'span class="{start}"', htmlformat(elem.content))
p.contents.append(content)
tagstack = [p]
root.add(p)
continue
if type(elem) is elements.NoteToEntryParagraph:
parkey = f"{key}.p{elem.number}"
p = Tag(f'p id="{parkey}"')
aside = Tag("aside", f'<a href="#{parkey}">{elem.number}</a>')
starttag = Tag('span class="notestart"',
f"Note {elem.notenumber} to entry:")
content = Tag('span class="note"', htmlformat(elem.content))
p.contents.append(aside)
p.contents.append(starttag)
p.contents.append(content)
tagstack = [p]
root.add(p)
continue
if type(elem) is elements.UnorderedListItem:
depthli = elem.level * 2 - 1
depthul = depthli - 1
# 4 cases:
# - unordered list already exists at this level, add to it
# - unordered list exists at the previous level, create nested
# list
# - definition exists at the previous level, create nested list
# - create unordered list at root level
if len(tagstack) > depthul and tagstack[depthul].tag == "ul":
# unordered list already exists at this level, add to it
li = Tag("li", htmlformat(elem.content))
tagstack[depthul].add(li)
# pop everything at depthli and above, push li
tagstack[depthli:] = [li]
continue
if (depthul - 2 >= 0
and len(tagstack) > depthul - 1
and tagstack[depthul - 2].tag == "ul"
and tagstack[depthul - 1].tag == "li"):
# unordered list exists at the previous level, create nested
# list
li = Tag("li", htmlformat(elem.content))
ul = Tag("ul", li)
tagstack[depthul - 1].add(ul)
# pop everything at depthul and above, push ul, push li
tagstack[depthul:] = [ul, li]
continue
if (depthul - 2 >= 0
and len(tagstack) > depthul - 1
and tagstack[depthul - 2].tag == "dl"
and tagstack[depthul - 1].tag == "dd"):
# unordered list exists at the previous level, create nested
# list
li = Tag("li", htmlformat(elem.content))
ul = Tag("ul", li)
tagstack[depthul - 1].add(ul)
# pop everything at depthul and above, push ul, push li
tagstack[depthul:] = [ul, li]
continue
if depthul == 0:
# create unordered list at root level
li = Tag("li", htmlformat(elem.content))
ul = Tag("ul", li)
tagstack = [ul, li]
root.add(ul)
continue
print("Invalid UnorderedListItem depth:", elem.level,
file=sys.stderr)
print(elem, file=sys.stderr)
print("tagstack:", [t.tag for t in tagstack], file=sys.stderr)
print(repr(root.contents[-1]), file=sys.stderr)
raise RuntimeError
if type(elem) is elements.OrderedListItem:
if len(tagstack) >= 2 and tagstack[0].tag == 'ol':
if tagstack[1].number + 1 != elem.number:
print("Non-consecutive OrderedListItems:",
tagstack[1].number, "and", elem.number,
file=sys.stderr)
print(elem, file=sys.stderr)
print(repr(root), file=sys.stderr)
print("tagstack:", [t.tag for t in tagstack],
file=sys.stderr)
raise RuntimeError
tagstack[1:] = [] # pop everything above ol, top is ol
li = Tag("li", htmlformat(elem.content))
li.number = elem.number
tagstack[0].add(li)
tagstack.append(li)
continue
if elem.number != 1:
print("Ordered list starting at", elem.number, file=sys.stderr)
print(elem, file=sys.stderr)
raise RuntimeError
li = Tag("li", elem.content)
li.number = 1
ol = Tag("ol", li)
tagstack = [ol, li]
root.add(ol)
continue
if type(elem) is elements.TitleHeading:
dumpfootnotes()
if elem.content[:6] == "Annex ":
key = elem.content[6:]
else:
key = elem.content
h1 = Tag(f'h1 id="{key}"', f'<a href="#{key}">{elem.content}</a>')
root.add(h1)
continue
if type(elem) is elements.NumberedHeading:
dumpfootnotes()
level = elem.key.count('.') + 1
key = elem.key
h = Tag(f'h{level} id="{key}"', f'<a href="#{key}">{key}</a>')
root.add(h)
continue
if type(elem) is elements.NumberedTitleHeading:
dumpfootnotes()
key = elem.key
if key[-1] == '.':
key = key[:-1]
level = key.count('.') + 1
h = Tag(f'h{level} id="{key}"', f'<a href="#{key}">{elem.key} '
f'{htmlformat(elem.content)}</a>')
root.add(h)
continue
if type(elem) is elements.Code:
lines = [htmlformat(l, False) for l in elem.lines]
if tagstack and tagstack[-1].tag == 'li':
pre = Tag("pre", lines)
tagstack[-1].add(pre)
continue
pre = Tag("pre", lines)
tagstack = list()
root.add(pre)
continue
if type(elem) is elements.NumberedCode:
parkey = f"{key}.p{elem.number}"
aside = Tag("aside", f'<a href="#{parkey}">{elem.number}</a>')
lines = [htmlformat(l, False) for l in elem.lines]
pre = Tag(f'pre id="#{parkey}"', lines)
div = Tag("div", aside)
div.add(pre)
tagstack = list()
root.add(div)
continue
if type(elem) is elements.ValueDefinition:
dt = Tag("dt", htmlformat(elem.value))
dd = Tag("dd", htmlformat(elem.content))
if tagstack and tagstack[0].tag == "dl":
tagstack[0].add(dt)
tagstack[0].add(dd)
continue
dl = Tag("dl", dt)
dl.add(dd)
tagstack = [dl, dd]
root.add(dl)
continue
raise ValueError(
f"Unknown type of element: {elem.__class__.__name__}")
dumpfootnotes()
def eatAbstract(root, abstract):
'''eatAbstract(root, abstract): Eat an Abstract.
Read all the elements of the page and turn them into HTML tags.'''
title = htmlformat(abstract.titleline.strip())
root.add(Tag(f'h1 id="{title}"', f'<a href="#{title}">{title}</a>'))
root.add(Tag("p", htmlformat(abstract.noteline.strip())))
for elem in abstract.elements:
root.add(Tag("p", htmlformat(elem.content)))
def eatTOC(root, t):
'''eatTOC(root, t): Eat a TOC.
Turn a toc.TOC into HTML tags.'''
title = htmlformat(t.titleline.strip())
root.add(Tag(f'h1 id="{title}"', f'<a href="#{title}">{title}</a>'))
main = Tag("ul")
root.add(main)
# list of tuple (str, Tag), the hierachical stack of ul Tags. A key with
# n dots is a child of levels[n][1]
levels = [(None, main)]
lastli = None
for title, key in t.titles:
if title[:6] == "Annex ":
key = title[6]
li = Tag("li", f'<a href="#{key}">{title}</a>')
lastli = li
main.add(li)
levels[1:] = []
elif key is None:
li = Tag("li", f'<a href="#{title}">{title}</a>')
lastli = None
main.add(li)
levels[1:] = []
else:
level = key.count('.')
li = Tag("li", f'{key} <a href="#{key}">{htmlformat(title)}</a>')
if level == len(levels):
# one deeper than the last
ul = Tag("ul", li)
lastli.add(ul)
lastkey = lastli.contents[0].split(maxsplit=1)[0]
levels.append((lastkey, ul))
lastli = li
elif level < len(levels):
# same level or higher that last
levels[level + 1:] = []
levels[level][1].add(li)
lastli = li
else:
print("Invalid TOC hierarchy", file=sys.stderr)
print("Entry:", key, title, file=sys.stderr)
print("Levels len:", len(levels), file=sys.stderr)
raise ValueError