-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcontent.py
executable file
·287 lines (203 loc) · 7.21 KB
/
content.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
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
import numpy
from box import Box
import colors
class BoilerPlate:
def __init__(self):
self.pageNum = None
self.chapterTitle = None # only one of [.chapterTitle, .bookTitle] will be set -- they're mutually exclusive.
self.bookTitle = None
def paint(self, image, color):
image = self.pageNum.paint(image, color, box=True)
if self.chapterTitle is not None:
image = self.chapterTitle.paint(image, color, box=True)
if self.bookTitle is not None:
image = self.bookTitle.paint(image, color, box=True)
return image
class SectionTitle:
def __init__(self, firstLine=None):
self.contentType = "SectionTitle"
if firstLine is None:
self.lines = []
else:
self.lines = [firstLine]
def append(self, line):
self.lines.append(line)
def paint(self, image, color=colors.MAGENTA):
for line in self.lines:
image = line.paint(image, color, box=True)
return image
class Figure:
def __init__(self):
self.contentType = "Figure"
self.image = None
self.caption = []
def paint(self, image, color=colors.CYAN):
image = self.image.paint(image, color, box=True)
for line in self.caption:
image = line.paint(image, color, box=True)
return image
class Paragraph:
def __init__(self, firstLine=None):
self.contentType = "Paragraph"
if firstLine is None:
self.lines = []
else:
self.lines = [firstLine]
def append(self, line):
self.lines.append(line)
def __add__(self, other):
# designed to be used when adding Content()s together, so that paragraphs which are split over
# a page can be reconstituted.
pass
def __getitem__(self, val):
return self.lines.__getitem__(val)
def __len__(self):
return self.lines.__len__()
def paint(self, image, color=colors.RED):
points = []
for line in self.lines:
for word in line.words:
for point in word.contour:
points.append(point)
points = numpy.array(points) # This needs to have the format [ [[a,b]], [[c,d]] ]
box = Box(points)
image = box.paint(image, color)
for line in self.lines:
image = line.paint(image, colors.BURNT_YELLOW, centerLine=True)
return image
class ChapterStart:
def __init__(self, lines):
self.contentType = "ChapterStart"
self.chapterNum = lines.pull()
self.titleLines = []
self.quoteLines = []
while not lines.peekStart().isHorizontalRule:
self.titleLines.append(lines.pull())
lines.pull() # discard the <hr> line.
while not lines.peekStart().isHorizontalRule:
self.quoteLines.append(lines.pull())
lines.pull() # discard the <hr> line.
def paint(self, image, color=colors.ORANGE):
image = self.chapterNum.paint(image, color)
for line in self.titleLines:
image = line.paint(image, color, box=True)
for line in self.quoteLines:
image = line.paint(image, color, box=True)
return image
class Content:
def __init__(self, lines, isChapterStart=False):
self.lines = lines
self.content = []
if isChapterStart:
chapterStart = ChapterStart(self.lines)
self.content.append(chapterStart)
self.stateMachine()
def stateMachine(self):
try:
newLine = self.lines.pull()
except IndexError:
return
if newLine.box.height > 300:
self.SM_newFigure(newLine)
elif newLine.isCentered:
self.SM_sectionTitle(newLine)
else:
self.SM_newParagraph(newLine)
def SM_newFigure(self, line):
figure = Figure()
figure.image = line
try:
newLine = self.lines.pull()
except IndexError:
self.content.append(figure)
return
if newLine.isCentered:
self.SM_addCaptionLine(newLine, figure)
else:
self.content.append(figure)
self.SM_newParagraph(newLine)
def SM_addCaptionLine(self, line, figure):
figure.caption.append(line)
try:
newLine = self.lines.pull()
except IndexError:
self.content.append(figure)
return
if newLine.isCentered:
self.SM_addCaptionLine(newLine, figure)
else:
self.content.append(figure)
self.SM_newParagraph(newLine)
def SM_newParagraph(self, line):
paragraph = Paragraph(line)
try:
newLine = self.lines.pull()
except IndexError:
self.content.append(paragraph)
return
if newLine.box.height > 300:
self.content.append(paragraph)
self.SM_newFigure(newLine)
elif newLine.isCentered:
self.content.append(paragraph)
self.SM_sectionTitle(newLine)
elif line.isParagraphEnd or newLine.isParagraphStart:
self.content.append(paragraph)
self.SM_newParagraph(newLine)
elif newLine.isParagraphEnd:
self.SM_paragraphEnd(newLine, paragraph)
else:
self.SM_paragraphBody(newLine, paragraph)
def SM_paragraphBody(self, line, paragraph):
paragraph.append(line)
try:
newLine = self.lines.pull()
except IndexError:
self.content.append(paragraph)
return
if newLine.box.height > 300:
self.content.append(paragraph)
self.SM_newFigure(newLine)
elif newLine.isCentered:
self.content.append(paragraph)
self.SM_sectionTitle(newLine)
elif newLine.isParagraphStart:
self.content.append(paragraph)
self.SM_newParagraph(newLine)
elif newLine.isParagraphEnd:
self.SM_paragraphEnd(newLine, paragraph)
else:
self.SM_paragraphBody(newLine, paragraph)
def SM_paragraphEnd(self, line, paragraph):
paragraph.append(line)
try:
newLine = self.lines.pull()
except IndexError:
self.content.append(paragraph)
return
if newLine.box.height > 300:
self.content.append(paragraph)
self.SM_newFigure(newLine)
elif newLine.isCentered:
self.content.append(paragraph)
self.SM_sectionTitle(newLine)
else:
self.content.append(paragraph)
self.SM_newParagraph(newLine)
def SM_sectionTitle(self, line):
sectionTitle = SectionTitle(line)
try:
newLine = self.lines.pull()
except IndexError:
self.content.append(sectionTitle)
return
if newLine.box.height > 300:
self.content.append(sectionTitle)
self.SM_newFigure(newLine)
else:
self.content.append(sectionTitle)
self.SM_newParagraph(newLine)
def paint(self, image):
for item in self.content:
image = item.paint(image)
return image