epub-generator 0.1.3__py3-none-any.whl → 0.1.5__py3-none-any.whl
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/__init__.py +2 -0
- epub_generator/context.py +4 -5
- epub_generator/data/nav.xhtml.jinja +1 -1
- epub_generator/data/style.css.jinja +22 -0
- epub_generator/generation/gen_asset.py +87 -35
- epub_generator/generation/gen_chapter.py +14 -71
- epub_generator/generation/gen_content.py +59 -0
- epub_generator/generation/gen_epub.py +43 -43
- epub_generator/generation/gen_nav.py +40 -62
- epub_generator/generation/gen_toc.py +57 -40
- epub_generator/generation/xml_utils.py +23 -0
- epub_generator/i18n.py +2 -0
- epub_generator/types.py +22 -9
- {epub_generator-0.1.3.dist-info → epub_generator-0.1.5.dist-info}/METADATA +19 -15
- epub_generator-0.1.5.dist-info/RECORD +26 -0
- epub_generator-0.1.3.dist-info/RECORD +0 -25
- {epub_generator-0.1.3.dist-info → epub_generator-0.1.5.dist-info}/LICENSE +0 -0
- {epub_generator-0.1.3.dist-info → epub_generator-0.1.5.dist-info}/WHEEL +0 -0
epub_generator/__init__.py
CHANGED
|
@@ -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",
|
epub_generator/context.py
CHANGED
|
@@ -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
|
|
@@ -9,10 +9,10 @@ from ..context import Context, Template
|
|
|
9
9
|
from ..html_tag import search_content
|
|
10
10
|
from ..i18n import I18N
|
|
11
11
|
from ..options import LaTeXRender, TableRender
|
|
12
|
-
from ..types import Chapter, EpubData, Formula, TextBlock
|
|
12
|
+
from ..types import BasicAsset, Chapter, ContentBlock, EpubData, Formula, TextBlock
|
|
13
13
|
from .gen_chapter import generate_chapter
|
|
14
14
|
from .gen_nav import gen_nav
|
|
15
|
-
from .gen_toc import
|
|
15
|
+
from .gen_toc import TocPoint, gen_toc, iter_toc
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
def generate_epub(
|
|
@@ -26,10 +26,8 @@ def generate_epub(
|
|
|
26
26
|
i18n = I18N(lan)
|
|
27
27
|
template = Template()
|
|
28
28
|
epub_file_path = Path(epub_file_path)
|
|
29
|
-
|
|
30
|
-
# Generate navigation points from TOC structure
|
|
31
29
|
has_cover = epub_data.cover_image_path is not None
|
|
32
|
-
|
|
30
|
+
toc_points = gen_toc(epub_data=epub_data)
|
|
33
31
|
|
|
34
32
|
epub_file_path.parent.mkdir(parents=True, exist_ok=True)
|
|
35
33
|
|
|
@@ -49,7 +47,7 @@ def generate_epub(
|
|
|
49
47
|
_write_chapters_from_data(
|
|
50
48
|
context=context,
|
|
51
49
|
i18n=i18n,
|
|
52
|
-
|
|
50
|
+
toc_points=toc_points,
|
|
53
51
|
epub_data=epub_data,
|
|
54
52
|
latex_render=latex_render,
|
|
55
53
|
assert_not_aborted=assert_not_aborted,
|
|
@@ -58,7 +56,7 @@ def generate_epub(
|
|
|
58
56
|
template=template,
|
|
59
57
|
i18n=i18n,
|
|
60
58
|
epub_data=epub_data,
|
|
61
|
-
|
|
59
|
+
toc_points=toc_points,
|
|
62
60
|
has_cover=has_cover,
|
|
63
61
|
)
|
|
64
62
|
file.writestr(
|
|
@@ -71,7 +69,7 @@ def generate_epub(
|
|
|
71
69
|
context=context,
|
|
72
70
|
i18n=i18n,
|
|
73
71
|
epub_data=epub_data,
|
|
74
|
-
|
|
72
|
+
toc_points=toc_points,
|
|
75
73
|
)
|
|
76
74
|
assert_not_aborted()
|
|
77
75
|
|
|
@@ -81,6 +79,7 @@ def generate_epub(
|
|
|
81
79
|
epub_data=epub_data,
|
|
82
80
|
)
|
|
83
81
|
|
|
82
|
+
|
|
84
83
|
def _write_assets_from_data(
|
|
85
84
|
context: Context,
|
|
86
85
|
i18n: I18N,
|
|
@@ -104,62 +103,67 @@ def _write_assets_from_data(
|
|
|
104
103
|
arcname="OEBPS/assets/cover.png",
|
|
105
104
|
)
|
|
106
105
|
|
|
106
|
+
|
|
107
107
|
def _write_chapters_from_data(
|
|
108
108
|
context: Context,
|
|
109
109
|
i18n: I18N,
|
|
110
|
-
|
|
110
|
+
toc_points: list[TocPoint],
|
|
111
111
|
epub_data: EpubData,
|
|
112
112
|
latex_render: LaTeXRender,
|
|
113
113
|
assert_not_aborted: Callable[[], None],
|
|
114
114
|
):
|
|
115
|
-
|
|
116
|
-
chapter =
|
|
115
|
+
for file_name, get_chapter in _search_chapters(epub_data, toc_points):
|
|
116
|
+
chapter = get_chapter()
|
|
117
117
|
data = generate_chapter(context, chapter, i18n)
|
|
118
118
|
context.file.writestr(
|
|
119
|
-
zinfo_or_arcname="OEBPS/Text/
|
|
119
|
+
zinfo_or_arcname="OEBPS/Text/" + file_name,
|
|
120
120
|
data=data.encode("utf-8"),
|
|
121
121
|
)
|
|
122
122
|
if latex_render == LaTeXRender.MATHML and _chapter_has_formula(chapter):
|
|
123
|
-
context.mark_chapter_has_mathml(
|
|
123
|
+
context.mark_chapter_has_mathml(file_name)
|
|
124
124
|
assert_not_aborted()
|
|
125
125
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
data=data.encode("utf-8"),
|
|
133
|
-
)
|
|
134
|
-
if latex_render == LaTeXRender.MATHML and _chapter_has_formula(chapter):
|
|
135
|
-
context.mark_chapter_has_mathml(nav_point.file_name)
|
|
136
|
-
assert_not_aborted()
|
|
126
|
+
|
|
127
|
+
def _search_chapters(epub_data: EpubData, toc_points: list[TocPoint]):
|
|
128
|
+
if epub_data.get_head is not None:
|
|
129
|
+
yield "head.xhtml", epub_data.get_head
|
|
130
|
+
for ref in iter_toc(toc_points):
|
|
131
|
+
yield ref.file_name, ref.get_chapter
|
|
137
132
|
|
|
138
133
|
|
|
139
134
|
def _chapter_has_formula(chapter: Chapter) -> bool:
|
|
140
|
-
"""Check if chapter contains any formulas (block-level or inline)."""
|
|
141
135
|
for element in chapter.elements:
|
|
142
|
-
if
|
|
136
|
+
if _content_block_has_formula(element):
|
|
143
137
|
return True
|
|
144
|
-
if isinstance(element, TextBlock):
|
|
145
|
-
for item in search_content(element.content):
|
|
146
|
-
if isinstance(item, Formula):
|
|
147
|
-
return True
|
|
148
138
|
for footnote in chapter.footnotes:
|
|
149
139
|
for content_block in footnote.contents:
|
|
150
|
-
if
|
|
140
|
+
if _content_block_has_formula(content_block):
|
|
151
141
|
return True
|
|
152
|
-
if isinstance(content_block, TextBlock):
|
|
153
|
-
for item in search_content(content_block.content):
|
|
154
|
-
if isinstance(item, Formula):
|
|
155
|
-
return True
|
|
156
142
|
return False
|
|
157
143
|
|
|
144
|
+
|
|
145
|
+
def _content_block_has_formula(content_block: ContentBlock) -> bool:
|
|
146
|
+
if isinstance(content_block, Formula):
|
|
147
|
+
return True
|
|
148
|
+
if isinstance(content_block, TextBlock):
|
|
149
|
+
for item in search_content(content_block.content):
|
|
150
|
+
if isinstance(item, Formula):
|
|
151
|
+
return True
|
|
152
|
+
if isinstance(content_block, BasicAsset):
|
|
153
|
+
for item in search_content(content_block.title):
|
|
154
|
+
if isinstance(item, Formula):
|
|
155
|
+
return True
|
|
156
|
+
for item in search_content(content_block.caption):
|
|
157
|
+
if isinstance(item, Formula):
|
|
158
|
+
return True
|
|
159
|
+
return False
|
|
160
|
+
|
|
161
|
+
|
|
158
162
|
def _write_basic_files(
|
|
159
163
|
context: Context,
|
|
160
164
|
i18n: I18N,
|
|
161
165
|
epub_data: EpubData,
|
|
162
|
-
|
|
166
|
+
toc_points: list[TocPoint],
|
|
163
167
|
):
|
|
164
168
|
meta = epub_data.meta
|
|
165
169
|
has_cover = epub_data.cover_image_path is not None
|
|
@@ -175,22 +179,18 @@ def _write_basic_files(
|
|
|
175
179
|
else:
|
|
176
180
|
modified_timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
177
181
|
|
|
178
|
-
|
|
179
|
-
nav_point.file_name
|
|
180
|
-
for nav_point in nav_points
|
|
181
|
-
if context.chapter_has_mathml(nav_point.file_name)
|
|
182
|
-
}
|
|
182
|
+
toc_refs = list(iter_toc(toc_points))
|
|
183
183
|
content = context.template.render(
|
|
184
184
|
template="content.opf",
|
|
185
185
|
meta=meta,
|
|
186
186
|
i18n=i18n,
|
|
187
187
|
ISBN=isbn,
|
|
188
188
|
modified_timestamp=modified_timestamp,
|
|
189
|
-
nav_points=
|
|
189
|
+
nav_points=toc_refs,
|
|
190
190
|
has_head_chapter=has_head_chapter,
|
|
191
191
|
has_cover=has_cover,
|
|
192
192
|
asset_files=context.used_files,
|
|
193
|
-
chapters_with_mathml=chapters_with_mathml,
|
|
193
|
+
chapters_with_mathml=context.chapters_with_mathml,
|
|
194
194
|
)
|
|
195
195
|
context.file.writestr(
|
|
196
196
|
zinfo_or_arcname="OEBPS/content.opf",
|
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
from
|
|
1
|
+
from xml.etree.ElementTree import Element
|
|
2
2
|
|
|
3
3
|
from ..context import Template
|
|
4
4
|
from ..i18n import I18N
|
|
5
|
-
from ..types import BookMeta, EpubData
|
|
6
|
-
from .gen_toc import
|
|
5
|
+
from ..types import BookMeta, EpubData
|
|
6
|
+
from .gen_toc import TocPoint, iter_toc
|
|
7
|
+
from .xml_utils import indent, serialize_element
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
def gen_nav(
|
|
10
11
|
template: Template,
|
|
11
12
|
i18n: I18N,
|
|
12
13
|
epub_data: EpubData,
|
|
13
|
-
|
|
14
|
+
toc_points: list[TocPoint],
|
|
14
15
|
has_cover: bool = False,
|
|
15
16
|
) -> str:
|
|
16
17
|
meta: BookMeta | None = epub_data.meta
|
|
17
18
|
has_head_chapter = epub_data.get_head is not None
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
toc_body = "\n".join(_render_toc_item(toc_points))
|
|
20
|
+
first_ref = next(iter_toc(toc_points), None)
|
|
21
|
+
|
|
22
|
+
first_chapter_file: str = ""
|
|
23
|
+
head_chapter_title: str = ""
|
|
24
|
+
if first_ref:
|
|
25
|
+
first_chapter_file = first_ref.file_name
|
|
21
26
|
if has_head_chapter and epub_data.get_head:
|
|
22
|
-
|
|
23
|
-
head_chapter_title = "Preface" # Default title
|
|
27
|
+
head_chapter_title = i18n.preface
|
|
24
28
|
|
|
25
29
|
return template.render(
|
|
26
30
|
template="nav.xhtml",
|
|
@@ -29,64 +33,38 @@ def gen_nav(
|
|
|
29
33
|
has_cover=has_cover,
|
|
30
34
|
has_head_chapter=has_head_chapter,
|
|
31
35
|
head_chapter_title=head_chapter_title,
|
|
32
|
-
|
|
36
|
+
toc_body=toc_body,
|
|
33
37
|
first_chapter_file=first_chapter_file,
|
|
34
38
|
)
|
|
35
39
|
|
|
36
40
|
|
|
37
|
-
def
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
)
|
|
42
|
-
nav_point_index = 0
|
|
43
|
-
|
|
44
|
-
html_parts = []
|
|
45
|
-
for chapters_list in (prefaces, chapters):
|
|
46
|
-
for toc_item in chapters_list:
|
|
47
|
-
nav_point_index, item_html = _generate_toc_item(
|
|
48
|
-
toc_item, nav_points, nav_point_index
|
|
49
|
-
)
|
|
50
|
-
html_parts.append(item_html)
|
|
51
|
-
|
|
52
|
-
return "\n".join(html_parts)
|
|
53
|
-
|
|
41
|
+
def _render_toc_item(toc_points: list[TocPoint]):
|
|
42
|
+
for toc_point in toc_points:
|
|
43
|
+
element = _create_toc_li_element(toc_point)
|
|
44
|
+
element = indent(element)
|
|
45
|
+
yield serialize_element(element)
|
|
54
46
|
|
|
55
|
-
def _generate_toc_item(
|
|
56
|
-
toc_item: TocItem,
|
|
57
|
-
nav_points: list[NavPoint],
|
|
58
|
-
nav_point_index: int,
|
|
59
|
-
) -> tuple[int, str]:
|
|
60
|
-
title_escaped = escape(toc_item.title)
|
|
61
|
-
file_name = None
|
|
62
|
-
if toc_item.get_chapter is not None and nav_point_index < len(nav_points):
|
|
63
|
-
file_name = nav_points[nav_point_index].file_name
|
|
64
|
-
nav_point_index += 1
|
|
65
47
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
nav_point_index, child_html = _generate_toc_item(
|
|
69
|
-
child, nav_points, nav_point_index
|
|
70
|
-
)
|
|
71
|
-
children_html.append(child_html)
|
|
48
|
+
def _create_toc_li_element(toc_point: TocPoint) -> Element:
|
|
49
|
+
li = Element("li")
|
|
72
50
|
|
|
73
|
-
if
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if file_name:
|
|
81
|
-
html_parts = [f' <li>\n <a href="Text/{file_name}">{title_escaped}</a>']
|
|
51
|
+
if toc_point.ref is not None:
|
|
52
|
+
file_name = toc_point.ref.file_name
|
|
53
|
+
link = Element("a")
|
|
54
|
+
link.set("href", f"Text/{file_name}")
|
|
55
|
+
link.text = toc_point.title
|
|
56
|
+
li.append(link)
|
|
82
57
|
else:
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
58
|
+
span = Element("span")
|
|
59
|
+
span.text = toc_point.title
|
|
60
|
+
li.append(span)
|
|
61
|
+
|
|
62
|
+
# 递归处理子节点
|
|
63
|
+
if toc_point.children:
|
|
64
|
+
ol = Element("ol")
|
|
65
|
+
for child in toc_point.children:
|
|
66
|
+
child_li = _create_toc_li_element(child)
|
|
67
|
+
ol.append(child_li)
|
|
68
|
+
li.append(ol)
|
|
69
|
+
|
|
70
|
+
return li
|
|
@@ -1,36 +1,57 @@
|
|
|
1
1
|
from dataclasses import dataclass
|
|
2
|
-
from typing import Any, Callable
|
|
2
|
+
from typing import Any, Callable, Generator
|
|
3
3
|
|
|
4
4
|
from ..types import EpubData, TocItem
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
@dataclass
|
|
8
|
-
class
|
|
9
|
-
|
|
10
|
-
file_name: str
|
|
8
|
+
class TocPoint:
|
|
9
|
+
title: str
|
|
11
10
|
order: int
|
|
12
|
-
|
|
11
|
+
ref: "TocPointRef | None"
|
|
12
|
+
children: list["TocPoint"]
|
|
13
|
+
|
|
14
|
+
@property
|
|
15
|
+
def is_placeholder(self) -> bool:
|
|
16
|
+
"""是否为占位节点(无对应文件)"""
|
|
17
|
+
return self.ref is None
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def has_file(self) -> bool:
|
|
21
|
+
"""是否有对应的 XHTML 文件"""
|
|
22
|
+
return self.ref is not None
|
|
23
|
+
|
|
24
|
+
@dataclass
|
|
25
|
+
class TocPointRef:
|
|
26
|
+
part_id: str
|
|
27
|
+
file_name: str
|
|
28
|
+
get_chapter: Callable[[], Any]
|
|
13
29
|
|
|
14
30
|
|
|
15
|
-
def
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
31
|
+
def iter_toc(toc_points: list[TocPoint]) -> Generator[TocPointRef, None, None]:
|
|
32
|
+
for toc_point in toc_points:
|
|
33
|
+
if toc_point.ref:
|
|
34
|
+
yield toc_point.ref
|
|
35
|
+
yield from iter_toc(toc_point.children)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def gen_toc(epub_data: EpubData) -> list[TocPoint]:
|
|
19
39
|
prefaces = epub_data.prefaces
|
|
20
40
|
chapters = epub_data.chapters
|
|
21
41
|
|
|
22
|
-
|
|
23
|
-
has_cover=has_cover,
|
|
42
|
+
toc_point_generation = _TocPointGenerator(
|
|
24
43
|
chapters_count=(
|
|
25
44
|
_count_toc_items(prefaces) +
|
|
26
45
|
_count_toc_items(chapters)
|
|
27
46
|
),
|
|
28
47
|
)
|
|
48
|
+
toc_points: list[TocPoint] = []
|
|
29
49
|
for chapters_list in (prefaces, chapters):
|
|
30
50
|
for toc_item in chapters_list:
|
|
31
|
-
|
|
51
|
+
toc_point = toc_point_generation.generate(toc_item)
|
|
52
|
+
toc_points.append(toc_point)
|
|
32
53
|
|
|
33
|
-
return
|
|
54
|
+
return toc_points
|
|
34
55
|
|
|
35
56
|
|
|
36
57
|
def _count_toc_items(items: list[TocItem]) -> int:
|
|
@@ -50,39 +71,35 @@ def _max_depth_toc_items(items: list[TocItem]) -> int:
|
|
|
50
71
|
return max_depth
|
|
51
72
|
|
|
52
73
|
|
|
53
|
-
class
|
|
54
|
-
def __init__(self,
|
|
55
|
-
self.
|
|
56
|
-
self._next_order: int = 2 if has_cover else 1
|
|
74
|
+
class _TocPointGenerator:
|
|
75
|
+
def __init__(self, chapters_count: int):
|
|
76
|
+
self._next_order: int = 0
|
|
57
77
|
self._next_id: int = 1
|
|
58
78
|
self._digits = len(str(chapters_count))
|
|
59
79
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return self._nav_points
|
|
63
|
-
|
|
64
|
-
def generate(self, toc_item: TocItem) -> None:
|
|
65
|
-
self._create_nav_point(toc_item)
|
|
80
|
+
def generate(self, toc_item: TocItem) -> TocPoint:
|
|
81
|
+
return self._create_toc_point(toc_item)
|
|
66
82
|
|
|
67
|
-
def
|
|
68
|
-
|
|
83
|
+
def _create_toc_point(self, toc_item: TocItem) -> TocPoint:
|
|
84
|
+
ref: TocPointRef | None = None
|
|
69
85
|
if toc_item.get_chapter is not None:
|
|
70
|
-
|
|
86
|
+
part_id = self._next_id
|
|
71
87
|
self._next_id += 1
|
|
72
|
-
part_id = str(
|
|
73
|
-
|
|
74
|
-
|
|
88
|
+
part_id = str(part_id).zfill(self._digits)
|
|
89
|
+
ref = TocPointRef(
|
|
90
|
+
part_id=part_id,
|
|
75
91
|
file_name=f"part{part_id}.xhtml",
|
|
76
|
-
order=self._next_order,
|
|
77
92
|
get_chapter=toc_item.get_chapter,
|
|
78
93
|
)
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
94
|
+
order = self._next_order # 确保 order 以中序遍历为顺序
|
|
95
|
+
self._next_order += 1
|
|
96
|
+
|
|
97
|
+
return TocPoint(
|
|
98
|
+
title=toc_item.title,
|
|
99
|
+
order=order,
|
|
100
|
+
ref=ref,
|
|
101
|
+
children=[
|
|
102
|
+
self._create_toc_point(child)
|
|
103
|
+
for child in toc_item.children
|
|
104
|
+
],
|
|
105
|
+
)
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import re
|
|
2
|
+
from typing import Container
|
|
2
3
|
from xml.etree.ElementTree import Element, tostring
|
|
3
4
|
|
|
4
5
|
_EPUB_NS = "http://www.idpf.org/2007/ops"
|
|
@@ -29,3 +30,25 @@ def serialize_element(element: Element) -> str:
|
|
|
29
30
|
xml_string = xml_string.replace(f"{ns_prefix}:", f"{prefix}:")
|
|
30
31
|
|
|
31
32
|
return xml_string
|
|
33
|
+
|
|
34
|
+
def indent(elem: Element, level: int = 0, skip_tags: Container[str] = ()) -> Element:
|
|
35
|
+
indent_str = " " * level
|
|
36
|
+
next_indent_str = " " * (level + 1)
|
|
37
|
+
|
|
38
|
+
if elem.tag in skip_tags:
|
|
39
|
+
if level > 0 and (not elem.tail or not elem.tail.strip()):
|
|
40
|
+
elem.tail = "\n" + indent_str
|
|
41
|
+
return elem
|
|
42
|
+
|
|
43
|
+
if len(elem):
|
|
44
|
+
if not elem.text or not elem.text.strip():
|
|
45
|
+
elem.text = "\n" + next_indent_str
|
|
46
|
+
for i, child in enumerate(elem):
|
|
47
|
+
indent(child, level + 1, skip_tags)
|
|
48
|
+
if i < len(elem) - 1:
|
|
49
|
+
child.tail = "\n" + next_indent_str
|
|
50
|
+
else:
|
|
51
|
+
child.tail = "\n" + indent_str
|
|
52
|
+
elif level > 0 and (not elem.tail or not elem.tail.strip()):
|
|
53
|
+
elem.tail = "\n" + indent_str
|
|
54
|
+
return elem
|
epub_generator/i18n.py
CHANGED
|
@@ -9,9 +9,11 @@ class I18N:
|
|
|
9
9
|
self.table_of_contents: str = "目录"
|
|
10
10
|
self.landmarks: str = "路标"
|
|
11
11
|
self.start_of_content: str = "正文开始"
|
|
12
|
+
self.preface: str = "前言"
|
|
12
13
|
elif lan == "en":
|
|
13
14
|
self.unnamed: str = "Unnamed"
|
|
14
15
|
self.cover: str = "Cover"
|
|
15
16
|
self.table_of_contents: str = "Table of Contents"
|
|
16
17
|
self.landmarks: str = "Landmarks"
|
|
17
18
|
self.start_of_content: str = "Start of Content"
|
|
19
|
+
self.preface: str = "Preface"
|
epub_generator/types.py
CHANGED
|
@@ -84,32 +84,45 @@ class Mark:
|
|
|
84
84
|
"""Citation ID, matches Footnote.id"""
|
|
85
85
|
|
|
86
86
|
@dataclass
|
|
87
|
-
class
|
|
88
|
-
"""
|
|
89
|
-
html_content: str
|
|
90
|
-
"""HTML table markup"""
|
|
87
|
+
class BasicAsset:
|
|
88
|
+
"""Asset as a base class for other assets."""
|
|
91
89
|
|
|
90
|
+
title: list["str | Mark | Formula | HTMLTag"] = field(default_factory=list, kw_only=True)
|
|
91
|
+
"""Asset title (before content)"""
|
|
92
|
+
caption: list["str | Mark | Formula | HTMLTag"] = field(default_factory=list, kw_only=True)
|
|
93
|
+
"""Asset caption (after content)"""
|
|
92
94
|
|
|
93
95
|
@dataclass
|
|
94
|
-
class
|
|
96
|
+
class Table(BasicAsset):
|
|
97
|
+
"""Table representation."""
|
|
98
|
+
|
|
99
|
+
html_content: "HTMLTag"
|
|
100
|
+
"""HTML content of the table"""
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@dataclass
|
|
104
|
+
class Formula(BasicAsset):
|
|
95
105
|
"""Mathematical formula."""
|
|
106
|
+
|
|
96
107
|
latex_expression: str
|
|
97
108
|
"""LaTeX expression"""
|
|
98
109
|
|
|
99
110
|
|
|
100
111
|
@dataclass
|
|
101
|
-
class Image:
|
|
112
|
+
class Image(BasicAsset):
|
|
102
113
|
"""Image reference."""
|
|
114
|
+
|
|
103
115
|
path: Path
|
|
104
116
|
"""Absolute path to the image file"""
|
|
105
117
|
|
|
106
|
-
alt_text: str = "image"
|
|
107
|
-
"""Alt text (defaults to "image")"""
|
|
108
|
-
|
|
109
118
|
@dataclass
|
|
110
119
|
class TextBlock:
|
|
120
|
+
"""Text block representation."""
|
|
121
|
+
|
|
111
122
|
kind: TextKind
|
|
112
123
|
"""Kind of text block."""
|
|
124
|
+
level: int
|
|
125
|
+
"""Heading level starting from 0 (only for HEADLINE: level 0 → h1, level 1 → h2, max h6; ignored for BODY and QUOTE)."""
|
|
113
126
|
content: list["str | Mark | Formula | HTMLTag"]
|
|
114
127
|
"""Text content with optional citation marks."""
|
|
115
128
|
|
|
@@ -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
|
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
epub_generator/__init__.py,sha256=5fFpZdgB4-FfXgCpE5IshBfrfrMaxNQK4SRKaKV2RdI,682
|
|
2
|
+
epub_generator/context.py,sha256=9jHRpnQsNooRUSBoY_tiQ7aQ_AMZmyKUO22gPoO8Koc,4324
|
|
3
|
+
epub_generator/data/container.xml.jinja,sha256=SkACyZgsAVUS5lmiCEhq3SpbFspYdyCnRNjWnLztLt0,252
|
|
4
|
+
epub_generator/data/content.opf.jinja,sha256=DDaR9GZnSBcpNk2BWUu56Uo_248TA91AxE4tKsBuKnQ,2839
|
|
5
|
+
epub_generator/data/cover.xhtml.jinja,sha256=heounlnHfOd-RNFIeytZQtAQ11ByPOiM1aB1lVyY6V4,328
|
|
6
|
+
epub_generator/data/mimetype.jinja,sha256=5GjjUNEUPrZI9gx7C9YDEQHsBUSjYcp07O8laskB9Is,20
|
|
7
|
+
epub_generator/data/nav.xhtml.jinja,sha256=zk5hf-MYoKxd4pcshZV5VliVrtDIgfH7n9f3-1L1cY0,1132
|
|
8
|
+
epub_generator/data/part.xhtml.jinja,sha256=FEQaUjHfCy7EJyyvYZj-6T-lkDcsmz1wvsk0b8LU3E0,558
|
|
9
|
+
epub_generator/data/style.css.jinja,sha256=n_DE-z97ikGzD3qufSwX_1iqkQcE_5kXiCIhyoXNjRA,1400
|
|
10
|
+
epub_generator/generation/__init__.py,sha256=UIscwHa8ocr2D1mk1KaP-zi3P1x9eYJzxTo0RJ2dnks,35
|
|
11
|
+
epub_generator/generation/gen_asset.py,sha256=WYwfGUvHM_CrwTuIIH7dYm-SL-vdhkTnvaZDymZxXzg,5978
|
|
12
|
+
epub_generator/generation/gen_chapter.py,sha256=P6kmB8hdQnJB6SCheHzu5cOmZrC5H0LqNV-uuuigX1M,3425
|
|
13
|
+
epub_generator/generation/gen_content.py,sha256=2ojjTgalveRnk1MXQaKsY53hPCgb7NHTwbMpLOXVrss,2018
|
|
14
|
+
epub_generator/generation/gen_epub.py,sha256=I7u8rrrslF9xoyDUsALarB2iWzY9zjKM9ZOR1wLMX1E,6184
|
|
15
|
+
epub_generator/generation/gen_nav.py,sha256=_cjOP18C1CoTn_DELIB06pyMPZZ0CPbkk4oPEvICdKs,1955
|
|
16
|
+
epub_generator/generation/gen_toc.py,sha256=MK2iTYBpF8VUtPHpwz5JB_H6nWsKRKpVuLzRPYGy0nw,2864
|
|
17
|
+
epub_generator/generation/xml_utils.py,sha256=kyHBWUihT5se5n_425BcEvBpsIK6yC52W25t012QUn0,2084
|
|
18
|
+
epub_generator/html_tag.py,sha256=P_Y0uRStCEEh7cCtpvK4t432NEcY9OLntAznvdxUF5k,343
|
|
19
|
+
epub_generator/i18n.py,sha256=-L6J6hsy796_IQ4nLpNtAeXIkRM6oFSWSHDlRZXW8aA,705
|
|
20
|
+
epub_generator/options.py,sha256=Er1dnaNvzDSnZRSRJGSqhkJsv1XtsCW2Ym_hUc8o_QI,181
|
|
21
|
+
epub_generator/template.py,sha256=RdN2QRICIrYMzpxCU_x4m4V9WWZEP9VvT6QLp2YCm90,1556
|
|
22
|
+
epub_generator/types.py,sha256=gBrdi1KYOVEnI0qEp1slLsyUw_Sd7v09uHvN8_Hf9Z8,4440
|
|
23
|
+
epub_generator-0.1.5.dist-info/LICENSE,sha256=9Zt_a4mrzkvR2rc0UbqTgbboIjWuumDFgeQyKos0H2E,1066
|
|
24
|
+
epub_generator-0.1.5.dist-info/METADATA,sha256=cwIGyOGFrt0hvtw_FHaaTjeoy-l-FP-SGZC4zP0MJyw,16555
|
|
25
|
+
epub_generator-0.1.5.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
|
26
|
+
epub_generator-0.1.5.dist-info/RECORD,,
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
epub_generator/__init__.py,sha256=CRtP7zjqNPxOA_m4S8Jgavuw_KgaKpoBW5kdgqUivLQ,648
|
|
2
|
-
epub_generator/context.py,sha256=AggXhRiOg70WZTLXF78LHA4coo3fa77OvKVe5wtgP6A,4495
|
|
3
|
-
epub_generator/data/container.xml.jinja,sha256=SkACyZgsAVUS5lmiCEhq3SpbFspYdyCnRNjWnLztLt0,252
|
|
4
|
-
epub_generator/data/content.opf.jinja,sha256=DDaR9GZnSBcpNk2BWUu56Uo_248TA91AxE4tKsBuKnQ,2839
|
|
5
|
-
epub_generator/data/cover.xhtml.jinja,sha256=heounlnHfOd-RNFIeytZQtAQ11ByPOiM1aB1lVyY6V4,328
|
|
6
|
-
epub_generator/data/mimetype.jinja,sha256=5GjjUNEUPrZI9gx7C9YDEQHsBUSjYcp07O8laskB9Is,20
|
|
7
|
-
epub_generator/data/nav.xhtml.jinja,sha256=FGunTu_cDJmSBxV8cfaIDjHVUNsyjogWg1jL4VK8ihU,1132
|
|
8
|
-
epub_generator/data/part.xhtml.jinja,sha256=FEQaUjHfCy7EJyyvYZj-6T-lkDcsmz1wvsk0b8LU3E0,558
|
|
9
|
-
epub_generator/data/style.css.jinja,sha256=HyGWoevaZD9xPDJeMQY_1xmM0f6aK0prmqoW3mhTGp0,1072
|
|
10
|
-
epub_generator/generation/__init__.py,sha256=UIscwHa8ocr2D1mk1KaP-zi3P1x9eYJzxTo0RJ2dnks,35
|
|
11
|
-
epub_generator/generation/gen_asset.py,sha256=0muwCvAohODC76F9G9_UBibRPQSpmMJkgQxlCsM7QcQ,4480
|
|
12
|
-
epub_generator/generation/gen_chapter.py,sha256=3f8i-QekhCHkCkRWeMbg1QYWmeNwKGsSNjeONrwMUhU,5346
|
|
13
|
-
epub_generator/generation/gen_epub.py,sha256=zkG0U5_g3FY-D6zkYGqp844IgWYJhbAqf6CnX2Do71Y,6412
|
|
14
|
-
epub_generator/generation/gen_nav.py,sha256=D-ZNsbm26AEAovbXtx1wSwTfH4Q8H2WYfoYeQ1Sb9bk,2813
|
|
15
|
-
epub_generator/generation/gen_toc.py,sha256=yt7GYu8Rfz9aw_GPZFUl9H3BKd1za1hSm2hhp8wyI68,2488
|
|
16
|
-
epub_generator/generation/xml_utils.py,sha256=xMcNZl8CaV21XYx2yeykkHhvnq5N7yRHfIFu5KRlRHc,1261
|
|
17
|
-
epub_generator/html_tag.py,sha256=P_Y0uRStCEEh7cCtpvK4t432NEcY9OLntAznvdxUF5k,343
|
|
18
|
-
epub_generator/i18n.py,sha256=GQjpHO7t8_0rXNuoYmO-G7_9nCF7S5kluBG0ip_2jIA,622
|
|
19
|
-
epub_generator/options.py,sha256=Er1dnaNvzDSnZRSRJGSqhkJsv1XtsCW2Ym_hUc8o_QI,181
|
|
20
|
-
epub_generator/template.py,sha256=RdN2QRICIrYMzpxCU_x4m4V9WWZEP9VvT6QLp2YCm90,1556
|
|
21
|
-
epub_generator/types.py,sha256=vxNXQ6ML-quf9fdybgfIe0HRcBL3zWwAm7SZGWKjw5g,3915
|
|
22
|
-
epub_generator-0.1.3.dist-info/LICENSE,sha256=9Zt_a4mrzkvR2rc0UbqTgbboIjWuumDFgeQyKos0H2E,1066
|
|
23
|
-
epub_generator-0.1.3.dist-info/METADATA,sha256=s74QvIsScCa87948JiwzLkRDvJI_yem5KvmizN7UXmY,16226
|
|
24
|
-
epub_generator-0.1.3.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
|
25
|
-
epub_generator-0.1.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|