-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
317 lines (279 loc) · 10.1 KB
/
generator.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
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
#############################################################################
# Copyright (C) 2024 CrowdWare
#
# This file is part of EbookCreator.
#
# EbookCreator is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# EbookCreator is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with EbookCreator. If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
import os
import uuid
import datetime
import shutil
import traceback
import sys
from tempfile import mkdtemp
from shutil import rmtree
from markdown2 import markdown
from markupsafe import Markup
from jinja2 import Template
from zipfile import ZipFile
from PySide6.QtCore import QCoreApplication
from xml.dom.minidom import parseString
def createEpub(output, book, win):
dir = mkdtemp()
guid = str(uuid.uuid4())
copyAssets(dir, book.theme)
os.mkdir(os.path.join(dir, "EPUB", "parts"))
os.mkdir(os.path.join(dir, "EPUB", "images"))
os.mkdir(os.path.join(dir, "META-INF"))
path = os.getcwd()
copyImages(dir, book)
writeMimetype(dir)
writeContainer(dir)
generatePackage(dir, book, guid)
toc = generateParts(dir, book)
generateToc(dir, book, toc)
generateNcx(dir, book, guid)
os.chdir(dir)
files = getAllFiles(dir)
with ZipFile(output, 'w') as zip:
for file in files:
zip.write(file)
os.chdir(path)
rmtree(dir)
win.statusBar().showMessage("Ready")
def getAllFiles(dir):
file_paths = []
for root, directories, files in os.walk(dir):
for filename in files:
if filename != ".DS_Store":
if root == dir:
filepath = filename
else:
filepath = os.path.join(root[len(dir) + 1:], filename)
file_paths.append(filepath)
return file_paths
def writeMimetype(dir):
with open(os.path.join(dir, "mimetype"), "w", encoding='utf-8') as f:
f.write("application/epub+zip")
def writeContainer(dir):
midir = os.path.join(dir, "META-INF")
with open(os.path.join(midir, "container.xml"), "w", encoding='utf-8') as f:
f.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
f.write("<container xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\" version=\"1.0\">")
f.write(" <rootfiles>")
f.write(" <rootfile full-path=\"EPUB/package.opf\" media-type=\"application/oebps-package+xml\"/>")
f.write(" </rootfiles>")
f.write("</container>")
def generatePackage(dir, book, uuid):
context = {}
context["uuid"] = uuid
context["lang"] = book.language
context["title"] = book.name
context["date"] = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
context["version"] = QCoreApplication.applicationVersion()
context["creator"] = book.creator
items = []
spine = []
for part in book._parts:
if not part.pdfOnly:
item = {}
name = part.name.replace(" ", "-").lower()
if name == "toc":
pass #only for PDF
else:
item["href"] = "parts/" + name + ".xhtml"
item["id"] = name
item["type"] = "application/xhtml+xml"
items.append(item)
spine.append(name)
for root, dirs, files in os.walk(os.path.join(dir, "EPUB", "images")):
for file in files:
filename, extension = os.path.splitext(file)
if filename != ".DS_Store":
item = {}
item["href"] = "images/" + file
item["id"] = filename +"_img"
item["type"] = "image/" + extension[1:]
items.append(item)
context["items"] = items
context["spine"] = spine
path = os.getcwd()
with open(os.path.join(path, "themes", book.theme, "layout", "package.opf"), "r", encoding='utf-8') as fp:
data = fp.read()
tmp = Template(data)
xml = tmp.render(context)
with open(os.path.join(dir, "EPUB", "package.opf"), "w", encoding='utf-8') as f:
f.write(xml)
def fixTables(text):
text = text.replace("<th align=\"center\"", "<th class=\"center\"")
text = text.replace("<th align=\"right\"", "<th class=\"right\"")
text = text.replace("<th align=\"left\"", "<th class=\"left\"")
text = text.replace("<td align=\"center\"", "<td class=\"center\"")
text = text.replace("<td align=\"right\"", "<td class=\"right\"")
text = text.replace("<td align=\"left\"", "<td class=\"left\"")
return text
def generateParts(dir, book):
toc = []
item = {}
item["href"] = "toc.xhtml"
if book.language == "de":
item["name"] = "Inhaltsverzeichnis"
else:
item["name"] = "Table of Contents"
item["id"] = "nav"
item["parts"] = []
toc.append(item)
path = os.getcwd()
for part in book._parts:
if not part.pdfOnly:
context = {}
with open(os.path.join(book.source_path, "parts", part.src), "r", encoding='utf-8') as i:
text = i.read()
name = part.name.replace(" ", "-").lower()
if name == "toc":
pass #only used to create PDF
else:
html = fixTables(markdown(text, html4tags = False, extras=["fenced-code-blocks", "wiki-tables", "tables", "header-ids"]))
list = getLinks(html, name)
for item in list:
toc.append(item)
context["content"] = html
with open(os.path.join(path, "themes", book.theme, "layout", "template.xhtml"), encoding='utf-8') as fp:
data = fp.read()
tmp = Template(data)
xhtml = tmp.render(context)
xhtml = addLineNumbers(xhtml)
with open(os.path.join(dir, "EPUB", "parts", name + ".xhtml"), "w", encoding='utf-8') as f:
f.write(xhtml)
return toc
def addLineNumbers(html):
pos = 0
ret = ""
end = 0
while True:
old_pos = pos
pos = html.find("<code>", pos + 1)
if pos < 0:
break
end = html.find("</code>", pos + 6)
inner = html[pos + 6:end - 1]
ret += html[old_pos:pos] + "<code>"
pos = end
line_no = 1
lines = inner.split("\n")
for line in lines:
if len(lines) > 1:
ret += "<span class=\"line-number\"><span>" + str(line_no) + "</span></span> " + line + "\n"
else:
ret += line + "\n"
line_no += 1
ret += html[end:]
return ret
def copyAssets(dir, theme):
path = os.getcwd()
shutil.copytree(os.path.join(path, "themes", theme, "assets"), os.path.join(dir, "EPUB"))
def copyImages(dir, book):
for root, dirs, files in os.walk(os.path.join(book.source_path, "images")):
for file in files:
shutil.copy(os.path.join(book.source_path, "images", file), os.path.join(dir, "EPUB", "images"))
def countHash(text):
count = 0
for letter in text:
if letter == "#":
count += 1
else:
break
return count
def getLinks(text, part_name):
nodes = []
list = []
for line in text.split("\n"):
if not line:
continue
if line.startswith("<h1 "):
c = 1
elif line.startswith("<h2 "):
c = 2
elif line.startswith("<h3 "):
c = 3
elif line.startswith("<h4 "):
c = 4
elif line.startswith("<h5 "):
c = 5
elif line.startswith("<h6 "):
c = 6
else:
c = 0
if c > 0:
start = line.find("id=")
end = line.find('"', start + 4)
id = line[start + 4:end]
start = line.find(">", end) + 1
end = line.find("<", start + 1)
name = line[start:end]
item = {}
item["href"] = part_name + ".xhtml#" + id
item["name"] = name
item["id"] = id
item["parts"] = []
if len(nodes) < c:
nodes.append(item)
else:
nodes[c - 1] = item
if c == 1:
list.append(item)
else:
nodes[c - 2]["parts"].append(item)
return list
def generateToc(dir, book, parts):
path = os.getcwd()
context = {}
context["parts"] = parts
with open(os.path.join(path, "themes", book.theme, "layout", "toc.xhtml"), "r", encoding='utf-8') as fp:
data = fp.read()
tmp = Template(data)
xhtml = tmp.render(context)
with open(os.path.join(dir, "EPUB", "parts", "toc.xhtml"), "w", encoding='utf-8') as f:
f.write(xhtml)
def generateNcx(dir, book, uuid):
items = []
context = {}
context["title"] = book.name
context["uuid"] = uuid
order = 0
for part in book._parts:
if not part.pdfOnly:
order += 1
item = {}
name = part.name.replace(" ", "-")
item["href"] = "parts/" + name.lower() + ".xhtml"
item["id"] = "navPoint-" + str(order)
item["name"] = name
item["order"] = str(order)
items.append(item)
context["items"] = items
if "epub2" in book.theme.lower():
try:
# only relevant for epub2
path = os.getcwd()
with open(os.path.join(path, "themes", book.theme, "layout", "toc.ncx"), encoding='utf-8') as fp:
data = fp.read()
tmp = Template(data)
xhtml = tmp.render(context)
with open(os.path.join(dir, "EPUB", "toc.ncx"), "w", encoding='utf-8') as f:
f.write(xhtml)
except:
traceback.print_exc(file=sys.stdout)