MCPcopy
hub / github.com/AstrBotDevs/AstrBot / _make_epub_bytes

Function _make_epub_bytes

tests/test_computer_fs_tools.py:205–293  ·  view source on GitHub ↗
(*, chapter_count: int = 1)

Source from the content-addressed store, hash-verified

203
204
205def _make_epub_bytes(*, chapter_count: int = 1) -> bytes:
206 manifest_items = [
207 '<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>'
208 ]
209 spine_items = ['<itemref idref="nav"/>']
210 nav_links = []
211
212 buffer = io.BytesIO()
213 with zipfile.ZipFile(buffer, mode="w") as archive:
214 archive.writestr(
215 "mimetype",
216 "application/epub+zip",
217 compress_type=zipfile.ZIP_STORED,
218 )
219 archive.writestr(
220 "META-INF/container.xml",
221 """<?xml version="1.0" encoding="UTF-8"?>
222<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
223 <rootfiles>
224 <rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
225 </rootfiles>
226</container>
227""",
228 )
229
230 for index in range(1, chapter_count + 1):
231 manifest_items.append(
232 f'<item id="chapter{index}" href="chapter{index}.xhtml" '
233 'media-type="application/xhtml+xml"/>'
234 )
235 spine_items.append(f'<itemref idref="chapter{index}"/>')
236 nav_links.append(
237 f'<li><a href="chapter{index}.xhtml">Chapter {index}</a></li>'
238 )
239 archive.writestr(
240 f"OEBPS/chapter{index}.xhtml",
241 f"""<?xml version="1.0" encoding="utf-8"?>
242<html xmlns="http://www.w3.org/1999/xhtml">
243 <head>
244 <title>Chapter {index}</title>
245 </head>
246 <body>
247 <h1>Chapter {index}</h1>
248 <p>Paragraph {index}</p>
249 </body>
250</html>
251""",
252 )
253
254 archive.writestr(
255 "OEBPS/nav.xhtml",
256 """<?xml version="1.0" encoding="utf-8"?>
257<html xmlns="http://www.w3.org/1999/xhtml">
258 <head>
259 <title>Navigation</title>
260 </head>
261 <body>
262 <nav epub:type="toc" xmlns:epub="http://www.idpf.org/2007/ops">

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected