-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelements.py
More file actions
316 lines (278 loc) · 10.4 KB
/
elements.py
File metadata and controls
316 lines (278 loc) · 10.4 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
'''Package elements:
Contains definitions about the various structuring elements that make up a page.
'''
import re
import sys
# Line-breaks in hyperlinks can happen after the colon or the double slash.
HTTPBREAKRE = re.compile("https?:(//)?$")
# hyphenated phrases we don't want to dehyphenate when undoing line breaks
NOSTITCHING = (
"60559-", # ISO/IEC 60559-specified
"bit-", # bit-precise
"const-", # const-qualified
"decimal-", # decimal-point
"derived-", # derived-declarator-type-list
"encoding-to-", # encoding-to-encoding
"end-of-", # end-of-file
"execution-", # execution-time
"floating-", # floating-point
"function-", # function-like
"half-", # half-revolutions
"implementation-", # implementation-defined
"little-", # little-endian
"locale-", # locale-specific
"new-", # new-line
"non-", # non-arithmetic, non-recursive, non-white-space
"null-", # null-terminated
"pointer-to-", # pointer-to-pointer
"real-", # real-floating
"runtime-", # runtime-constraints
"single-", # single-quotes
"storage-", # storage-class
"string-from-", # string-from-encoding
"type-", # type-generic
"va-opt-" # va-opt-replacement
)
class Text:
'''A text container.
Attributes:
- content: str, the text of the paragraph
- footnotes: set of int, the footnotes present in the content'''
def __init__(self, content):
self.content = content.strip()
self.footnotes = set()
def addcontent(self, content):
'''addcontent(self, content): Append to the contents.
Parameters:
- content: str, the text to append
If the current paragraph ends with a broken URL, the remainder of the
hyperlink is concatenated with the current paragraph, then a newline,
then the rest of content.
If the current paragraph ends with a hyphen-broken word, the remainder
of the word is stitched back with its beginning, then a newline, then
the rest of content. "Stitching" involves removing the hyphen, except
for the following hyphenated phrases:
- ISO/IEC 60559-specified
- bit-precise
- const-qualified
- decimal-point
- derived-declarator-type-list
- encoding-to-encoding
- end-of-file
- execution-time
- floating-point
- function-like
- half-revolutions
- implementation-defined
- little-endian
- locale-specific
- new-line
- non-arithmetic
- non-recursive
- non-white-space
- null-terminated
- pointer-to-pointer
- real-floating
- runtime-constraints
- single-quotes
- storage-class
- string-from-encoding
- type-generic
- va-opt-replacement
Otherwise, content is concatenated with a a newline.
'''
if not self.content:
self.content = content.strip()
return
# "newline": concatenate '\n' + content
# "first-word": concatenate first word of content + '\n' + rest of
# content
# "dehyphenate": like "first word", but remove a hyphen from
# self.content first
concatenation = "newline"
if HTTPBREAKRE.search(self.content):
concatenation = "first-word"
elif self.content[-1] == '-':
last_word = self.content.split()[-1]
if last_word.lower() in NOSTITCHING:
concatenation = "first-word"
else:
concatenation = "dehyphenate"
if concatenation == "newline":
self.content += '\n' + content.strip()
if concatenation == "dehyphenate":
self.content = self.content[:-1]
concatenation = "first-word"
if concatenation == "first-word":
content_list = content.split(maxsplit=1)
if len(content_list) == 1:
# Only one word of content -> just concatenate
self.content += content.strip()
else:
first_word, rest = content_list
self.content += first_word.strip() + '\n' + rest.strip()
def __str__(self):
return self.content.replace('\n', r"\n")
class Paragraph(Text):
'''Attributes:
- content: str, see Text'''
pass
class NumberedParagraph(Paragraph):
'''Attributes:
- number: int, the number associated to the paragraph
- content: str, see Text'''
def __init__(self, number, content):
self.number = number
Paragraph.__init__(self, content)
class NoteParagraph(NumberedParagraph):
'''Attributes:
- number: int, see NumberedParagraph
- content: str, see Text'''
def __str__(self):
return "NOTE " + super().__str__()
class NoteNumberParagraph(NoteParagraph):
'''Attributes:
- notenumber: int, the number associated to the note
- number: int, see NumberedParagraph
- content: str, see Text'''
def __init__(self, number, notenumber, content):
self.notenumber = notenumber
NoteParagraph.__init__(self, number, content)
def __str__(self):
return f"NOTE {self.notenumber} " + super().__str__()
class NoteToEntryParagraph(NoteNumberParagraph):
def __str__(self):
return f"Note {self.notenumber} to entry: " + super().__str__()
class ExampleParagraph(NumberedParagraph):
'''Attributes:
- number: int, see NumberedParagraph
- content: str, see Text'''
def __str__(self):
return "EXAMPLE " + super().__str__()
class ExampleNumberParagraph(ExampleParagraph):
'''Attributes:
- examplenumber: int, the number associated to the example
- number: int, see NumberedParagraph
- content: str, see Text'''
def __init__(self, number, examplenumber, content):
self.examplenumber = examplenumber
ExampleParagraph.__init__(self, number, content)
def __str__(self):
return f"EXAMPLE {self.examplenumber} " + super().__str__()
class UnorderedListItem(Text):
'''A member of an unordered list.
Attributes:
- level: int, 1 if the bullet is '—', 2 if the bullet is '•'
- indent: int, the number of spaces to expect at the beginning of
similar items
- see Text'''
def __init__(self, line):
try:
(bullet, content) = line.split(maxsplit=1)
except ValueError:
print("Could not split bullet", file=sys.stderr)
print(line, file=sys.stderr)
raise
else:
if bullet == '—':
self.level = 1
elif bullet == '•':
self.level = 2
else:
print("Unknown bullet:", bullet, file=sys.stderr)
print(line, file=sys.stderr)
raise NotImplementedError
self.indent = len(line) - len(line.lstrip())
Text.__init__(self, content)
def __repr__(self):
return " " * (self.level - 1) + self.content
class OrderedListItem(Text):
'''A member of an ordered list.
Attributes:
- number: int, the number of the item
- see Text'''
def __init__(self, line):
try:
(number, content) = line.split(maxsplit=1)
except ValueError:
print("Could not split line", file=sys.stderr)
print(line, file=sys.stderr)
raise
else:
try:
self.number = int(number[:-1])
except ValueError:
print("Could not parse ordered item", file=sys.stderr)
print(line, file=sys.stderr)
raise
else:
Text.__init__(self, content)
class TitleHeading:
'''A single standing title.
Attributes:
- content: str, the text of the heading'''
def __init__(self, content):
self.content = content.strip()
def __repr__(self):
return self.content
class NumberedHeading:
'''A subsection heading, numbered, without a title.
Attributes:
- key: str, the key of the subsection'''
def __init__(self, key):
self.key = key.strip()
def __repr__(self):
return self.key
class NumberedTitleHeading(NumberedHeading, TitleHeading):
'''Attributes:
- see NumberedHeading
- see TitleHeading'''
def __init__(self, line):
key, title = line.split(maxsplit=1)
NumberedHeading.__init__(self, key)
TitleHeading.__init__(self, title)
def __repr__(self):
return (NumberedHeading.__repr__(self)
+ ' '
+ TitleHeading.__repr__(self))
class Code:
'''A text container. The contents are not left-stripped.
Attributes:
- lines: list of str, the lines of content
- footnotes: set of int, the footnotes present in the content'''
def __init__(self, content):
self.lines = list()
self.footnotes = set()
self.addcontent(content)
def addcontent(self, content):
'''addcontent(self, content): Append to the contents.
Parameters:
- content: str, the text to append'''
self.lines.append(content.rstrip())
def __repr__(self):
return repr(self.lines)
class NumberedCode(Code):
'''Attributes:
- number: int, the number associated to the code block
- lines: list of str, see Code'''
def __init__(self, number, content):
self.number = number
Code.__init__(self, content)
class ValueDefinition(Text):
'''A value associated with a definition.
Attributes:
- value: str, the value being defined
- see Text'''
def __init__(self, value, content):
value = value.strip()
self.value = value[:-1] if value[-1] == ':' else value
Text.__init__(self, content)
def __str__(self):
return self.value + ":\t" + Text.__str__(self)
class NumberedValueDefinition(ValueDefinition):
'''Attributes:
- number: int, the number associated to the definition
- see ValueDefinition'''
def __init__(self, number, value, content):
self.number = number
ValueDefinition.__init__(self, value, content)