epub-generator 0.0.2__tar.gz → 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. epub_generator-0.1.0/PKG-INFO +497 -0
  2. epub_generator-0.1.0/README.md +468 -0
  3. epub_generator-0.1.0/epub_generator/__init__.py +39 -0
  4. epub_generator-0.1.0/epub_generator/context.py +141 -0
  5. epub_generator-0.1.0/epub_generator/data/container.xml.jinja +6 -0
  6. epub_generator-0.1.0/epub_generator/data/content.opf.jinja +68 -0
  7. epub_generator-0.1.0/epub_generator/data/cover.xhtml.jinja +16 -0
  8. epub_generator-0.1.0/epub_generator/data/mimetype.jinja +1 -0
  9. epub_generator-0.1.0/epub_generator/data/nav.xhtml.jinja +43 -0
  10. epub_generator-0.1.0/epub_generator/data/part.xhtml.jinja +25 -0
  11. epub_generator-0.1.0/epub_generator/data/style.css.jinja +56 -0
  12. epub_generator-0.1.0/epub_generator/generation/__init__.py +1 -0
  13. epub_generator-0.1.0/epub_generator/generation/gen_asset.py +142 -0
  14. epub_generator-0.1.0/epub_generator/generation/gen_chapter.py +150 -0
  15. epub_generator-0.1.0/epub_generator/generation/gen_epub.py +173 -0
  16. epub_generator-0.1.0/epub_generator/generation/gen_nav.py +92 -0
  17. epub_generator-0.1.0/epub_generator/generation/gen_toc.py +88 -0
  18. epub_generator-0.1.0/epub_generator/generation/xml_utils.py +18 -0
  19. epub_generator-0.1.0/epub_generator/i18n.py +19 -0
  20. epub_generator-0.1.0/epub_generator/options.py +12 -0
  21. epub_generator-0.1.0/epub_generator/template.py +52 -0
  22. epub_generator-0.1.0/epub_generator/types.py +142 -0
  23. epub_generator-0.1.0/pyproject.toml +38 -0
  24. epub_generator-0.0.2/PKG-INFO +0 -19
  25. epub_generator-0.0.2/README.md +0 -1
  26. epub_generator-0.0.2/epub_generator/__init__.py +0 -4
  27. epub_generator-0.0.2/epub_generator/context.py +0 -90
  28. epub_generator-0.0.2/epub_generator/gen_asset.py +0 -133
  29. epub_generator-0.0.2/epub_generator/gen_epub.py +0 -178
  30. epub_generator-0.0.2/epub_generator/gen_index.py +0 -161
  31. epub_generator-0.0.2/epub_generator/gen_part.py +0 -126
  32. epub_generator-0.0.2/epub_generator/hash.py +0 -7
  33. epub_generator-0.0.2/epub_generator/i18n.py +0 -13
  34. epub_generator-0.0.2/epub_generator/template.py +0 -50
  35. epub_generator-0.0.2/epub_generator/types.py +0 -11
  36. epub_generator-0.0.2/pyproject.toml +0 -20
  37. {epub_generator-0.0.2 → epub_generator-0.1.0}/LICENSE +0 -0
@@ -0,0 +1,497 @@
1
+ Metadata-Version: 2.4
2
+ Name: epub-generator
3
+ Version: 0.1.0
4
+ Summary: A simple Python EPUB 3.0 generator with a single API call
5
+ License: MIT
6
+ License-File: LICENSE
7
+ Keywords: epub,epub3,ebook,generator,publishing
8
+ Author: Tao Zeyu
9
+ Author-email: i@taozeyu.com
10
+ Requires-Python: >=3.10,<3.14
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
+ Classifier: Topic :: Text Processing :: Markup :: HTML
21
+ Classifier: Topic :: Text Processing :: Markup :: XML
22
+ Requires-Dist: jinja2 (>=3.1.5,<4.0.0)
23
+ Requires-Dist: latex2mathml (>=3.77.0,<4.0.0)
24
+ Requires-Dist: matplotlib (>=3.10.1,<3.11.0)
25
+ Project-URL: Homepage, https://github.com/moskize91/epub-generator
26
+ Project-URL: Repository, https://github.com/moskize91/epub-generator
27
+ Description-Content-Type: text/markdown
28
+
29
+ <div align="center">
30
+ <h1>EPUB Generator</h1>
31
+ <p>A simple Python EPUB 3.0 generator with a single API call.</p>
32
+ <p>
33
+ <a href="https://github.com/moskize91/epub-generator/actions/workflows/build.yml" target="_blank"><img src="https://img.shields.io/github/actions/workflow/status/moskize91/epub-generator/build.yml" alt="ci" /></a>
34
+ <a href="https://pypi.org/project/epub-generator/" target="_blank"><img src="https://img.shields.io/badge/pip_install-epub--generator-blue" alt="pip install epub-generator" /></a>
35
+ <a href="https://pypi.org/project/epub-generator/" target="_blank"><img src="https://img.shields.io/pypi/v/epub-generator.svg" alt="pypi epub-generator" /></a>
36
+ <a href="https://pypi.org/project/epub-generator/" target="_blank"><img src="https://img.shields.io/pypi/pyversions/epub-generator.svg" alt="python versions" /></a>
37
+ <a href="https://github.com/moskize91/epub-generator/blob/main/LICENSE" target="_blank"><img src="https://img.shields.io/github/license/moskize91/epub-generator" alt="license" /></a>
38
+ </p>
39
+ </div>
40
+
41
+ ## Quick Start
42
+
43
+ ### Installation
44
+
45
+ ```bash
46
+ pip install epub-generator
47
+ ```
48
+
49
+ ### Generate Your First Book in 5 Minutes
50
+
51
+ ```python
52
+ from epub_generator import generate_epub, EpubData, BookMeta, TocItem, Chapter, Text, TextKind
53
+
54
+ # Prepare book data
55
+ epub_data = EpubData(
56
+ meta=BookMeta(
57
+ title="My First Book",
58
+ authors=["John Doe"],
59
+ ),
60
+ chapters=[
61
+ TocItem(
62
+ title="Chapter 1",
63
+ get_chapter=lambda: Chapter(
64
+ elements=[
65
+ Text(kind=TextKind.HEADLINE, content=["Chapter 1"]),
66
+ Text(kind=TextKind.BODY, content=["This is the first paragraph."]),
67
+ Text(kind=TextKind.BODY, content=["This is the second paragraph."]),
68
+ ]
69
+ ),
70
+ ),
71
+ ],
72
+ )
73
+
74
+ # Generate EPUB file
75
+ generate_epub(epub_data, "my_book.epub")
76
+ ```
77
+
78
+ That's it! You now have a valid EPUB 3.0 ebook file.
79
+
80
+ ## Features
81
+
82
+ - **Minimal API**: Just one function call `generate_epub()`
83
+ - **EPUB 3.0**: Generates standards-compliant EPUB 3.0 format
84
+ - **Rich Content**: Supports text, images, tables, math formulas, footnotes
85
+ - **Flexible Structure**: Nested chapters, prefaces, cover images
86
+ - **Math Support**: LaTeX to MathML conversion
87
+ - **Type Safe**: Full type annotations included
88
+
89
+ ## Advanced Usage
90
+
91
+ ### Add Cover and Complete Metadata
92
+
93
+ ```python
94
+ from datetime import datetime, timezone
95
+ from pathlib import Path
96
+ from epub_generator import generate_epub, EpubData, BookMeta, TocItem, Chapter, Text, TextKind
97
+
98
+ epub_data = EpubData(
99
+ meta=BookMeta(
100
+ title="Complete Example",
101
+ description="A book with full metadata",
102
+ publisher="Example Publisher",
103
+ isbn="978-0-123456-78-9",
104
+ authors=["Author One", "Author Two"],
105
+ editors=["Editor"],
106
+ translators=["Translator"],
107
+ modified=datetime.now(timezone.utc),
108
+ ),
109
+ cover_image_path=Path("cover.png"), # Cover image
110
+ chapters=[
111
+ TocItem(
112
+ title="Chapter 1",
113
+ get_chapter=lambda: Chapter(
114
+ elements=[
115
+ Text(kind=TextKind.HEADLINE, content=["Chapter 1"]),
116
+ Text(kind=TextKind.BODY, content=["Main content..."]),
117
+ ]
118
+ ),
119
+ ),
120
+ ],
121
+ )
122
+
123
+ generate_epub(epub_data, "book_with_cover.epub")
124
+ ```
125
+
126
+ ### Nested Chapter Structure
127
+
128
+ ```python
129
+ from epub_generator import generate_epub, EpubData, TocItem, Chapter, Text, TextKind
130
+
131
+ epub_data = EpubData(
132
+ chapters=[
133
+ TocItem(
134
+ title="Part 1",
135
+ children=[ # Nested sub-chapters
136
+ TocItem(
137
+ title="Chapter 1.1",
138
+ get_chapter=lambda: Chapter(
139
+ elements=[
140
+ Text(kind=TextKind.BODY, content=["Content 1.1..."]),
141
+ ]
142
+ ),
143
+ ),
144
+ TocItem(
145
+ title="Chapter 1.2",
146
+ get_chapter=lambda: Chapter(
147
+ elements=[
148
+ Text(kind=TextKind.BODY, content=["Content 1.2..."]),
149
+ ]
150
+ ),
151
+ ),
152
+ ],
153
+ ),
154
+ ],
155
+ )
156
+
157
+ generate_epub(epub_data, "book_with_nested_chapters.epub")
158
+ ```
159
+
160
+ ### Add Images
161
+
162
+ ```python
163
+ from pathlib import Path
164
+ from epub_generator import generate_epub, EpubData, TocItem, Chapter, Text, TextKind, Image
165
+
166
+ epub_data = EpubData(
167
+ chapters=[
168
+ TocItem(
169
+ title="Chapter 1",
170
+ get_chapter=lambda: Chapter(
171
+ elements=[
172
+ Text(kind=TextKind.BODY, content=["Here's an image:"]),
173
+ Image(
174
+ path=Path("image.png"), # Image path
175
+ alt_text="Image description",
176
+ ),
177
+ ]
178
+ ),
179
+ ),
180
+ ],
181
+ )
182
+
183
+ generate_epub(epub_data, "book_with_images.epub")
184
+ ```
185
+
186
+ ### Add Footnotes
187
+
188
+ ```python
189
+ from epub_generator import generate_epub, EpubData, TocItem, Chapter, Text, TextKind, Mark, Footnote
190
+
191
+ epub_data = EpubData(
192
+ chapters=[
193
+ TocItem(
194
+ title="Chapter 1",
195
+ get_chapter=lambda: Chapter(
196
+ elements=[
197
+ Text(
198
+ kind=TextKind.BODY,
199
+ content=[
200
+ "This is text with a footnote",
201
+ Mark(id=1), # Footnote marker
202
+ ".",
203
+ ],
204
+ ),
205
+ ],
206
+ footnotes=[
207
+ Footnote(
208
+ id=1,
209
+ contents=[
210
+ Text(kind=TextKind.BODY, content=["This is the footnote content."]),
211
+ ],
212
+ ),
213
+ ],
214
+ ),
215
+ ),
216
+ ],
217
+ )
218
+
219
+ generate_epub(epub_data, "book_with_footnotes.epub")
220
+ ```
221
+
222
+ ### Add Tables
223
+
224
+ ```python
225
+ from epub_generator import generate_epub, EpubData, TocItem, Chapter, Text, TextKind, Table
226
+
227
+ epub_data = EpubData(
228
+ chapters=[
229
+ TocItem(
230
+ title="Chapter 1",
231
+ get_chapter=lambda: Chapter(
232
+ elements=[
233
+ Text(kind=TextKind.BODY, content=["Here's a table:"]),
234
+ Table(
235
+ html_content="""
236
+ <table>
237
+ <tr><th>Name</th><th>Age</th></tr>
238
+ <tr><td>Alice</td><td>25</td></tr>
239
+ <tr><td>Bob</td><td>30</td></tr>
240
+ </table>
241
+ """
242
+ ),
243
+ ]
244
+ ),
245
+ ),
246
+ ],
247
+ )
248
+
249
+ generate_epub(epub_data, "book_with_tables.epub")
250
+ ```
251
+
252
+ ### Add Math Formulas
253
+
254
+ ```python
255
+ from epub_generator import generate_epub, EpubData, TocItem, Chapter, Text, TextKind, Formula, LaTeXRender
256
+
257
+ epub_data = EpubData(
258
+ chapters=[
259
+ TocItem(
260
+ title="Chapter 1",
261
+ get_chapter=lambda: Chapter(
262
+ elements=[
263
+ Text(kind=TextKind.BODY, content=["Pythagorean theorem:"]),
264
+ Formula(latex_expression="x^2 + y^2 = z^2"), # LaTeX expression
265
+ ]
266
+ ),
267
+ ),
268
+ ],
269
+ )
270
+
271
+ # Render math formulas using MathML
272
+ generate_epub(epub_data, "book_with_math.epub", latex_render=LaTeXRender.MATHML)
273
+ ```
274
+
275
+ ### Add Prefaces
276
+
277
+ ```python
278
+ from epub_generator import generate_epub, EpubData, BookMeta, TocItem, Chapter, Text, TextKind
279
+
280
+ epub_data = EpubData(
281
+ meta=BookMeta(title="Book with Prefaces"),
282
+ prefaces=[ # Preface chapters
283
+ TocItem(
284
+ title="Preface",
285
+ get_chapter=lambda: Chapter(
286
+ elements=[
287
+ Text(kind=TextKind.HEADLINE, content=["Preface"]),
288
+ Text(kind=TextKind.BODY, content=["This is the preface content..."]),
289
+ ]
290
+ ),
291
+ ),
292
+ ],
293
+ chapters=[ # Main chapters
294
+ TocItem(
295
+ title="Chapter 1",
296
+ get_chapter=lambda: Chapter(
297
+ elements=[
298
+ Text(kind=TextKind.BODY, content=["Main content..."]),
299
+ ]
300
+ ),
301
+ ),
302
+ ],
303
+ )
304
+
305
+ generate_epub(epub_data, "book_with_prefaces.epub")
306
+ ```
307
+
308
+ ## API Reference
309
+
310
+ ### Core Function
311
+
312
+ #### `generate_epub()`
313
+
314
+ ```python
315
+ def generate_epub(
316
+ epub_data: EpubData,
317
+ epub_file_path: PathLike,
318
+ lan: Literal["zh", "en"] = "zh",
319
+ table_render: TableRender = TableRender.HTML,
320
+ latex_render: LaTeXRender = LaTeXRender.MATHML,
321
+ ) -> None:
322
+ ```
323
+
324
+ **Parameters:**
325
+ - `epub_data`: Complete ebook data
326
+ - `epub_file_path`: Output file path
327
+ - `lan`: Language (`"zh"` or `"en"`, defaults to `"zh"`)
328
+ - `table_render`: Table rendering mode (defaults to `TableRender.HTML`)
329
+ - `latex_render`: Math formula rendering mode (defaults to `LaTeXRender.MATHML`)
330
+
331
+ ### Data Types
332
+
333
+ #### `EpubData`
334
+
335
+ Complete EPUB book data structure.
336
+
337
+ ```python
338
+ @dataclass
339
+ class EpubData:
340
+ meta: BookMeta | None = None # Book metadata
341
+ get_head: ChapterGetter | None = None # Head chapter without TOC entry
342
+ prefaces: list[TocItem] = [] # Preface chapters
343
+ chapters: list[TocItem] = [] # Main chapters
344
+ cover_image_path: Path | None = None # Cover image path
345
+ ```
346
+
347
+ #### `BookMeta`
348
+
349
+ Book metadata information.
350
+
351
+ ```python
352
+ @dataclass
353
+ class BookMeta:
354
+ title: str | None = None # Book title
355
+ description: str | None = None # Description
356
+ publisher: str | None = None # Publisher
357
+ isbn: str | None = None # ISBN
358
+ authors: list[str] = [] # Authors list
359
+ editors: list[str] = [] # Editors list
360
+ translators: list[str] = [] # Translators list
361
+ modified: datetime | None = None # Modification timestamp
362
+ ```
363
+
364
+ #### `TocItem`
365
+
366
+ Table of contents item with title, content, and optional nested children.
367
+
368
+ ```python
369
+ @dataclass
370
+ class TocItem:
371
+ title: str # Chapter title
372
+ get_chapter: ChapterGetter | None = None # Chapter content getter
373
+ children: list[TocItem] = [] # Nested sub-chapters
374
+ ```
375
+
376
+ #### `Chapter`
377
+
378
+ Complete content of a single chapter.
379
+
380
+ ```python
381
+ @dataclass
382
+ class Chapter:
383
+ elements: list[ContentBlock] = [] # Main content blocks
384
+ footnotes: list[Footnote] = [] # Footnotes
385
+ ```
386
+
387
+ #### Content Block Types
388
+
389
+ `ContentBlock` is a union of:
390
+
391
+ - **`Text`**: Text paragraph
392
+ ```python
393
+ @dataclass
394
+ class Text:
395
+ kind: TextKind # BODY | HEADLINE | QUOTE
396
+ content: list[str | Mark] # Text content with optional marks
397
+ ```
398
+
399
+ - **`Image`**: Image reference
400
+ ```python
401
+ @dataclass
402
+ class Image:
403
+ path: Path # Image file path (absolute)
404
+ alt_text: str = "image" # Alt text
405
+ ```
406
+
407
+ - **`Table`**: HTML table
408
+ ```python
409
+ @dataclass
410
+ class Table:
411
+ html_content: str # HTML table markup
412
+ ```
413
+
414
+ - **`Formula`**: Mathematical formula
415
+ ```python
416
+ @dataclass
417
+ class Formula:
418
+ latex_expression: str # LaTeX expression
419
+ ```
420
+
421
+ #### `Footnote`
422
+
423
+ Footnote/citation.
424
+
425
+ ```python
426
+ @dataclass
427
+ class Footnote:
428
+ id: int # Footnote ID
429
+ has_mark: bool = True # Whether contains mark indicator
430
+ contents: list[ContentBlock] = [] # Content blocks
431
+ ```
432
+
433
+ #### `Mark`
434
+
435
+ Footnote reference marker.
436
+
437
+ ```python
438
+ @dataclass
439
+ class Mark:
440
+ id: int # Reference ID, matches Footnote.id
441
+ ```
442
+
443
+ ### Configuration Options
444
+
445
+ #### `TextKind`
446
+
447
+ Text type enumeration.
448
+
449
+ ```python
450
+ class TextKind(Enum):
451
+ BODY = "body" # Regular paragraph
452
+ HEADLINE = "headline" # Chapter heading
453
+ QUOTE = "quote" # Quoted text
454
+ ```
455
+
456
+ #### `TableRender`
457
+
458
+ Table rendering mode.
459
+
460
+ ```python
461
+ class TableRender(Enum):
462
+ HTML = "html" # HTML rendering
463
+ ```
464
+
465
+ #### `LaTeXRender`
466
+
467
+ Math formula rendering mode.
468
+
469
+ ```python
470
+ class LaTeXRender(Enum):
471
+ MATHML = "mathml" # MathML rendering
472
+ ```
473
+
474
+ ## Development
475
+
476
+ ### Install Development Dependencies
477
+
478
+ ```bash
479
+ poetry install
480
+ ```
481
+
482
+ ### Run Tests
483
+
484
+ ```bash
485
+ python -m pytest tests/
486
+ ```
487
+
488
+ Requires [epubcheck](https://github.com/w3c/epubcheck) to validate generated EPUB files.
489
+
490
+ ## License
491
+
492
+ MIT License
493
+
494
+ ## Author
495
+
496
+ Tao Zeyu <i@taozeyu.com>
497
+