epublication 0.5.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.
- epublication-0.5.0/LICENSE +20 -0
- epublication-0.5.0/PKG-INFO +399 -0
- epublication-0.5.0/README.md +376 -0
- epublication-0.5.0/pyproject.toml +45 -0
- epublication-0.5.0/pyproject.toml.orig +46 -0
- epublication-0.5.0/src/epublication/__init__.py +8 -0
- epublication-0.5.0/src/epublication/publication.py +274 -0
- epublication-0.5.0/src/epublication/py.typed +0 -0
- epublication-0.5.0/src/epublication/section.py +120 -0
- epublication-0.5.0/src/epublication/utils.py +13 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright 2026 Per Kraulis
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
“Software”), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: epublication
|
|
3
|
+
Version: 0.5.0
|
|
4
|
+
Summary: Simple package for generating a publication in EPUB3 format file.
|
|
5
|
+
Keywords: python,package,EPUB3
|
|
6
|
+
Author: Per Kraulis
|
|
7
|
+
Author-email: Per Kraulis <per.kraulis@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
17
|
+
Classifier: Topic :: Text Processing :: Markup :: HTML
|
|
18
|
+
Requires-Dist: minixml>=0.9.2
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Project-URL: Homepage, https://github.com/pekrau/epublication
|
|
21
|
+
Project-URL: Issues, https://github.com/pekrau/epublication/issues
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# epublication
|
|
25
|
+
|
|
26
|
+
Simple package for generating a publication in EPUB3 format file.
|
|
27
|
+
|
|
28
|
+
The `Publication` instance contains metadata and named CSS stylesheets.
|
|
29
|
+
The content is added in the form of instances of `Section`.
|
|
30
|
+
|
|
31
|
+
The table of contents can be generated automatically for a `Publication`.
|
|
32
|
+
It is based on the order of the added `Section` instances.
|
|
33
|
+
|
|
34
|
+
A `Section` instance must have a non-empty title. It will be shown
|
|
35
|
+
in the table of contents, and is by default displayed at the top of
|
|
36
|
+
the content.
|
|
37
|
+
|
|
38
|
+
The XHTML content of a `Section` can be set at creation, and/or
|
|
39
|
+
appended to the instance. It can be a string of pre-made XHTML code,
|
|
40
|
+
or an instance of `minixml.Element`.
|
|
41
|
+
|
|
42
|
+
By default, all stylesheets defined in the `Publication` instance
|
|
43
|
+
will be used for the content in a `Section`. It is possible to
|
|
44
|
+
set exactly which to use.
|
|
45
|
+
|
|
46
|
+
A `Section` instance may contain subsections, which will also
|
|
47
|
+
be included in the table of contents.
|
|
48
|
+
|
|
49
|
+
# Software
|
|
50
|
+
|
|
51
|
+
The code depends on the minixml package https://pypi.org/project/minixml/
|
|
52
|
+
|
|
53
|
+
This code has been lovingly hand-crafted. No AI tools were used in its development.
|
|
54
|
+
|
|
55
|
+
This code was written using the minimal EPUB3 example in
|
|
56
|
+
https://github.com/thansen0/sample-epub-minimal as a guide.
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
# License
|
|
60
|
+
|
|
61
|
+
[MIT](https://raw.githubusercontent.com/pekrau/epublication/refs/heads/main/LICENSE)
|
|
62
|
+
|
|
63
|
+
# To do
|
|
64
|
+
|
|
65
|
+
- 'add_image'
|
|
66
|
+
|
|
67
|
+
# Installation
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
pip install epublication
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The `minixml` will be installed automatically since it is a requirement.
|
|
74
|
+
|
|
75
|
+
# Usage
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
import epublication
|
|
79
|
+
import minixml
|
|
80
|
+
|
|
81
|
+
# By default, a CSS stylesheet named 'basic' is defined.
|
|
82
|
+
publication = epublication.Publication(
|
|
83
|
+
"Example document",
|
|
84
|
+
"some_id",
|
|
85
|
+
creators=["Per Kraulis"],
|
|
86
|
+
language="en",
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# Add content in the form of pre-made XHTML.
|
|
90
|
+
chapter1 = epublication.Section(
|
|
91
|
+
"Chapter 1",
|
|
92
|
+
content="""<p>Add some text in the form of <code>XHTML</code> code.</p>"""
|
|
93
|
+
)
|
|
94
|
+
publication.add_section(chapter1)
|
|
95
|
+
|
|
96
|
+
# Create `minixml.Element` tree explicitly and append to the section content.
|
|
97
|
+
ol = minixml.Element("ol")
|
|
98
|
+
ol.create("li").append("The first item.")
|
|
99
|
+
ol.create("li").append("The second item.")
|
|
100
|
+
ol.create("li").create("strong").append("The very important third item.")
|
|
101
|
+
chapter1.append(ol)
|
|
102
|
+
|
|
103
|
+
chapter2 = epublication.Section(
|
|
104
|
+
"Chapter 2",
|
|
105
|
+
content="""<p>The second chapter text.</p>"""
|
|
106
|
+
)
|
|
107
|
+
publication.add_section(chapter2)
|
|
108
|
+
|
|
109
|
+
# Add a subsection to chapter 2.
|
|
110
|
+
section = epublication.Section(
|
|
111
|
+
"A subsection of chapter 2",
|
|
112
|
+
add_title=False, # Do not automatically add the title.
|
|
113
|
+
content="""<h2>Use this title</h2><p>The text for the subsection.</p>"""
|
|
114
|
+
)
|
|
115
|
+
chapter2.add_section(section)
|
|
116
|
+
|
|
117
|
+
publication.write("example.epub")
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
The produced `example.epub` file can be found at the
|
|
121
|
+
[GitHub repository](https://github.com/pekrau/epublication).
|
|
122
|
+
|
|
123
|
+
# API
|
|
124
|
+
|
|
125
|
+
The `epublication` namespace contains the features most client software will need:
|
|
126
|
+
|
|
127
|
+
* [Publication](#publication-objects) (class)
|
|
128
|
+
* [Section](#section-objects) (class)
|
|
129
|
+
|
|
130
|
+
These are automatically imported into `epublication` from `epublication.publication`
|
|
131
|
+
and `epublication.section`, which modules in most cases will not have to be
|
|
132
|
+
imported directly by client software.
|
|
133
|
+
|
|
134
|
+
The following was generated using **pydoc-markdown**
|
|
135
|
+
https://github.com/NiklasRosenstein/pydoc-markdown
|
|
136
|
+
|
|
137
|
+
<a id="epublication"></a>
|
|
138
|
+
|
|
139
|
+
# epublication
|
|
140
|
+
|
|
141
|
+
Simple package for generating a publication in EPUB3 format file.
|
|
142
|
+
|
|
143
|
+
<a id="epublication.section"></a>
|
|
144
|
+
|
|
145
|
+
# epublication.section
|
|
146
|
+
|
|
147
|
+
Class Section; a chapter or subsection in a Publication instance.
|
|
148
|
+
|
|
149
|
+
<a id="epublication.section.Section"></a>
|
|
150
|
+
|
|
151
|
+
## Section Objects
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
class Section()
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Section in Publication; XHTML code container.
|
|
158
|
+
|
|
159
|
+
<a id="epublication.section.Section.__init__"></a>
|
|
160
|
+
|
|
161
|
+
#### \_\_init\_\_
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
def __init__(title, add_title=1, name=None, content=None, styles=True)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
- `title` must be unique in the publication.
|
|
168
|
+
- `name` if provided, must be safe, e.g. contain only ASCII letters, digits,
|
|
169
|
+
underscore and dash.
|
|
170
|
+
- `add_title` if non-zero, 'title' is added in an 'h' element as
|
|
171
|
+
the first element in the XHTML 'body' element, where the value
|
|
172
|
+
determines the `h` level.
|
|
173
|
+
- `styles` if True, then all styles defined for the `Publication` instance are used.
|
|
174
|
+
If a string (whitespace delimited), then use only those.
|
|
175
|
+
|
|
176
|
+
NOTE: The XHTML `body` element is automatically created.
|
|
177
|
+
|
|
178
|
+
<a id="epublication.section.Section.output"></a>
|
|
179
|
+
|
|
180
|
+
#### output
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
def output()
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Return the XHTML for this section.
|
|
187
|
+
The XHTML `html` and `head` elements are created, the styles added
|
|
188
|
+
and the pre-existing `body` element is appended.
|
|
189
|
+
|
|
190
|
+
<a id="epublication.section.Section.append"></a>
|
|
191
|
+
|
|
192
|
+
#### append
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
def append(content)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Append `content` to the `body` element.
|
|
199
|
+
|
|
200
|
+
- `content` is either a str or an `minixml.Element` instance.
|
|
201
|
+
If str, then it will be parsed using `minixml.parse`.
|
|
202
|
+
It must *not* contain the `html`, `head` and `body` elements;
|
|
203
|
+
those are created automatically and wrapped around the content.
|
|
204
|
+
|
|
205
|
+
<a id="epublication.section.Section.add_section"></a>
|
|
206
|
+
|
|
207
|
+
#### add\_section
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
def add_section(section: Section)
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Add a `Subsection` instance to this section.
|
|
214
|
+
The order of addition is significant for the table of contents.
|
|
215
|
+
|
|
216
|
+
<a id="epublication.section.Section.get_all_names"></a>
|
|
217
|
+
|
|
218
|
+
#### get\_all\_names
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
def get_all_names()
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Get all names for this section and its subsections, recursively.
|
|
225
|
+
|
|
226
|
+
<a id="epublication.section.Section.get_all_sections"></a>
|
|
227
|
+
|
|
228
|
+
#### get\_all\_sections
|
|
229
|
+
|
|
230
|
+
```python
|
|
231
|
+
def get_all_sections()
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Get all subsections recursively as list of sections.
|
|
235
|
+
|
|
236
|
+
<a id="epublication.publication"></a>
|
|
237
|
+
|
|
238
|
+
# epublication.publication
|
|
239
|
+
|
|
240
|
+
Class Publication containing metadata and the sections to output as an EPUB3 file.
|
|
241
|
+
|
|
242
|
+
<a id="epublication.publication.Publication"></a>
|
|
243
|
+
|
|
244
|
+
## Publication Objects
|
|
245
|
+
|
|
246
|
+
```python
|
|
247
|
+
class Publication()
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Publication class containing metadata, style sheets and sections with content.
|
|
251
|
+
|
|
252
|
+
<a id="epublication.publication.Publication.__init__"></a>
|
|
253
|
+
|
|
254
|
+
#### \_\_init\_\_
|
|
255
|
+
|
|
256
|
+
```python
|
|
257
|
+
def __init__(title: str,
|
|
258
|
+
identifier: str,
|
|
259
|
+
language: str = "sv",
|
|
260
|
+
creators: list[str] = [],
|
|
261
|
+
contributors: list[str] = [],
|
|
262
|
+
type: None | str = None,
|
|
263
|
+
source: None | str = None,
|
|
264
|
+
description: None | str = None,
|
|
265
|
+
subject: None | str = None,
|
|
266
|
+
publisher: None | str = None,
|
|
267
|
+
rights: None | str = None,
|
|
268
|
+
date: None | str = None,
|
|
269
|
+
toc: bool = True,
|
|
270
|
+
basic_style: bool = True)
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
Dublin Core metadata items are denoted (DC).
|
|
274
|
+
- `title` is the title for the publication; required. (DC)
|
|
275
|
+
- `identifier` should be a unique identifier for the publication; required. (DC)
|
|
276
|
+
- `language` is an ISO 639 language code. (DC)
|
|
277
|
+
- `creators` is a list of creator names (e.g. authors). (DC)
|
|
278
|
+
- `contributors` is a list of contributor names. (DC)
|
|
279
|
+
- `type` is the type of the publication. (DC)
|
|
280
|
+
- `source` is the source of the publication. (DC)
|
|
281
|
+
- `description` is a brief description of the publication. (DC)
|
|
282
|
+
- `subject` contains a list of keywords for the publication. (DC)
|
|
283
|
+
- `publisher` is the publisher of the publication. (DC)
|
|
284
|
+
- `rights` is the rights affirmation for the publication. (DC)
|
|
285
|
+
- `date` is the date of publication. Must be an ISO date `YYYY-MM-DD`. (DC)
|
|
286
|
+
- `toc` indicates whether a table of contents (TOC) is to be output.
|
|
287
|
+
- `basic_style` if True defines a simple CSS stylesheet called 'basic'.
|
|
288
|
+
|
|
289
|
+
<a id="epublication.publication.Publication.add_style"></a>
|
|
290
|
+
|
|
291
|
+
#### add\_style
|
|
292
|
+
|
|
293
|
+
```python
|
|
294
|
+
def add_style(name: str, css: str)
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
Add the named style CSS code.
|
|
298
|
+
|
|
299
|
+
<a id="epublication.publication.Publication.add_basic_style"></a>
|
|
300
|
+
|
|
301
|
+
#### add\_basic\_style
|
|
302
|
+
|
|
303
|
+
```python
|
|
304
|
+
def add_basic_style()
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
Add a style CSS file named 'basic', for convenience.
|
|
308
|
+
|
|
309
|
+
<a id="epublication.publication.Publication.add_toc_style"></a>
|
|
310
|
+
|
|
311
|
+
#### add\_toc\_style
|
|
312
|
+
|
|
313
|
+
```python
|
|
314
|
+
def add_toc_style(name)
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
Set the explicit style for the TOC. Added to the current list, if any.
|
|
318
|
+
|
|
319
|
+
<a id="epublication.publication.Publication.add_section"></a>
|
|
320
|
+
|
|
321
|
+
#### add\_section
|
|
322
|
+
|
|
323
|
+
```python
|
|
324
|
+
def add_section(section)
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
Add a Section instance having name, title, styles and XHTML content.
|
|
328
|
+
The order of addition is significant for the TOC.
|
|
329
|
+
|
|
330
|
+
<a id="epublication.publication.Publication.get_all_names"></a>
|
|
331
|
+
|
|
332
|
+
#### get\_all\_names
|
|
333
|
+
|
|
334
|
+
```python
|
|
335
|
+
def get_all_names()
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
Get names for all sections and their subsections, recursively.
|
|
339
|
+
|
|
340
|
+
<a id="epublication.publication.Publication.get_all_sections"></a>
|
|
341
|
+
|
|
342
|
+
#### get\_all\_sections
|
|
343
|
+
|
|
344
|
+
```python
|
|
345
|
+
def get_all_sections()
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
Get a list of all sections, including subsections, recursively.
|
|
349
|
+
|
|
350
|
+
<a id="epublication.publication.Publication.get_container_xml"></a>
|
|
351
|
+
|
|
352
|
+
#### get\_container\_xml
|
|
353
|
+
|
|
354
|
+
```python
|
|
355
|
+
def get_container_xml()
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
Return XML content for the `container.xml` file to be part of the EPUB3 file.
|
|
359
|
+
|
|
360
|
+
<a id="epublication.publication.Publication.get_content_opf"></a>
|
|
361
|
+
|
|
362
|
+
#### get\_content\_opf
|
|
363
|
+
|
|
364
|
+
```python
|
|
365
|
+
def get_content_opf()
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
Return XML content for the `content.opf` file to be part of the EPUB3 file.
|
|
369
|
+
|
|
370
|
+
<a id="epublication.publication.Publication.get_toc_xhtml"></a>
|
|
371
|
+
|
|
372
|
+
#### get\_toc\_xhtml
|
|
373
|
+
|
|
374
|
+
```python
|
|
375
|
+
def get_toc_xhtml()
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
Return the XHTML content for the TOC.
|
|
379
|
+
|
|
380
|
+
<a id="epublication.publication.Publication.get_toc_section_li"></a>
|
|
381
|
+
|
|
382
|
+
#### get\_toc\_section\_li
|
|
383
|
+
|
|
384
|
+
```python
|
|
385
|
+
def get_toc_section_li(section)
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
Return the entry in the table of contents for this section, recursively.
|
|
389
|
+
|
|
390
|
+
<a id="epublication.publication.Publication.write"></a>
|
|
391
|
+
|
|
392
|
+
#### write
|
|
393
|
+
|
|
394
|
+
```python
|
|
395
|
+
def write(file_or_path)
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
Write the EPUB3 file.
|
|
399
|
+
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
# epublication
|
|
2
|
+
|
|
3
|
+
Simple package for generating a publication in EPUB3 format file.
|
|
4
|
+
|
|
5
|
+
The `Publication` instance contains metadata and named CSS stylesheets.
|
|
6
|
+
The content is added in the form of instances of `Section`.
|
|
7
|
+
|
|
8
|
+
The table of contents can be generated automatically for a `Publication`.
|
|
9
|
+
It is based on the order of the added `Section` instances.
|
|
10
|
+
|
|
11
|
+
A `Section` instance must have a non-empty title. It will be shown
|
|
12
|
+
in the table of contents, and is by default displayed at the top of
|
|
13
|
+
the content.
|
|
14
|
+
|
|
15
|
+
The XHTML content of a `Section` can be set at creation, and/or
|
|
16
|
+
appended to the instance. It can be a string of pre-made XHTML code,
|
|
17
|
+
or an instance of `minixml.Element`.
|
|
18
|
+
|
|
19
|
+
By default, all stylesheets defined in the `Publication` instance
|
|
20
|
+
will be used for the content in a `Section`. It is possible to
|
|
21
|
+
set exactly which to use.
|
|
22
|
+
|
|
23
|
+
A `Section` instance may contain subsections, which will also
|
|
24
|
+
be included in the table of contents.
|
|
25
|
+
|
|
26
|
+
# Software
|
|
27
|
+
|
|
28
|
+
The code depends on the minixml package https://pypi.org/project/minixml/
|
|
29
|
+
|
|
30
|
+
This code has been lovingly hand-crafted. No AI tools were used in its development.
|
|
31
|
+
|
|
32
|
+
This code was written using the minimal EPUB3 example in
|
|
33
|
+
https://github.com/thansen0/sample-epub-minimal as a guide.
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# License
|
|
37
|
+
|
|
38
|
+
[MIT](https://raw.githubusercontent.com/pekrau/epublication/refs/heads/main/LICENSE)
|
|
39
|
+
|
|
40
|
+
# To do
|
|
41
|
+
|
|
42
|
+
- 'add_image'
|
|
43
|
+
|
|
44
|
+
# Installation
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
pip install epublication
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The `minixml` will be installed automatically since it is a requirement.
|
|
51
|
+
|
|
52
|
+
# Usage
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
import epublication
|
|
56
|
+
import minixml
|
|
57
|
+
|
|
58
|
+
# By default, a CSS stylesheet named 'basic' is defined.
|
|
59
|
+
publication = epublication.Publication(
|
|
60
|
+
"Example document",
|
|
61
|
+
"some_id",
|
|
62
|
+
creators=["Per Kraulis"],
|
|
63
|
+
language="en",
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
# Add content in the form of pre-made XHTML.
|
|
67
|
+
chapter1 = epublication.Section(
|
|
68
|
+
"Chapter 1",
|
|
69
|
+
content="""<p>Add some text in the form of <code>XHTML</code> code.</p>"""
|
|
70
|
+
)
|
|
71
|
+
publication.add_section(chapter1)
|
|
72
|
+
|
|
73
|
+
# Create `minixml.Element` tree explicitly and append to the section content.
|
|
74
|
+
ol = minixml.Element("ol")
|
|
75
|
+
ol.create("li").append("The first item.")
|
|
76
|
+
ol.create("li").append("The second item.")
|
|
77
|
+
ol.create("li").create("strong").append("The very important third item.")
|
|
78
|
+
chapter1.append(ol)
|
|
79
|
+
|
|
80
|
+
chapter2 = epublication.Section(
|
|
81
|
+
"Chapter 2",
|
|
82
|
+
content="""<p>The second chapter text.</p>"""
|
|
83
|
+
)
|
|
84
|
+
publication.add_section(chapter2)
|
|
85
|
+
|
|
86
|
+
# Add a subsection to chapter 2.
|
|
87
|
+
section = epublication.Section(
|
|
88
|
+
"A subsection of chapter 2",
|
|
89
|
+
add_title=False, # Do not automatically add the title.
|
|
90
|
+
content="""<h2>Use this title</h2><p>The text for the subsection.</p>"""
|
|
91
|
+
)
|
|
92
|
+
chapter2.add_section(section)
|
|
93
|
+
|
|
94
|
+
publication.write("example.epub")
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The produced `example.epub` file can be found at the
|
|
98
|
+
[GitHub repository](https://github.com/pekrau/epublication).
|
|
99
|
+
|
|
100
|
+
# API
|
|
101
|
+
|
|
102
|
+
The `epublication` namespace contains the features most client software will need:
|
|
103
|
+
|
|
104
|
+
* [Publication](#publication-objects) (class)
|
|
105
|
+
* [Section](#section-objects) (class)
|
|
106
|
+
|
|
107
|
+
These are automatically imported into `epublication` from `epublication.publication`
|
|
108
|
+
and `epublication.section`, which modules in most cases will not have to be
|
|
109
|
+
imported directly by client software.
|
|
110
|
+
|
|
111
|
+
The following was generated using **pydoc-markdown**
|
|
112
|
+
https://github.com/NiklasRosenstein/pydoc-markdown
|
|
113
|
+
|
|
114
|
+
<a id="epublication"></a>
|
|
115
|
+
|
|
116
|
+
# epublication
|
|
117
|
+
|
|
118
|
+
Simple package for generating a publication in EPUB3 format file.
|
|
119
|
+
|
|
120
|
+
<a id="epublication.section"></a>
|
|
121
|
+
|
|
122
|
+
# epublication.section
|
|
123
|
+
|
|
124
|
+
Class Section; a chapter or subsection in a Publication instance.
|
|
125
|
+
|
|
126
|
+
<a id="epublication.section.Section"></a>
|
|
127
|
+
|
|
128
|
+
## Section Objects
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
class Section()
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Section in Publication; XHTML code container.
|
|
135
|
+
|
|
136
|
+
<a id="epublication.section.Section.__init__"></a>
|
|
137
|
+
|
|
138
|
+
#### \_\_init\_\_
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
def __init__(title, add_title=1, name=None, content=None, styles=True)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
- `title` must be unique in the publication.
|
|
145
|
+
- `name` if provided, must be safe, e.g. contain only ASCII letters, digits,
|
|
146
|
+
underscore and dash.
|
|
147
|
+
- `add_title` if non-zero, 'title' is added in an 'h' element as
|
|
148
|
+
the first element in the XHTML 'body' element, where the value
|
|
149
|
+
determines the `h` level.
|
|
150
|
+
- `styles` if True, then all styles defined for the `Publication` instance are used.
|
|
151
|
+
If a string (whitespace delimited), then use only those.
|
|
152
|
+
|
|
153
|
+
NOTE: The XHTML `body` element is automatically created.
|
|
154
|
+
|
|
155
|
+
<a id="epublication.section.Section.output"></a>
|
|
156
|
+
|
|
157
|
+
#### output
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
def output()
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Return the XHTML for this section.
|
|
164
|
+
The XHTML `html` and `head` elements are created, the styles added
|
|
165
|
+
and the pre-existing `body` element is appended.
|
|
166
|
+
|
|
167
|
+
<a id="epublication.section.Section.append"></a>
|
|
168
|
+
|
|
169
|
+
#### append
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
def append(content)
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Append `content` to the `body` element.
|
|
176
|
+
|
|
177
|
+
- `content` is either a str or an `minixml.Element` instance.
|
|
178
|
+
If str, then it will be parsed using `minixml.parse`.
|
|
179
|
+
It must *not* contain the `html`, `head` and `body` elements;
|
|
180
|
+
those are created automatically and wrapped around the content.
|
|
181
|
+
|
|
182
|
+
<a id="epublication.section.Section.add_section"></a>
|
|
183
|
+
|
|
184
|
+
#### add\_section
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
def add_section(section: Section)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Add a `Subsection` instance to this section.
|
|
191
|
+
The order of addition is significant for the table of contents.
|
|
192
|
+
|
|
193
|
+
<a id="epublication.section.Section.get_all_names"></a>
|
|
194
|
+
|
|
195
|
+
#### get\_all\_names
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
def get_all_names()
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Get all names for this section and its subsections, recursively.
|
|
202
|
+
|
|
203
|
+
<a id="epublication.section.Section.get_all_sections"></a>
|
|
204
|
+
|
|
205
|
+
#### get\_all\_sections
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
def get_all_sections()
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Get all subsections recursively as list of sections.
|
|
212
|
+
|
|
213
|
+
<a id="epublication.publication"></a>
|
|
214
|
+
|
|
215
|
+
# epublication.publication
|
|
216
|
+
|
|
217
|
+
Class Publication containing metadata and the sections to output as an EPUB3 file.
|
|
218
|
+
|
|
219
|
+
<a id="epublication.publication.Publication"></a>
|
|
220
|
+
|
|
221
|
+
## Publication Objects
|
|
222
|
+
|
|
223
|
+
```python
|
|
224
|
+
class Publication()
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
Publication class containing metadata, style sheets and sections with content.
|
|
228
|
+
|
|
229
|
+
<a id="epublication.publication.Publication.__init__"></a>
|
|
230
|
+
|
|
231
|
+
#### \_\_init\_\_
|
|
232
|
+
|
|
233
|
+
```python
|
|
234
|
+
def __init__(title: str,
|
|
235
|
+
identifier: str,
|
|
236
|
+
language: str = "sv",
|
|
237
|
+
creators: list[str] = [],
|
|
238
|
+
contributors: list[str] = [],
|
|
239
|
+
type: None | str = None,
|
|
240
|
+
source: None | str = None,
|
|
241
|
+
description: None | str = None,
|
|
242
|
+
subject: None | str = None,
|
|
243
|
+
publisher: None | str = None,
|
|
244
|
+
rights: None | str = None,
|
|
245
|
+
date: None | str = None,
|
|
246
|
+
toc: bool = True,
|
|
247
|
+
basic_style: bool = True)
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Dublin Core metadata items are denoted (DC).
|
|
251
|
+
- `title` is the title for the publication; required. (DC)
|
|
252
|
+
- `identifier` should be a unique identifier for the publication; required. (DC)
|
|
253
|
+
- `language` is an ISO 639 language code. (DC)
|
|
254
|
+
- `creators` is a list of creator names (e.g. authors). (DC)
|
|
255
|
+
- `contributors` is a list of contributor names. (DC)
|
|
256
|
+
- `type` is the type of the publication. (DC)
|
|
257
|
+
- `source` is the source of the publication. (DC)
|
|
258
|
+
- `description` is a brief description of the publication. (DC)
|
|
259
|
+
- `subject` contains a list of keywords for the publication. (DC)
|
|
260
|
+
- `publisher` is the publisher of the publication. (DC)
|
|
261
|
+
- `rights` is the rights affirmation for the publication. (DC)
|
|
262
|
+
- `date` is the date of publication. Must be an ISO date `YYYY-MM-DD`. (DC)
|
|
263
|
+
- `toc` indicates whether a table of contents (TOC) is to be output.
|
|
264
|
+
- `basic_style` if True defines a simple CSS stylesheet called 'basic'.
|
|
265
|
+
|
|
266
|
+
<a id="epublication.publication.Publication.add_style"></a>
|
|
267
|
+
|
|
268
|
+
#### add\_style
|
|
269
|
+
|
|
270
|
+
```python
|
|
271
|
+
def add_style(name: str, css: str)
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
Add the named style CSS code.
|
|
275
|
+
|
|
276
|
+
<a id="epublication.publication.Publication.add_basic_style"></a>
|
|
277
|
+
|
|
278
|
+
#### add\_basic\_style
|
|
279
|
+
|
|
280
|
+
```python
|
|
281
|
+
def add_basic_style()
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Add a style CSS file named 'basic', for convenience.
|
|
285
|
+
|
|
286
|
+
<a id="epublication.publication.Publication.add_toc_style"></a>
|
|
287
|
+
|
|
288
|
+
#### add\_toc\_style
|
|
289
|
+
|
|
290
|
+
```python
|
|
291
|
+
def add_toc_style(name)
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
Set the explicit style for the TOC. Added to the current list, if any.
|
|
295
|
+
|
|
296
|
+
<a id="epublication.publication.Publication.add_section"></a>
|
|
297
|
+
|
|
298
|
+
#### add\_section
|
|
299
|
+
|
|
300
|
+
```python
|
|
301
|
+
def add_section(section)
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
Add a Section instance having name, title, styles and XHTML content.
|
|
305
|
+
The order of addition is significant for the TOC.
|
|
306
|
+
|
|
307
|
+
<a id="epublication.publication.Publication.get_all_names"></a>
|
|
308
|
+
|
|
309
|
+
#### get\_all\_names
|
|
310
|
+
|
|
311
|
+
```python
|
|
312
|
+
def get_all_names()
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
Get names for all sections and their subsections, recursively.
|
|
316
|
+
|
|
317
|
+
<a id="epublication.publication.Publication.get_all_sections"></a>
|
|
318
|
+
|
|
319
|
+
#### get\_all\_sections
|
|
320
|
+
|
|
321
|
+
```python
|
|
322
|
+
def get_all_sections()
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
Get a list of all sections, including subsections, recursively.
|
|
326
|
+
|
|
327
|
+
<a id="epublication.publication.Publication.get_container_xml"></a>
|
|
328
|
+
|
|
329
|
+
#### get\_container\_xml
|
|
330
|
+
|
|
331
|
+
```python
|
|
332
|
+
def get_container_xml()
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Return XML content for the `container.xml` file to be part of the EPUB3 file.
|
|
336
|
+
|
|
337
|
+
<a id="epublication.publication.Publication.get_content_opf"></a>
|
|
338
|
+
|
|
339
|
+
#### get\_content\_opf
|
|
340
|
+
|
|
341
|
+
```python
|
|
342
|
+
def get_content_opf()
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
Return XML content for the `content.opf` file to be part of the EPUB3 file.
|
|
346
|
+
|
|
347
|
+
<a id="epublication.publication.Publication.get_toc_xhtml"></a>
|
|
348
|
+
|
|
349
|
+
#### get\_toc\_xhtml
|
|
350
|
+
|
|
351
|
+
```python
|
|
352
|
+
def get_toc_xhtml()
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
Return the XHTML content for the TOC.
|
|
356
|
+
|
|
357
|
+
<a id="epublication.publication.Publication.get_toc_section_li"></a>
|
|
358
|
+
|
|
359
|
+
#### get\_toc\_section\_li
|
|
360
|
+
|
|
361
|
+
```python
|
|
362
|
+
def get_toc_section_li(section)
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
Return the entry in the table of contents for this section, recursively.
|
|
366
|
+
|
|
367
|
+
<a id="epublication.publication.Publication.write"></a>
|
|
368
|
+
|
|
369
|
+
#### write
|
|
370
|
+
|
|
371
|
+
```python
|
|
372
|
+
def write(file_or_path)
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
Write the EPUB3 file.
|
|
376
|
+
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "epublication"
|
|
3
|
+
version = "0.5.0"
|
|
4
|
+
description = "Simple package for generating a publication in EPUB3 format file."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.12"
|
|
9
|
+
dependencies = ["minixml>=0.9.2"]
|
|
10
|
+
keywords = [
|
|
11
|
+
"python",
|
|
12
|
+
"package",
|
|
13
|
+
"EPUB3",
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Programming Language :: Python :: 3.14",
|
|
22
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
23
|
+
"Topic :: Text Processing :: Markup :: HTML",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[[project.authors]]
|
|
27
|
+
name = "Per Kraulis"
|
|
28
|
+
email = "per.kraulis@gmail.com"
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/pekrau/epublication"
|
|
32
|
+
Issues = "https://github.com/pekrau/epublication/issues"
|
|
33
|
+
|
|
34
|
+
[build-system]
|
|
35
|
+
requires = ["uv_build>=0.12.21,<=0.13.10"]
|
|
36
|
+
build-backend = "uv_build"
|
|
37
|
+
|
|
38
|
+
[[tool.uv.index]]
|
|
39
|
+
name = "testpypi"
|
|
40
|
+
url = "https://test.pypi.org/simple/"
|
|
41
|
+
publish-url = "https://test.pypi.org/legacy/"
|
|
42
|
+
explicit = true
|
|
43
|
+
|
|
44
|
+
[dependency-groups]
|
|
45
|
+
dev = ["pytest>=9.2.0"]
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "epublication"
|
|
3
|
+
version = "0.5.0"
|
|
4
|
+
description = "Simple package for generating a publication in EPUB3 format file."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Per Kraulis", email = "per.kraulis@gmail.com" }
|
|
8
|
+
]
|
|
9
|
+
license = "MIT"
|
|
10
|
+
license-files = ["LICENSE"]
|
|
11
|
+
|
|
12
|
+
requires-python = ">=3.12"
|
|
13
|
+
dependencies = [
|
|
14
|
+
"minixml>=0.9.2",
|
|
15
|
+
]
|
|
16
|
+
keywords = ["python", "package", "EPUB3"]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Development Status :: 3 - Alpha",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Programming Language :: Python :: 3.14",
|
|
24
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
25
|
+
"Topic :: Text Processing :: Markup :: HTML"
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/pekrau/epublication"
|
|
30
|
+
Issues = "https://github.com/pekrau/epublication/issues"
|
|
31
|
+
|
|
32
|
+
[build-system]
|
|
33
|
+
requires = ["uv_build>=0.12.21,<=0.13.10"]
|
|
34
|
+
build-backend = "uv_build"
|
|
35
|
+
|
|
36
|
+
[[tool.uv.index]]
|
|
37
|
+
name = "testpypi"
|
|
38
|
+
url = "https://test.pypi.org/simple/"
|
|
39
|
+
publish-url = "https://test.pypi.org/legacy/"
|
|
40
|
+
explicit = true
|
|
41
|
+
|
|
42
|
+
[dependency-groups]
|
|
43
|
+
dev = [
|
|
44
|
+
"pytest>=9.2.0",
|
|
45
|
+
]
|
|
46
|
+
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
"Class Publication containing metadata and the sections to output as an EPUB3 file."
|
|
2
|
+
|
|
3
|
+
import datetime as dt
|
|
4
|
+
import zipfile
|
|
5
|
+
|
|
6
|
+
import minixml
|
|
7
|
+
|
|
8
|
+
from .utils import safe
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Publication:
|
|
12
|
+
"Publication class containing metadata, style sheets and sections with content."
|
|
13
|
+
|
|
14
|
+
def __init__(
|
|
15
|
+
self,
|
|
16
|
+
title: str,
|
|
17
|
+
identifier: str,
|
|
18
|
+
language: str = "sv",
|
|
19
|
+
creators: list[str] = [],
|
|
20
|
+
contributors: list[str] = [],
|
|
21
|
+
type: None | str = None,
|
|
22
|
+
source: None | str = None,
|
|
23
|
+
description: None | str = None,
|
|
24
|
+
subject: None | str = None,
|
|
25
|
+
publisher: None | str = None,
|
|
26
|
+
rights: None | str = None,
|
|
27
|
+
date: None | str = None,
|
|
28
|
+
toc: bool = True,
|
|
29
|
+
basic_style: bool = True,
|
|
30
|
+
):
|
|
31
|
+
"""Dublin Core metadata items are denoted (DC).
|
|
32
|
+
- `title` is the title for the publication; required. (DC)
|
|
33
|
+
- `identifier` should be a unique identifier for the publication; required. (DC)
|
|
34
|
+
- `language` is an ISO 639 language code. (DC)
|
|
35
|
+
- `creators` is a list of creator names (e.g. authors). (DC)
|
|
36
|
+
- `contributors` is a list of contributor names. (DC)
|
|
37
|
+
- `type` is the type of the publication. (DC)
|
|
38
|
+
- `source` is the source of the publication. (DC)
|
|
39
|
+
- `description` is a brief description of the publication. (DC)
|
|
40
|
+
- `subject` contains a list of keywords for the publication. (DC)
|
|
41
|
+
- `publisher` is the publisher of the publication. (DC)
|
|
42
|
+
- `rights` is the rights affirmation for the publication. (DC)
|
|
43
|
+
- `date` is the date of publication. Must be an ISO date `YYYY-MM-DD`. (DC)
|
|
44
|
+
- `toc` indicates whether a table of contents (TOC) is to be output.
|
|
45
|
+
- `basic_style` if True defines a simple CSS stylesheet called 'basic'.
|
|
46
|
+
"""
|
|
47
|
+
if not title:
|
|
48
|
+
raise ValueError("'title' may not be empty")
|
|
49
|
+
self.title = title
|
|
50
|
+
if not identifier:
|
|
51
|
+
raise ValueError("'identifier' may not be empty")
|
|
52
|
+
self.identifier = identifier
|
|
53
|
+
self.language = language
|
|
54
|
+
self.creators = creators
|
|
55
|
+
self.contributors = contributors
|
|
56
|
+
self.type = type
|
|
57
|
+
self.source = source
|
|
58
|
+
self.description = description
|
|
59
|
+
self.subject = subject
|
|
60
|
+
self.publisher = publisher
|
|
61
|
+
self.rights = rights
|
|
62
|
+
self.date = date
|
|
63
|
+
self.styles = {} # Key: name, value: CSS code.
|
|
64
|
+
if basic_style:
|
|
65
|
+
self.add_basic_style()
|
|
66
|
+
self.toc = toc
|
|
67
|
+
# All styles used for TOC. Else list of style names.
|
|
68
|
+
self.toc_styles = True
|
|
69
|
+
self.sections = {} # Key: name, value: Section instance.
|
|
70
|
+
|
|
71
|
+
def add_style(self, name: str, css: str):
|
|
72
|
+
"Add the named style CSS code."
|
|
73
|
+
if not name:
|
|
74
|
+
raise ValueError("'name' may not be empty")
|
|
75
|
+
if name != safe(name):
|
|
76
|
+
raise ValueError("'name' may not contain non-safe characters")
|
|
77
|
+
self.styles[name] = css
|
|
78
|
+
|
|
79
|
+
def add_basic_style(self):
|
|
80
|
+
"Add a style CSS file named 'basic', for convenience."
|
|
81
|
+
self.add_style(
|
|
82
|
+
"basic",
|
|
83
|
+
"""body {
|
|
84
|
+
font-family: sans-serif;
|
|
85
|
+
background-color: white;
|
|
86
|
+
}
|
|
87
|
+
blockquote {
|
|
88
|
+
font-family: serif;
|
|
89
|
+
margin: 2em;
|
|
90
|
+
padding-left: 1em;
|
|
91
|
+
border-left: 0.5em solid lightgray;
|
|
92
|
+
}
|
|
93
|
+
mark {
|
|
94
|
+
font-family: monospace;
|
|
95
|
+
background-color: white;
|
|
96
|
+
}
|
|
97
|
+
ol.toc {
|
|
98
|
+
list-style-type: none;
|
|
99
|
+
}
|
|
100
|
+
li.toc {
|
|
101
|
+
list-style-type: none;
|
|
102
|
+
}
|
|
103
|
+
""",
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
def add_toc_style(self, name):
|
|
107
|
+
"Set the explicit style for the TOC. Added to the current list, if any."
|
|
108
|
+
if name not in self.styles:
|
|
109
|
+
raise ValueError(f"style '{name}' not defined in publication")
|
|
110
|
+
if isinstance(self.toc_styles, list):
|
|
111
|
+
self.toc_styles.append(name)
|
|
112
|
+
else:
|
|
113
|
+
self.toc_style = [name]
|
|
114
|
+
|
|
115
|
+
def add_section(self, section):
|
|
116
|
+
"""Add a Section instance having name, title, styles and XHTML content.
|
|
117
|
+
The order of addition is significant for the TOC.
|
|
118
|
+
"""
|
|
119
|
+
assert section.publication is None
|
|
120
|
+
section_names = set(section.get_all_names())
|
|
121
|
+
publication_names = set(self.get_all_names())
|
|
122
|
+
names = ", ".join(
|
|
123
|
+
[f"'n'" for n in section_names.intersection(publication_names)]
|
|
124
|
+
)
|
|
125
|
+
if names:
|
|
126
|
+
raise ValueError(f"section name(s) {names} already in publication")
|
|
127
|
+
self.sections[section.name] = section
|
|
128
|
+
section.publication = self
|
|
129
|
+
|
|
130
|
+
def get_all_names(self):
|
|
131
|
+
"Get names for all sections and their subsections, recursively."
|
|
132
|
+
result = []
|
|
133
|
+
for section in self.sections.values():
|
|
134
|
+
result.extend(section.get_all_names())
|
|
135
|
+
return result
|
|
136
|
+
|
|
137
|
+
def get_all_sections(self):
|
|
138
|
+
"Get a list of all sections, including subsections, recursively."
|
|
139
|
+
result = []
|
|
140
|
+
for section in self.sections.values():
|
|
141
|
+
result.append(section)
|
|
142
|
+
result.extend(section.get_all_sections())
|
|
143
|
+
return result
|
|
144
|
+
|
|
145
|
+
def get_container_xml(self):
|
|
146
|
+
"Return XML content for the `container.xml` file to be part of the EPUB3 file."
|
|
147
|
+
container = minixml.Element(
|
|
148
|
+
"container",
|
|
149
|
+
version="1.0",
|
|
150
|
+
xmlns="urn:oasis:names:tc:opendocument:xmlns:container",
|
|
151
|
+
)
|
|
152
|
+
rootfile = container.create("rootfiles").create("rootfile")
|
|
153
|
+
rootfile["media-type"] = "application/oebps-package+xml"
|
|
154
|
+
rootfile["full-path"] = "OEBPS/content.opf"
|
|
155
|
+
return minixml.unparse(container)
|
|
156
|
+
|
|
157
|
+
def get_content_opf(self):
|
|
158
|
+
"Return XML content for the `content.opf` file to be part of the EPUB3 file."
|
|
159
|
+
publication = minixml.Element(
|
|
160
|
+
"package", xmlns="http://www.idpf.org/2007/opf", version="3.0"
|
|
161
|
+
)
|
|
162
|
+
publication["xmlns:dc"] = "http://purl.org/dc/elements/1.1/"
|
|
163
|
+
publication["unique-identifier"] = "identifier"
|
|
164
|
+
metadata = publication.create("metadata")
|
|
165
|
+
metadata.create("dc:title", id="title").append(self.title)
|
|
166
|
+
metadata.create("dc:identifier", id="identifier").append(self.identifier)
|
|
167
|
+
metadata.create("dc:language").append(self.language)
|
|
168
|
+
for pos, creator in enumerate(self.creators, start=1):
|
|
169
|
+
metadata.create("dc:creator", id=f"creator{pos}").append(creator)
|
|
170
|
+
for pos, contributor in enumerate(self.contributors, start=1):
|
|
171
|
+
metadata.create("dc:contributor", id=f"contributor{pos}").append(
|
|
172
|
+
contributor
|
|
173
|
+
)
|
|
174
|
+
if self.type:
|
|
175
|
+
metadata.create("dc:type").append(self.type)
|
|
176
|
+
if self.source:
|
|
177
|
+
metadata.create("dc:source").append(self.source)
|
|
178
|
+
if self.description:
|
|
179
|
+
metadata.create("dc:description").append(self.description)
|
|
180
|
+
if self.subject:
|
|
181
|
+
metadata.create("dc:subject").append(self.subject)
|
|
182
|
+
if self.publisher:
|
|
183
|
+
metadata.create("dc:publisher").append(self.publisher)
|
|
184
|
+
if self.rights:
|
|
185
|
+
metadata.create("dc:rights").append(self.rights)
|
|
186
|
+
if self.date:
|
|
187
|
+
metadata.create("dc:date").append(self.date)
|
|
188
|
+
metadata.create("meta", property="dcterms:modified").append(
|
|
189
|
+
dt.datetime.now(dt.UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
190
|
+
)
|
|
191
|
+
manifest = publication.create("manifest")
|
|
192
|
+
if self.toc:
|
|
193
|
+
item = manifest.create("item", id="toc", href="toc.xhtml", properties="nav")
|
|
194
|
+
item["media-type"] = "application/xhtml+xml"
|
|
195
|
+
for name in self.styles:
|
|
196
|
+
item = manifest.create("item", id=f"{name}_css", href=f"CSS/{name}.css")
|
|
197
|
+
item["media-type"] = "text/css"
|
|
198
|
+
for section in self.get_all_sections():
|
|
199
|
+
item = manifest.create(
|
|
200
|
+
"item", id=section.name, href=f"{section.name}.xhtml"
|
|
201
|
+
)
|
|
202
|
+
item["media-type"] = "application/xhtml+xml"
|
|
203
|
+
|
|
204
|
+
spine = publication.create("spine")
|
|
205
|
+
if self.toc:
|
|
206
|
+
spine.create("itemref", idref="toc")
|
|
207
|
+
for section in self.get_all_sections():
|
|
208
|
+
spine.create("itemref", idref=section.name)
|
|
209
|
+
return minixml.unparse(publication)
|
|
210
|
+
|
|
211
|
+
def get_toc_xhtml(self):
|
|
212
|
+
"Return the XHTML content for the TOC."
|
|
213
|
+
assert self.toc
|
|
214
|
+
publication = minixml.Element("html", xmlns="http://www.w3.org/1999/xhtml")
|
|
215
|
+
publication["xmlns:epub"] = "http://www.idpf.org/2007/ops"
|
|
216
|
+
head = publication.create("head")
|
|
217
|
+
head.create("title").append("TOC")
|
|
218
|
+
if self.toc_styles is True:
|
|
219
|
+
styles = list(self.styles.keys())
|
|
220
|
+
elif isinstance(self.toc_styles, list):
|
|
221
|
+
styles = self.toc_styles
|
|
222
|
+
else:
|
|
223
|
+
styles = []
|
|
224
|
+
for style in styles:
|
|
225
|
+
if style not in self.styles:
|
|
226
|
+
raise ValueError(f"TOC style '{style}' is not present in publication")
|
|
227
|
+
head.create(
|
|
228
|
+
"link", href=f"CSS/{style}.css", rel="stylesheet", type="text/css"
|
|
229
|
+
)
|
|
230
|
+
nav = publication.create("body").create("nav", id="toc")
|
|
231
|
+
nav["epub:type"] = "toc"
|
|
232
|
+
h1 = nav.create("h1")
|
|
233
|
+
h1["class"] = "toc" # XXX or frontmatter?
|
|
234
|
+
h1.append("Table of contents")
|
|
235
|
+
ol = nav.create("ol")
|
|
236
|
+
ol["class"] = "toc"
|
|
237
|
+
for section in self.sections.values():
|
|
238
|
+
ol.append(self.get_toc_section_li(section))
|
|
239
|
+
return minixml.unparse(publication)
|
|
240
|
+
|
|
241
|
+
def get_toc_section_li(self, section):
|
|
242
|
+
"Return the entry in the table of contents for this section, recursively."
|
|
243
|
+
li = minixml.Element("li")
|
|
244
|
+
li["class"] = "toc"
|
|
245
|
+
li.create("a", href=f"{section.name}.xhtml").append(section.title)
|
|
246
|
+
if section.subsections:
|
|
247
|
+
ol = li.create("ol")
|
|
248
|
+
ol["class"] = "toc"
|
|
249
|
+
for subsection in section.subsections.values():
|
|
250
|
+
ol.append(self.get_toc_section_li(subsection))
|
|
251
|
+
return li
|
|
252
|
+
|
|
253
|
+
def write(self, file_or_path):
|
|
254
|
+
"Write the EPUB3 file."
|
|
255
|
+
with zipfile.ZipFile(
|
|
256
|
+
file_or_path, mode="w", compression=zipfile.ZIP_DEFLATED, compresslevel=9
|
|
257
|
+
) as zip:
|
|
258
|
+
zip.writestr(
|
|
259
|
+
"mimetype",
|
|
260
|
+
"application/epub+zip",
|
|
261
|
+
compress_type=zipfile.ZIP_STORED,
|
|
262
|
+
compresslevel=0,
|
|
263
|
+
)
|
|
264
|
+
zip.writestr("META-INF/", "")
|
|
265
|
+
zip.writestr("META-INF/container.xml", self.get_container_xml())
|
|
266
|
+
zip.writestr("OEBPS/", "")
|
|
267
|
+
zip.writestr("OEBPS/CSS/", "")
|
|
268
|
+
for name, content in self.styles.items():
|
|
269
|
+
zip.writestr(f"OEBPS/CSS/{name}.css", content)
|
|
270
|
+
zip.writestr("OEBPS/content.opf", self.get_content_opf())
|
|
271
|
+
if self.toc:
|
|
272
|
+
zip.writestr("OEBPS/toc.xhtml", self.get_toc_xhtml())
|
|
273
|
+
for section in self.get_all_sections():
|
|
274
|
+
zip.writestr(f"OEBPS/{section.name}.xhtml", section.output())
|
|
File without changes
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"Class Section; a chapter or subsection in a Publication instance."
|
|
2
|
+
|
|
3
|
+
import minixml
|
|
4
|
+
|
|
5
|
+
from .utils import safe
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Section:
|
|
9
|
+
"Section in Publication; XHTML code container."
|
|
10
|
+
|
|
11
|
+
def __init__(self, title, add_title=1, name=None, content=None, styles=True):
|
|
12
|
+
"""- `title` must be unique in the publication.
|
|
13
|
+
- `name` if provided, must be safe, e.g. contain only ASCII letters, digits,
|
|
14
|
+
underscore and dash.
|
|
15
|
+
- `add_title` if positive, `title` is added in an `h` element as
|
|
16
|
+
the first element in the XHTML `body` element, where the value
|
|
17
|
+
determines the `h` level.
|
|
18
|
+
- `styles` if True, then all styles defined for the `Publication`
|
|
19
|
+
instance are used.
|
|
20
|
+
If a string (whitespace delimited), then use only those.
|
|
21
|
+
|
|
22
|
+
NOTE: The XHTML `body` element is automatically created.
|
|
23
|
+
"""
|
|
24
|
+
if not title:
|
|
25
|
+
raise ValueError("Section title must be non-empty string")
|
|
26
|
+
self.title = title
|
|
27
|
+
if name and name != safe(name):
|
|
28
|
+
raise ValueError("Section name must be safe, if given explicitly")
|
|
29
|
+
self.name = name or safe(title)
|
|
30
|
+
if isinstance(styles, str):
|
|
31
|
+
self.styles = [s for s in styles.split()]
|
|
32
|
+
else:
|
|
33
|
+
self.styles = styles
|
|
34
|
+
self.body = minixml.Element("body")
|
|
35
|
+
if add_title > 0:
|
|
36
|
+
self.body.create(f"h{min(add_title, 6)}").append(self.title)
|
|
37
|
+
if content:
|
|
38
|
+
self.append(content)
|
|
39
|
+
self.publication = None
|
|
40
|
+
self.subsections = {}
|
|
41
|
+
|
|
42
|
+
def output(self):
|
|
43
|
+
"""Return the XHTML for this section.
|
|
44
|
+
The XHTML `html` and `head` elements are created, the styles added
|
|
45
|
+
and the pre-existing `body` element is appended.
|
|
46
|
+
"""
|
|
47
|
+
root = minixml.Element("html", xmlns="http://www.w3.org/1999/xhtml")
|
|
48
|
+
root["xmlns:epub"] = "http://www.idpf.org/2007/ops"
|
|
49
|
+
head = root.create("head")
|
|
50
|
+
head.create("title").append(self.title)
|
|
51
|
+
styles = []
|
|
52
|
+
if self.styles is True:
|
|
53
|
+
if self.publication:
|
|
54
|
+
styles = list(self.publication.styles.keys())
|
|
55
|
+
elif isinstance(self.styles, list):
|
|
56
|
+
styles = self.styles
|
|
57
|
+
for style in styles:
|
|
58
|
+
if self.publication and style not in self.publication.styles:
|
|
59
|
+
raise ValueError(
|
|
60
|
+
f"section style '{style}' is not present in Publication"
|
|
61
|
+
)
|
|
62
|
+
head.create(
|
|
63
|
+
"link", href=f"CSS/{style}.css", rel="stylesheet", type="text/css"
|
|
64
|
+
)
|
|
65
|
+
root.append(self.body)
|
|
66
|
+
return minixml.unparse(root)
|
|
67
|
+
|
|
68
|
+
def append(self, content):
|
|
69
|
+
"""Append `content` to the `body` element.
|
|
70
|
+
|
|
71
|
+
- `content` is either a str or an `minixml.Element` instance.
|
|
72
|
+
If str, then it will be parsed using `minixml.parse`.
|
|
73
|
+
It must *not* contain the `html`, `head` and `body` elements;
|
|
74
|
+
those are created automatically and wrapped around the content.
|
|
75
|
+
"""
|
|
76
|
+
if isinstance(content, str):
|
|
77
|
+
for elem in minixml.parse(f"<tmp>{content}</tmp>").children:
|
|
78
|
+
if isinstance(elem, minixml.Element):
|
|
79
|
+
elem.free()
|
|
80
|
+
self.body.append(elem)
|
|
81
|
+
elif isinstance(content, minixml.Element):
|
|
82
|
+
self.body.append(content)
|
|
83
|
+
else:
|
|
84
|
+
raise ValueError(f"invalid content type: {type(content)}")
|
|
85
|
+
|
|
86
|
+
def add_section(self, section: Section):
|
|
87
|
+
"""Add a `Subsection` instance to this section.
|
|
88
|
+
The order of addition is significant for the table of contents.
|
|
89
|
+
"""
|
|
90
|
+
if section.publication:
|
|
91
|
+
raise ValueError(
|
|
92
|
+
f"subsection '{section}' is already part of '{publication}'"
|
|
93
|
+
)
|
|
94
|
+
if self.publication is None:
|
|
95
|
+
if section.name in self.get_all_names():
|
|
96
|
+
raise ValueError(
|
|
97
|
+
f"subsection name '{section.name}' is already in section"
|
|
98
|
+
)
|
|
99
|
+
else:
|
|
100
|
+
if section.name in self.publication.get_all_names():
|
|
101
|
+
raise ValueError(
|
|
102
|
+
f"subsection name '{section.name}' is already in publication"
|
|
103
|
+
)
|
|
104
|
+
self.subsections[section.name] = section
|
|
105
|
+
section.publication = self.publication
|
|
106
|
+
|
|
107
|
+
def get_all_names(self):
|
|
108
|
+
"Get all names for this section and its subsections, recursively."
|
|
109
|
+
result = [self.name]
|
|
110
|
+
for subsection in self.subsections.values():
|
|
111
|
+
result.extend(subsection.get_all_names())
|
|
112
|
+
return result
|
|
113
|
+
|
|
114
|
+
def get_all_sections(self):
|
|
115
|
+
"Get all subsections recursively as list of sections."
|
|
116
|
+
result = []
|
|
117
|
+
for subsection in self.subsections.values():
|
|
118
|
+
result.append(subsection)
|
|
119
|
+
result.extend(subsection.get_all_sections())
|
|
120
|
+
return result
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"Various utility functions."
|
|
2
|
+
|
|
3
|
+
import string
|
|
4
|
+
import unicodedata
|
|
5
|
+
|
|
6
|
+
# NOTE: Blank is not considered safe.
|
|
7
|
+
SAFE_CHARACTERS = set(string.ascii_letters + string.digits + "-_")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def safe(s):
|
|
11
|
+
"Make string safe: convert to ASCII and replace non-safe characters with '-'."
|
|
12
|
+
s = unicodedata.normalize("NFKD", s).encode("ASCII", "ignore").decode("utf-8")
|
|
13
|
+
return "".join([c if c in SAFE_CHARACTERS else "-" for c in s])
|