epub-generator 0.1.3__tar.gz → 0.1.5__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.
- {epub_generator-0.1.3 → epub_generator-0.1.5}/PKG-INFO +19 -15
- {epub_generator-0.1.3 → epub_generator-0.1.5}/README.md +18 -14
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/__init__.py +2 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/context.py +4 -5
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/data/nav.xhtml.jinja +1 -1
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/data/style.css.jinja +22 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/generation/gen_asset.py +87 -35
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/generation/gen_chapter.py +14 -71
- epub_generator-0.1.5/epub_generator/generation/gen_content.py +59 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/generation/gen_epub.py +43 -43
- epub_generator-0.1.5/epub_generator/generation/gen_nav.py +70 -0
- epub_generator-0.1.5/epub_generator/generation/gen_toc.py +105 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/generation/xml_utils.py +23 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/i18n.py +2 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/types.py +22 -9
- {epub_generator-0.1.3 → epub_generator-0.1.5}/pyproject.toml +1 -1
- epub_generator-0.1.3/epub_generator/generation/gen_nav.py +0 -92
- epub_generator-0.1.3/epub_generator/generation/gen_toc.py +0 -88
- {epub_generator-0.1.3 → epub_generator-0.1.5}/LICENSE +0 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/data/container.xml.jinja +0 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/data/content.opf.jinja +0 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/data/cover.xhtml.jinja +0 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/data/mimetype.jinja +0 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/data/part.xhtml.jinja +0 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/generation/__init__.py +0 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/html_tag.py +0 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/options.py +0 -0
- {epub_generator-0.1.3 → epub_generator-0.1.5}/epub_generator/template.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: epub-generator
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.5
|
|
4
4
|
Summary: A simple Python EPUB 3.0 generator with a single API call
|
|
5
5
|
License: MIT
|
|
6
6
|
Keywords: epub,epub3,ebook,generator,publishing
|
|
@@ -61,9 +61,9 @@ epub_data = EpubData(
|
|
|
61
61
|
title="Chapter 1",
|
|
62
62
|
get_chapter=lambda: Chapter(
|
|
63
63
|
elements=[
|
|
64
|
-
TextBlock(kind=TextKind.HEADLINE, content=["Chapter 1"]),
|
|
65
|
-
TextBlock(kind=TextKind.BODY, content=["This is the first paragraph."]),
|
|
66
|
-
TextBlock(kind=TextKind.BODY, content=["This is the second paragraph."]),
|
|
64
|
+
TextBlock(kind=TextKind.HEADLINE, level=0, content=["Chapter 1"]),
|
|
65
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["This is the first paragraph."]),
|
|
66
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["This is the second paragraph."]),
|
|
67
67
|
]
|
|
68
68
|
),
|
|
69
69
|
),
|
|
@@ -111,8 +111,8 @@ epub_data = EpubData(
|
|
|
111
111
|
title="Chapter 1",
|
|
112
112
|
get_chapter=lambda: Chapter(
|
|
113
113
|
elements=[
|
|
114
|
-
TextBlock(kind=TextKind.HEADLINE, content=["Chapter 1"]),
|
|
115
|
-
TextBlock(kind=TextKind.BODY, content=["Main content..."]),
|
|
114
|
+
TextBlock(kind=TextKind.HEADLINE, level=0, content=["Chapter 1"]),
|
|
115
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Main content..."]),
|
|
116
116
|
]
|
|
117
117
|
),
|
|
118
118
|
),
|
|
@@ -136,7 +136,7 @@ epub_data = EpubData(
|
|
|
136
136
|
title="Chapter 1.1",
|
|
137
137
|
get_chapter=lambda: Chapter(
|
|
138
138
|
elements=[
|
|
139
|
-
TextBlock(kind=TextKind.BODY, content=["Content 1.1..."]),
|
|
139
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Content 1.1..."]),
|
|
140
140
|
]
|
|
141
141
|
),
|
|
142
142
|
),
|
|
@@ -144,7 +144,7 @@ epub_data = EpubData(
|
|
|
144
144
|
title="Chapter 1.2",
|
|
145
145
|
get_chapter=lambda: Chapter(
|
|
146
146
|
elements=[
|
|
147
|
-
TextBlock(kind=TextKind.BODY, content=["Content 1.2..."]),
|
|
147
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Content 1.2..."]),
|
|
148
148
|
]
|
|
149
149
|
),
|
|
150
150
|
),
|
|
@@ -168,7 +168,7 @@ epub_data = EpubData(
|
|
|
168
168
|
title="Chapter 1",
|
|
169
169
|
get_chapter=lambda: Chapter(
|
|
170
170
|
elements=[
|
|
171
|
-
TextBlock(kind=TextKind.BODY, content=["Here's an image:"]),
|
|
171
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Here's an image:"]),
|
|
172
172
|
Image(
|
|
173
173
|
path=Path("image.png"), # Image path
|
|
174
174
|
alt_text="Image description",
|
|
@@ -195,6 +195,7 @@ epub_data = EpubData(
|
|
|
195
195
|
elements=[
|
|
196
196
|
TextBlock(
|
|
197
197
|
kind=TextKind.BODY,
|
|
198
|
+
level=0,
|
|
198
199
|
content=[
|
|
199
200
|
"This is text with a footnote",
|
|
200
201
|
Mark(id=1), # Footnote marker
|
|
@@ -206,7 +207,7 @@ epub_data = EpubData(
|
|
|
206
207
|
Footnote(
|
|
207
208
|
id=1,
|
|
208
209
|
contents=[
|
|
209
|
-
TextBlock(kind=TextKind.BODY, content=["This is the footnote content."]),
|
|
210
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["This is the footnote content."]),
|
|
210
211
|
],
|
|
211
212
|
),
|
|
212
213
|
],
|
|
@@ -229,7 +230,7 @@ epub_data = EpubData(
|
|
|
229
230
|
title="Chapter 1",
|
|
230
231
|
get_chapter=lambda: Chapter(
|
|
231
232
|
elements=[
|
|
232
|
-
TextBlock(kind=TextKind.BODY, content=["Here's a table:"]),
|
|
233
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Here's a table:"]),
|
|
233
234
|
Table(
|
|
234
235
|
html_content="""
|
|
235
236
|
<table>
|
|
@@ -261,7 +262,7 @@ epub_data = EpubData(
|
|
|
261
262
|
title="Chapter 1",
|
|
262
263
|
get_chapter=lambda: Chapter(
|
|
263
264
|
elements=[
|
|
264
|
-
TextBlock(kind=TextKind.BODY, content=["Pythagorean theorem:"]),
|
|
265
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Pythagorean theorem:"]),
|
|
265
266
|
Formula(latex_expression="x^2 + y^2 = z^2"), # Block-level formula
|
|
266
267
|
]
|
|
267
268
|
),
|
|
@@ -284,6 +285,7 @@ epub_data = EpubData(
|
|
|
284
285
|
elements=[
|
|
285
286
|
TextBlock(
|
|
286
287
|
kind=TextKind.BODY,
|
|
288
|
+
level=0,
|
|
287
289
|
content=[
|
|
288
290
|
"The Pythagorean theorem ",
|
|
289
291
|
Formula(latex_expression="a^2 + b^2 = c^2"), # Inline formula
|
|
@@ -314,6 +316,7 @@ epub_data = EpubData(
|
|
|
314
316
|
title="Chapter 1",
|
|
315
317
|
get_chapter=lambda: Chapter(elements=[TextBlock(
|
|
316
318
|
kind=TextKind.BODY,
|
|
319
|
+
level=0,
|
|
317
320
|
content=[
|
|
318
321
|
"This is normal text with ",
|
|
319
322
|
HTMLTag(
|
|
@@ -348,8 +351,8 @@ epub_data = EpubData(
|
|
|
348
351
|
title="Preface",
|
|
349
352
|
get_chapter=lambda: Chapter(
|
|
350
353
|
elements=[
|
|
351
|
-
TextBlock(kind=TextKind.HEADLINE, content=["Preface"]),
|
|
352
|
-
TextBlock(kind=TextKind.BODY, content=["This is the preface content..."]),
|
|
354
|
+
TextBlock(kind=TextKind.HEADLINE, level=0, content=["Preface"]),
|
|
355
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["This is the preface content..."]),
|
|
353
356
|
]
|
|
354
357
|
),
|
|
355
358
|
),
|
|
@@ -359,7 +362,7 @@ epub_data = EpubData(
|
|
|
359
362
|
title="Chapter 1",
|
|
360
363
|
get_chapter=lambda: Chapter(
|
|
361
364
|
elements=[
|
|
362
|
-
TextBlock(kind=TextKind.BODY, content=["Main content..."]),
|
|
365
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Main content..."]),
|
|
363
366
|
]
|
|
364
367
|
),
|
|
365
368
|
),
|
|
@@ -457,6 +460,7 @@ class Chapter:
|
|
|
457
460
|
@dataclass
|
|
458
461
|
class TextBlock:
|
|
459
462
|
kind: TextKind # BODY | HEADLINE | QUOTE
|
|
463
|
+
level: int # Heading level (0→h1, 1→h2, max h6; only for HEADLINE)
|
|
460
464
|
content: list[str | Mark | Formula | HTMLTag] # Text with optional marks, inline formulas, and HTML tags
|
|
461
465
|
```
|
|
462
466
|
|
|
@@ -34,9 +34,9 @@ epub_data = EpubData(
|
|
|
34
34
|
title="Chapter 1",
|
|
35
35
|
get_chapter=lambda: Chapter(
|
|
36
36
|
elements=[
|
|
37
|
-
TextBlock(kind=TextKind.HEADLINE, content=["Chapter 1"]),
|
|
38
|
-
TextBlock(kind=TextKind.BODY, content=["This is the first paragraph."]),
|
|
39
|
-
TextBlock(kind=TextKind.BODY, content=["This is the second paragraph."]),
|
|
37
|
+
TextBlock(kind=TextKind.HEADLINE, level=0, content=["Chapter 1"]),
|
|
38
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["This is the first paragraph."]),
|
|
39
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["This is the second paragraph."]),
|
|
40
40
|
]
|
|
41
41
|
),
|
|
42
42
|
),
|
|
@@ -84,8 +84,8 @@ epub_data = EpubData(
|
|
|
84
84
|
title="Chapter 1",
|
|
85
85
|
get_chapter=lambda: Chapter(
|
|
86
86
|
elements=[
|
|
87
|
-
TextBlock(kind=TextKind.HEADLINE, content=["Chapter 1"]),
|
|
88
|
-
TextBlock(kind=TextKind.BODY, content=["Main content..."]),
|
|
87
|
+
TextBlock(kind=TextKind.HEADLINE, level=0, content=["Chapter 1"]),
|
|
88
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Main content..."]),
|
|
89
89
|
]
|
|
90
90
|
),
|
|
91
91
|
),
|
|
@@ -109,7 +109,7 @@ epub_data = EpubData(
|
|
|
109
109
|
title="Chapter 1.1",
|
|
110
110
|
get_chapter=lambda: Chapter(
|
|
111
111
|
elements=[
|
|
112
|
-
TextBlock(kind=TextKind.BODY, content=["Content 1.1..."]),
|
|
112
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Content 1.1..."]),
|
|
113
113
|
]
|
|
114
114
|
),
|
|
115
115
|
),
|
|
@@ -117,7 +117,7 @@ epub_data = EpubData(
|
|
|
117
117
|
title="Chapter 1.2",
|
|
118
118
|
get_chapter=lambda: Chapter(
|
|
119
119
|
elements=[
|
|
120
|
-
TextBlock(kind=TextKind.BODY, content=["Content 1.2..."]),
|
|
120
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Content 1.2..."]),
|
|
121
121
|
]
|
|
122
122
|
),
|
|
123
123
|
),
|
|
@@ -141,7 +141,7 @@ epub_data = EpubData(
|
|
|
141
141
|
title="Chapter 1",
|
|
142
142
|
get_chapter=lambda: Chapter(
|
|
143
143
|
elements=[
|
|
144
|
-
TextBlock(kind=TextKind.BODY, content=["Here's an image:"]),
|
|
144
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Here's an image:"]),
|
|
145
145
|
Image(
|
|
146
146
|
path=Path("image.png"), # Image path
|
|
147
147
|
alt_text="Image description",
|
|
@@ -168,6 +168,7 @@ epub_data = EpubData(
|
|
|
168
168
|
elements=[
|
|
169
169
|
TextBlock(
|
|
170
170
|
kind=TextKind.BODY,
|
|
171
|
+
level=0,
|
|
171
172
|
content=[
|
|
172
173
|
"This is text with a footnote",
|
|
173
174
|
Mark(id=1), # Footnote marker
|
|
@@ -179,7 +180,7 @@ epub_data = EpubData(
|
|
|
179
180
|
Footnote(
|
|
180
181
|
id=1,
|
|
181
182
|
contents=[
|
|
182
|
-
TextBlock(kind=TextKind.BODY, content=["This is the footnote content."]),
|
|
183
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["This is the footnote content."]),
|
|
183
184
|
],
|
|
184
185
|
),
|
|
185
186
|
],
|
|
@@ -202,7 +203,7 @@ epub_data = EpubData(
|
|
|
202
203
|
title="Chapter 1",
|
|
203
204
|
get_chapter=lambda: Chapter(
|
|
204
205
|
elements=[
|
|
205
|
-
TextBlock(kind=TextKind.BODY, content=["Here's a table:"]),
|
|
206
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Here's a table:"]),
|
|
206
207
|
Table(
|
|
207
208
|
html_content="""
|
|
208
209
|
<table>
|
|
@@ -234,7 +235,7 @@ epub_data = EpubData(
|
|
|
234
235
|
title="Chapter 1",
|
|
235
236
|
get_chapter=lambda: Chapter(
|
|
236
237
|
elements=[
|
|
237
|
-
TextBlock(kind=TextKind.BODY, content=["Pythagorean theorem:"]),
|
|
238
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Pythagorean theorem:"]),
|
|
238
239
|
Formula(latex_expression="x^2 + y^2 = z^2"), # Block-level formula
|
|
239
240
|
]
|
|
240
241
|
),
|
|
@@ -257,6 +258,7 @@ epub_data = EpubData(
|
|
|
257
258
|
elements=[
|
|
258
259
|
TextBlock(
|
|
259
260
|
kind=TextKind.BODY,
|
|
261
|
+
level=0,
|
|
260
262
|
content=[
|
|
261
263
|
"The Pythagorean theorem ",
|
|
262
264
|
Formula(latex_expression="a^2 + b^2 = c^2"), # Inline formula
|
|
@@ -287,6 +289,7 @@ epub_data = EpubData(
|
|
|
287
289
|
title="Chapter 1",
|
|
288
290
|
get_chapter=lambda: Chapter(elements=[TextBlock(
|
|
289
291
|
kind=TextKind.BODY,
|
|
292
|
+
level=0,
|
|
290
293
|
content=[
|
|
291
294
|
"This is normal text with ",
|
|
292
295
|
HTMLTag(
|
|
@@ -321,8 +324,8 @@ epub_data = EpubData(
|
|
|
321
324
|
title="Preface",
|
|
322
325
|
get_chapter=lambda: Chapter(
|
|
323
326
|
elements=[
|
|
324
|
-
TextBlock(kind=TextKind.HEADLINE, content=["Preface"]),
|
|
325
|
-
TextBlock(kind=TextKind.BODY, content=["This is the preface content..."]),
|
|
327
|
+
TextBlock(kind=TextKind.HEADLINE, level=0, content=["Preface"]),
|
|
328
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["This is the preface content..."]),
|
|
326
329
|
]
|
|
327
330
|
),
|
|
328
331
|
),
|
|
@@ -332,7 +335,7 @@ epub_data = EpubData(
|
|
|
332
335
|
title="Chapter 1",
|
|
333
336
|
get_chapter=lambda: Chapter(
|
|
334
337
|
elements=[
|
|
335
|
-
TextBlock(kind=TextKind.BODY, content=["Main content..."]),
|
|
338
|
+
TextBlock(kind=TextKind.BODY, level=0, content=["Main content..."]),
|
|
336
339
|
]
|
|
337
340
|
),
|
|
338
341
|
),
|
|
@@ -430,6 +433,7 @@ class Chapter:
|
|
|
430
433
|
@dataclass
|
|
431
434
|
class TextBlock:
|
|
432
435
|
kind: TextKind # BODY | HEADLINE | QUOTE
|
|
436
|
+
level: int # Heading level (0→h1, 1→h2, max h6; only for HEADLINE)
|
|
433
437
|
content: list[str | Mark | Formula | HTMLTag] # Text with optional marks, inline formulas, and HTML tags
|
|
434
438
|
```
|
|
435
439
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from .generation import generate_epub
|
|
2
2
|
from .options import LaTeXRender, TableRender
|
|
3
3
|
from .types import (
|
|
4
|
+
BasicAsset,
|
|
4
5
|
BookMeta,
|
|
5
6
|
Chapter,
|
|
6
7
|
ChapterGetter,
|
|
@@ -35,6 +36,7 @@ __all__ = [
|
|
|
35
36
|
"Table",
|
|
36
37
|
"Formula",
|
|
37
38
|
"HTMLTag",
|
|
39
|
+
"BasicAsset",
|
|
38
40
|
"Image",
|
|
39
41
|
"Footnote",
|
|
40
42
|
"Mark",
|
|
@@ -55,15 +55,14 @@ class Context:
|
|
|
55
55
|
nodes = list(self._hash_to_node.values())
|
|
56
56
|
nodes.sort(key=lambda node: node.file_name)
|
|
57
57
|
return [(node.file_name, node.media_type) for node in nodes]
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def chapters_with_mathml(self) -> set[str]:
|
|
61
|
+
return self._chapters_with_mathml
|
|
58
62
|
|
|
59
63
|
def mark_chapter_has_mathml(self, chapter_file_name: str) -> None:
|
|
60
|
-
"""Mark a chapter as containing MathML content for EPUB 3.0 manifest properties."""
|
|
61
64
|
self._chapters_with_mathml.add(chapter_file_name)
|
|
62
65
|
|
|
63
|
-
def chapter_has_mathml(self, chapter_file_name: str) -> bool:
|
|
64
|
-
"""Check if a chapter contains MathML content."""
|
|
65
|
-
return chapter_file_name in self._chapters_with_mathml
|
|
66
|
-
|
|
67
66
|
def use_asset(
|
|
68
67
|
self,
|
|
69
68
|
source_path: Path,
|
|
@@ -65,4 +65,26 @@ span.formula-inline img {
|
|
|
65
65
|
vertical-align: middle;
|
|
66
66
|
margin: 0 0.2em;
|
|
67
67
|
max-height: 1.2em;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
div.asset {
|
|
71
|
+
page-break-inside: avoid;
|
|
72
|
+
margin: 1em 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
div.asset-title {
|
|
76
|
+
text-align: center;
|
|
77
|
+
font-weight: 600;
|
|
78
|
+
font-size: 0.95em;
|
|
79
|
+
color: #333;
|
|
80
|
+
margin-bottom: 0.5em;
|
|
81
|
+
font-style: italic;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
div.asset-caption {
|
|
85
|
+
text-align: center;
|
|
86
|
+
font-size: 0.9em;
|
|
87
|
+
color: #666;
|
|
88
|
+
margin-top: 0.5em;
|
|
89
|
+
font-style: italic;
|
|
68
90
|
}
|
|
@@ -8,7 +8,8 @@ from latex2mathml.converter import convert
|
|
|
8
8
|
|
|
9
9
|
from ..context import Context
|
|
10
10
|
from ..options import LaTeXRender, TableRender
|
|
11
|
-
from ..types import Formula, Image, Table
|
|
11
|
+
from ..types import BasicAsset, Formula, Image, Table
|
|
12
|
+
from .gen_content import render_html_tag, render_inline_content
|
|
12
13
|
|
|
13
14
|
_MEDIA_TYPE_MAP = {
|
|
14
15
|
".png": "image/png",
|
|
@@ -18,25 +19,40 @@ _MEDIA_TYPE_MAP = {
|
|
|
18
19
|
".svg": "image/svg+xml",
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
def process_table(context: Context, table: Table) -> Element | None:
|
|
22
|
-
if context.table_render == TableRender.CLIPPING:
|
|
23
|
-
return None
|
|
24
|
-
try:
|
|
25
|
-
wrapped_html = f"<div>{table.html_content}</div>"
|
|
26
|
-
parsed = fromstring(wrapped_html)
|
|
27
|
-
wrapper = Element("div", attrib={"class": "alt-wrapper"})
|
|
28
22
|
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
def render_inline_formula(context: Context, formula: Formula) -> Element | None:
|
|
24
|
+
return _render_formula(
|
|
25
|
+
context=context,
|
|
26
|
+
formula=formula,
|
|
27
|
+
inline_mode=True,
|
|
28
|
+
)
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
|
|
31
|
+
def render_asset_block(context: Context, block: Table | Formula | Image) -> Element | None:
|
|
32
|
+
element: Element | None = None
|
|
33
|
+
if isinstance(block, Table):
|
|
34
|
+
element = _render_table(context, block)
|
|
35
|
+
elif isinstance(block, Formula):
|
|
36
|
+
element = _render_formula(context, block, inline_mode=False)
|
|
37
|
+
elif isinstance(block, Image):
|
|
38
|
+
element = _process_image(context, block)
|
|
39
|
+
return element
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _render_table(context: Context, table: Table) -> Element | None:
|
|
43
|
+
if context.table_render == TableRender.CLIPPING:
|
|
34
44
|
return None
|
|
35
45
|
|
|
46
|
+
return _wrap_asset_content(
|
|
47
|
+
context=context,
|
|
48
|
+
asset=table,
|
|
49
|
+
content_element=render_html_tag(context, table.html_content),
|
|
50
|
+
)
|
|
51
|
+
|
|
36
52
|
|
|
37
|
-
def
|
|
38
|
-
context: Context,
|
|
39
|
-
formula: Formula,
|
|
53
|
+
def _render_formula(
|
|
54
|
+
context: Context,
|
|
55
|
+
formula: Formula,
|
|
40
56
|
inline_mode: bool,
|
|
41
57
|
) -> Element | None:
|
|
42
58
|
|
|
@@ -47,9 +63,10 @@ def process_formula(
|
|
|
47
63
|
if not latex_expr:
|
|
48
64
|
return None
|
|
49
65
|
|
|
66
|
+
content_element = None
|
|
50
67
|
if context.latex_render == LaTeXRender.MATHML:
|
|
51
|
-
|
|
52
|
-
latex=latex_expr,
|
|
68
|
+
content_element = _latex2mathml(
|
|
69
|
+
latex=latex_expr,
|
|
53
70
|
inline_mode=inline_mode,
|
|
54
71
|
)
|
|
55
72
|
elif context.latex_render == LaTeXRender.SVG:
|
|
@@ -64,31 +81,40 @@ def process_formula(
|
|
|
64
81
|
img_element = Element("img")
|
|
65
82
|
img_element.set("src", f"../assets/{file_name}")
|
|
66
83
|
img_element.set("alt", "formula")
|
|
84
|
+
content_element = img_element
|
|
67
85
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
else:
|
|
71
|
-
wrapper = Element("div", attrib={"class": "alt-wrapper"})
|
|
86
|
+
if content_element is None:
|
|
87
|
+
return None
|
|
72
88
|
|
|
73
|
-
|
|
74
|
-
|
|
89
|
+
return _wrap_asset_content(
|
|
90
|
+
context=context,
|
|
91
|
+
asset=formula,
|
|
92
|
+
content_element=content_element,
|
|
93
|
+
inline_mode=inline_mode,
|
|
94
|
+
)
|
|
75
95
|
|
|
76
|
-
return None
|
|
77
96
|
|
|
78
|
-
def
|
|
97
|
+
def _process_image(context: Context, image: Image) -> Element:
|
|
79
98
|
file_ext = image.path.suffix or ".png"
|
|
80
99
|
file_name = context.use_asset(
|
|
81
|
-
source_path=image.path,
|
|
82
|
-
media_type=_MEDIA_TYPE_MAP.get(file_ext.lower(), "image/png"),
|
|
100
|
+
source_path=image.path,
|
|
101
|
+
media_type=_MEDIA_TYPE_MAP.get(file_ext.lower(), "image/png"),
|
|
83
102
|
file_ext=file_ext,
|
|
84
103
|
)
|
|
85
104
|
img_element = Element("img")
|
|
86
105
|
img_element.set("src", f"../assets/{file_name}")
|
|
87
|
-
img_element.set("alt",
|
|
106
|
+
img_element.set("alt", "") # Empty alt text, use caption instead
|
|
88
107
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
108
|
+
return _wrap_asset_content(
|
|
109
|
+
context=context,
|
|
110
|
+
asset=image,
|
|
111
|
+
content_element=img_element,
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
def _normalize_expression(expression: str) -> str:
|
|
115
|
+
expression = expression.replace("\n", "")
|
|
116
|
+
expression = expression.strip()
|
|
117
|
+
return expression
|
|
92
118
|
|
|
93
119
|
|
|
94
120
|
_ESCAPE_UNICODE_PATTERN = re.compile(r"&#x([0-9A-Fa-f]{5});")
|
|
@@ -148,9 +174,35 @@ def _latex_formula2svg(latex: str, font_size: int = 12):
|
|
|
148
174
|
return output.getvalue()
|
|
149
175
|
except Exception:
|
|
150
176
|
return None
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _wrap_asset_content(
|
|
180
|
+
context: Context,
|
|
181
|
+
asset: BasicAsset,
|
|
182
|
+
content_element: Element,
|
|
183
|
+
inline_mode: bool = False,
|
|
184
|
+
) -> Element:
|
|
185
|
+
|
|
186
|
+
if inline_mode:
|
|
187
|
+
wrapper = Element("span", attrib={"class": "formula-inline"})
|
|
188
|
+
else:
|
|
189
|
+
wrapper = Element("div", attrib={"class": "alt-wrapper"})
|
|
151
190
|
|
|
191
|
+
wrapper.append(content_element)
|
|
152
192
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
193
|
+
if not asset.title and not asset.caption:
|
|
194
|
+
return wrapper
|
|
195
|
+
|
|
196
|
+
container = Element("div", attrib={"class": "asset"})
|
|
197
|
+
if asset.title:
|
|
198
|
+
title_div = Element("div", attrib={"class": "asset-title"})
|
|
199
|
+
render_inline_content(context, title_div, asset.title)
|
|
200
|
+
container.append(title_div)
|
|
201
|
+
|
|
202
|
+
container.append(wrapper)
|
|
203
|
+
if asset.caption:
|
|
204
|
+
caption_div = Element("div", attrib={"class": "asset-caption"})
|
|
205
|
+
render_inline_content(context, caption_div, asset.caption)
|
|
206
|
+
container.append(caption_div)
|
|
207
|
+
|
|
208
|
+
return container
|
|
@@ -7,16 +7,17 @@ from ..types import (
|
|
|
7
7
|
Chapter,
|
|
8
8
|
ContentBlock,
|
|
9
9
|
Formula,
|
|
10
|
-
HTMLTag,
|
|
11
10
|
Image,
|
|
12
|
-
Mark,
|
|
13
11
|
Table,
|
|
14
12
|
TextBlock,
|
|
15
13
|
TextKind,
|
|
16
14
|
)
|
|
17
|
-
from .gen_asset import
|
|
15
|
+
from .gen_asset import render_asset_block
|
|
16
|
+
from .gen_content import render_inline_content
|
|
18
17
|
from .xml_utils import serialize_element, set_epub_type
|
|
19
18
|
|
|
19
|
+
_MAX_HEADING_LEVEL = 6 # HTML standard defines heading levels from h1 to h6
|
|
20
|
+
|
|
20
21
|
|
|
21
22
|
def generate_chapter(
|
|
22
23
|
context: Context,
|
|
@@ -89,9 +90,13 @@ def _render_footnotes(
|
|
|
89
90
|
|
|
90
91
|
|
|
91
92
|
def _render_content_block(context: Context, block: ContentBlock) -> Element | None:
|
|
92
|
-
if isinstance(block,
|
|
93
|
+
if isinstance(block, Table | Formula | Image):
|
|
94
|
+
return render_asset_block(context, block)
|
|
95
|
+
|
|
96
|
+
elif isinstance(block, TextBlock):
|
|
93
97
|
if block.kind == TextKind.HEADLINE:
|
|
94
|
-
|
|
98
|
+
heading_level = min(block.level + 1, _MAX_HEADING_LEVEL)
|
|
99
|
+
container = Element(f"h{heading_level}")
|
|
95
100
|
elif block.kind == TextKind.QUOTE:
|
|
96
101
|
container = Element("p")
|
|
97
102
|
elif block.kind == TextKind.BODY:
|
|
@@ -99,9 +104,9 @@ def _render_content_block(context: Context, block: ContentBlock) -> Element | No
|
|
|
99
104
|
else:
|
|
100
105
|
raise ValueError(f"Unknown TextKind: {block.kind}")
|
|
101
106
|
|
|
102
|
-
|
|
103
|
-
context=context,
|
|
104
|
-
parent=container,
|
|
107
|
+
render_inline_content(
|
|
108
|
+
context=context,
|
|
109
|
+
parent=container,
|
|
105
110
|
content=block.content,
|
|
106
111
|
)
|
|
107
112
|
if block.kind == TextKind.QUOTE:
|
|
@@ -110,68 +115,6 @@ def _render_content_block(context: Context, block: ContentBlock) -> Element | No
|
|
|
110
115
|
return blockquote
|
|
111
116
|
|
|
112
117
|
return container
|
|
113
|
-
|
|
114
|
-
elif isinstance(block, Table):
|
|
115
|
-
return process_table(context, block)
|
|
116
|
-
|
|
117
|
-
elif isinstance(block, Formula):
|
|
118
|
-
return process_formula(context, block, inline_mode=False)
|
|
119
|
-
|
|
120
|
-
elif isinstance(block, Image):
|
|
121
|
-
return process_image(context, block)
|
|
122
|
-
|
|
118
|
+
|
|
123
119
|
else:
|
|
124
120
|
return None
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
def _render_text_content(context: Context, parent: Element, content: list[str | Mark | Formula | HTMLTag]) -> None:
|
|
128
|
-
"""Render text content with inline citation marks."""
|
|
129
|
-
current_element = parent
|
|
130
|
-
for item in content:
|
|
131
|
-
if isinstance(item, str):
|
|
132
|
-
if current_element is parent:
|
|
133
|
-
if parent.text is None:
|
|
134
|
-
parent.text = item
|
|
135
|
-
else:
|
|
136
|
-
parent.text += item
|
|
137
|
-
else:
|
|
138
|
-
if current_element.tail is None:
|
|
139
|
-
current_element.tail = item
|
|
140
|
-
else:
|
|
141
|
-
current_element.tail += item
|
|
142
|
-
|
|
143
|
-
elif isinstance(item, HTMLTag):
|
|
144
|
-
tag_element = Element(item.name)
|
|
145
|
-
for attr, value in item.attributes:
|
|
146
|
-
tag_element.set(attr, value)
|
|
147
|
-
_render_text_content(
|
|
148
|
-
context=context,
|
|
149
|
-
parent=tag_element,
|
|
150
|
-
content=item.content,
|
|
151
|
-
)
|
|
152
|
-
parent.append(tag_element)
|
|
153
|
-
current_element = tag_element
|
|
154
|
-
|
|
155
|
-
elif isinstance(item, Formula):
|
|
156
|
-
formula_element = process_formula(
|
|
157
|
-
context=context,
|
|
158
|
-
formula=item,
|
|
159
|
-
inline_mode=True,
|
|
160
|
-
)
|
|
161
|
-
if formula_element is not None:
|
|
162
|
-
parent.append(formula_element)
|
|
163
|
-
current_element = formula_element
|
|
164
|
-
|
|
165
|
-
elif isinstance(item, Mark):
|
|
166
|
-
# EPUB 3.0 noteref with semantic attributes
|
|
167
|
-
anchor = Element("a")
|
|
168
|
-
anchor.attrib = {
|
|
169
|
-
"id": f"ref-{item.id}",
|
|
170
|
-
"href": f"#fn-{item.id}",
|
|
171
|
-
"class": "super",
|
|
172
|
-
}
|
|
173
|
-
# Set epub:type using utility function (avoids global namespace pollution)
|
|
174
|
-
set_epub_type(anchor, "noteref")
|
|
175
|
-
anchor.text = f"[{item.id}]"
|
|
176
|
-
parent.append(anchor)
|
|
177
|
-
current_element = anchor
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
from xml.etree.ElementTree import Element
|
|
2
|
+
|
|
3
|
+
from ..context import Context
|
|
4
|
+
from ..types import Formula, HTMLTag, Mark
|
|
5
|
+
from .xml_utils import set_epub_type
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def render_inline_content(
|
|
9
|
+
context: Context,
|
|
10
|
+
parent: Element,
|
|
11
|
+
content: list[str | Mark | Formula | HTMLTag]
|
|
12
|
+
) -> None:
|
|
13
|
+
current_element = parent
|
|
14
|
+
for item in content:
|
|
15
|
+
if isinstance(item, str):
|
|
16
|
+
if current_element is parent:
|
|
17
|
+
if parent.text is None:
|
|
18
|
+
parent.text = item
|
|
19
|
+
else:
|
|
20
|
+
parent.text += item
|
|
21
|
+
else:
|
|
22
|
+
if current_element.tail is None:
|
|
23
|
+
current_element.tail = item
|
|
24
|
+
else:
|
|
25
|
+
current_element.tail += item
|
|
26
|
+
|
|
27
|
+
elif isinstance(item, HTMLTag):
|
|
28
|
+
tag_element = render_html_tag(context, item)
|
|
29
|
+
parent.append(tag_element)
|
|
30
|
+
current_element = tag_element
|
|
31
|
+
|
|
32
|
+
elif isinstance(item, Formula):
|
|
33
|
+
from .gen_asset import render_inline_formula # avoid circular import
|
|
34
|
+
formula_element = render_inline_formula(context, item)
|
|
35
|
+
if formula_element is not None:
|
|
36
|
+
parent.append(formula_element)
|
|
37
|
+
current_element = formula_element
|
|
38
|
+
|
|
39
|
+
elif isinstance(item, Mark):
|
|
40
|
+
# EPUB 3.0 noteref with semantic attributes
|
|
41
|
+
anchor = Element("a")
|
|
42
|
+
anchor.attrib = {
|
|
43
|
+
"id": f"ref-{item.id}",
|
|
44
|
+
"href": f"#fn-{item.id}",
|
|
45
|
+
"class": "super",
|
|
46
|
+
}
|
|
47
|
+
set_epub_type(anchor, "noteref")
|
|
48
|
+
anchor.text = f"[{item.id}]"
|
|
49
|
+
parent.append(anchor)
|
|
50
|
+
current_element = anchor
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def render_html_tag(context: Context, tag: HTMLTag) -> Element:
|
|
54
|
+
"""Convert HTMLTag to XML Element with full inline content support."""
|
|
55
|
+
element = Element(tag.name)
|
|
56
|
+
for attr, value in tag.attributes:
|
|
57
|
+
element.set(attr, value)
|
|
58
|
+
render_inline_content(context, element, tag.content)
|
|
59
|
+
return element
|