lit-reader 1.2.0__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.
- lit_reader/__init__.py +3 -0
- lit_reader/__main__.py +5 -0
- lit_reader/core.py +3377 -0
- lit_reader/epub.py +419 -0
- lit_reader/fonts/OFL.txt +93 -0
- lit_reader/fonts/literata-opsz-italic.woff2 +0 -0
- lit_reader/fonts/literata-opsz-normal.woff2 +0 -0
- lit_reader/mail.py +240 -0
- lit_reader/reader.css +443 -0
- lit_reader/reader.js +899 -0
- lit_reader-1.2.0.dist-info/METADATA +269 -0
- lit_reader-1.2.0.dist-info/RECORD +16 -0
- lit_reader-1.2.0.dist-info/WHEEL +4 -0
- lit_reader-1.2.0.dist-info/entry_points.txt +2 -0
- lit_reader-1.2.0.dist-info/licenses/LICENSE +21 -0
- lit_reader-1.2.0.dist-info/licenses/src/lit_reader/fonts/OFL.txt +93 -0
lit_reader/epub.py
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
"""EPUB 3 export of a finished reading copy, for Kindles and other e-readers.
|
|
2
|
+
|
|
3
|
+
The EPUB is made from the page lit-reader has already built, so every route
|
|
4
|
+
gives the same result. The text is split into one file per top-level section.
|
|
5
|
+
Sidenotes become pop-up footnotes where the reader supports them, the original
|
|
6
|
+
page numbers become EPUB page breaks with a page list, and embedded images
|
|
7
|
+
become files in the book. Settings, buttons and links to the local PDF are left
|
|
8
|
+
out, because an e-reader has its own.
|
|
9
|
+
"""
|
|
10
|
+
import base64
|
|
11
|
+
import datetime
|
|
12
|
+
import hashlib
|
|
13
|
+
import os
|
|
14
|
+
import re
|
|
15
|
+
import uuid
|
|
16
|
+
import zipfile
|
|
17
|
+
|
|
18
|
+
from .core import Node, attr, esc, image_kind, parse_html, short_citation
|
|
19
|
+
|
|
20
|
+
XHTML_NS = "http://www.w3.org/1999/xhtml"
|
|
21
|
+
MATH_NS = "http://www.w3.org/1998/Math/MathML"
|
|
22
|
+
KEEP = {"p", "em", "strong", "i", "b", "sup", "sub", "span", "a", "ul", "ol", "li", "table", "thead", "tbody",
|
|
23
|
+
"tfoot", "tr", "th", "td", "caption", "figure", "figcaption", "h2", "h3", "h4", "h5", "h6", "br", "hr",
|
|
24
|
+
"small", "code", "pre", "blockquote", "div", "img", "cite", "q", "u", "s", "dl", "dt", "dd", "section"}
|
|
25
|
+
SKIP = {"button", "script", "style", "summary", "nav", "input", "link", "meta"}
|
|
26
|
+
VOID_OUT = {"br", "hr", "img"}
|
|
27
|
+
PLAIN_ATTRS = {"id", "href", "src", "alt", "colspan", "rowspan", "width", "height", "value", "start"}
|
|
28
|
+
BAD_CHARS = re.compile(r"[\x00-\x08\x0b\x0c\x0e-\x1f]")
|
|
29
|
+
EXT = {"image/png": "png", "image/jpeg": "jpg", "image/gif": "gif"}
|
|
30
|
+
|
|
31
|
+
CSS = """body { margin: 0 4%; line-height: 1.5; }
|
|
32
|
+
body, p, li, h1, h2, h3, h4, td, th, figcaption { -webkit-hyphens: manual; -epub-hyphens: manual; adobe-hyphenate: none; hyphens: manual; }
|
|
33
|
+
h1 { font-size: 1.5em; line-height: 1.25; margin: 1em 0 .5em; }
|
|
34
|
+
h2 { font-size: 1.25em; margin: 1.6em 0 .5em; }
|
|
35
|
+
h3 { font-size: 1.1em; margin: 1.3em 0 .4em; }
|
|
36
|
+
h4 { font-size: 1em; font-style: italic; }
|
|
37
|
+
p { margin: 0 0 .8em; text-indent: 0; }
|
|
38
|
+
p.cont { margin-top: -.4em; }
|
|
39
|
+
.kicker, .meta, .source { font-size: .85em; }
|
|
40
|
+
.authors { font-weight: bold; }
|
|
41
|
+
div.pg { font-size: .75em; color: #777; text-align: right; margin: .2em 0; }
|
|
42
|
+
div.table-text { font-size: .85em; margin: 0 0 1.2em; }
|
|
43
|
+
figure { margin: 1.2em 0; }
|
|
44
|
+
figure img, .eq img { max-width: 100%; height: auto; }
|
|
45
|
+
figcaption { font-size: .85em; margin-top: .4em; }
|
|
46
|
+
table { border-collapse: collapse; font-size: .85em; margin: .8em 0; }
|
|
47
|
+
td, th { border: 1px solid #999; padding: .2em .4em; vertical-align: top; }
|
|
48
|
+
.eq { margin: .8em 0; text-align: center; }
|
|
49
|
+
aside { font-size: .9em; }
|
|
50
|
+
ol.refs li { margin-bottom: .4em; font-size: .9em; }
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def clean(text):
|
|
55
|
+
return BAD_CHARS.sub("", text)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def fix_id(i):
|
|
59
|
+
"""An XML id must start with a letter."""
|
|
60
|
+
i = re.sub(r"[^A-Za-z0-9_.-]", "-", i)
|
|
61
|
+
return i if re.match(r"[A-Za-z_]", i) else "i" + i
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class Book(object):
|
|
65
|
+
def __init__(self, meta, bundle=None):
|
|
66
|
+
self.meta = meta
|
|
67
|
+
self.bundle = bundle or {} # in a bundle's EPUB: each paper's page name -> (its folder, a label)
|
|
68
|
+
self.images = {} # sha -> (name, media type, bytes)
|
|
69
|
+
self.where = {} # id -> chapter file
|
|
70
|
+
self.pages = [] # (file, id, label)
|
|
71
|
+
|
|
72
|
+
def image(self, src):
|
|
73
|
+
"""Store a data-URI image and return its path in the book, or None."""
|
|
74
|
+
if not src.startswith("data:"):
|
|
75
|
+
return None
|
|
76
|
+
try:
|
|
77
|
+
data = base64.b64decode(src.split(",", 1)[1])
|
|
78
|
+
except (IndexError, ValueError):
|
|
79
|
+
return None
|
|
80
|
+
kind = image_kind(data)
|
|
81
|
+
if not kind:
|
|
82
|
+
return None
|
|
83
|
+
sha = hashlib.sha1(data).hexdigest()[:16]
|
|
84
|
+
if sha not in self.images:
|
|
85
|
+
self.images[sha] = ("images/%s.%s" % (sha, EXT[kind]), kind, data)
|
|
86
|
+
return self.images[sha][0]
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class Writer(object):
|
|
90
|
+
"""Serialises one chapter's nodes as XHTML."""
|
|
91
|
+
|
|
92
|
+
def __init__(self, book, fname):
|
|
93
|
+
self.book = book
|
|
94
|
+
self.fname = fname
|
|
95
|
+
self.notes = []
|
|
96
|
+
|
|
97
|
+
def href(self, h):
|
|
98
|
+
if h.startswith("#"):
|
|
99
|
+
target = fix_id(h[1:])
|
|
100
|
+
f = self.book.where.get(target)
|
|
101
|
+
if f is None:
|
|
102
|
+
return None
|
|
103
|
+
return ("#" if f == self.fname else f + "#") + target
|
|
104
|
+
if re.match(r"(?i)https?://|mailto:", h):
|
|
105
|
+
return h
|
|
106
|
+
return None # local files (the PDF, the index page) aren't in the book
|
|
107
|
+
|
|
108
|
+
def attrs(self, n, extra=""):
|
|
109
|
+
out = []
|
|
110
|
+
for k, v in n.attrs.items():
|
|
111
|
+
if k == "id":
|
|
112
|
+
out.append(' id="%s"' % attr(fix_id(v)))
|
|
113
|
+
elif k in PLAIN_ATTRS and k not in ("href", "src"):
|
|
114
|
+
out.append(' %s="%s"' % (k, attr(clean(v))))
|
|
115
|
+
return "".join(out) + extra
|
|
116
|
+
|
|
117
|
+
def kids(self, n):
|
|
118
|
+
return "".join(self.node(k) for k in n.kids)
|
|
119
|
+
|
|
120
|
+
def math(self, n, root=False):
|
|
121
|
+
if isinstance(n, str):
|
|
122
|
+
return esc(clean(n))
|
|
123
|
+
a = "".join(' %s="%s"' % (k, attr(clean(v))) for k, v in n.attrs.items()
|
|
124
|
+
if not k.startswith(("on", "data-")) and k not in ("style", "class") and re.fullmatch(r"[a-z][a-z-]*", k))
|
|
125
|
+
if root:
|
|
126
|
+
a = ' xmlns="%s"' % MATH_NS + a
|
|
127
|
+
inner = "".join(self.math(k) for k in n.kids)
|
|
128
|
+
return "<%s%s>%s</%s>" % (n.tag, a, inner, n.tag) if inner else "<%s%s/>" % (n.tag, a)
|
|
129
|
+
|
|
130
|
+
def node(self, n):
|
|
131
|
+
if isinstance(n, str):
|
|
132
|
+
return esc(clean(n))
|
|
133
|
+
t, c = n.tag, n.cls()
|
|
134
|
+
if t in SKIP:
|
|
135
|
+
return ""
|
|
136
|
+
if t == "math":
|
|
137
|
+
return self.math(n, root=True)
|
|
138
|
+
if "sn-mark" in c:
|
|
139
|
+
target = fix_id(n.attrs.get("data-for", ""))
|
|
140
|
+
shown = esc(clean(n.text().strip()))
|
|
141
|
+
return '<sup><a epub:type="noteref" href="#%s" id="nref-%s">%s</a></sup>' % (target, target, shown)
|
|
142
|
+
if "sidenote" in c:
|
|
143
|
+
num = n.find(lambda x: "n" in x.cls())
|
|
144
|
+
shown = num.text().strip() if num is not None else ""
|
|
145
|
+
if num is not None:
|
|
146
|
+
num.kids = []
|
|
147
|
+
self.notes.append((fix_id(n.attrs.get("id", "")), shown, self.kids(n)))
|
|
148
|
+
return ""
|
|
149
|
+
if "pg" in c and t == "span":
|
|
150
|
+
pid = fix_id(n.attrs.get("id", "pg"))
|
|
151
|
+
label = clean(n.text().strip())
|
|
152
|
+
self.book.pages.append((self.fname, pid, re.sub(r"^p\.\s*", "", label)))
|
|
153
|
+
return '<div class="pg" epub:type="pagebreak" role="doc-pagebreak" id="%s" title="%s">%s</div>' % (
|
|
154
|
+
pid, attr(re.sub(r"^p\.\s*", "", label)), esc(label))
|
|
155
|
+
if t == "details" and "orig-page" in c:
|
|
156
|
+
s = n.find(lambda x: x.tag == "summary")
|
|
157
|
+
name = re.sub(r"^Show the original page \((.*)\)$", r"\1", s.text().strip()) if s is not None else "page"
|
|
158
|
+
img = n.find(lambda x: x.tag == "img")
|
|
159
|
+
src = self.book.image(img.attrs.get("src", "")) if img is not None else None
|
|
160
|
+
if not src:
|
|
161
|
+
return ""
|
|
162
|
+
return ('<figure><img src="%s" alt="%s"/><figcaption>The original %s, as printed. Some text on this page '
|
|
163
|
+
'may not have come through.</figcaption></figure>') % (src, attr("The original " + name), esc(name))
|
|
164
|
+
if t == "details" and "table-text" in c: # keep its label: the summary is dropped like every summary
|
|
165
|
+
s = n.find(lambda x: x.tag == "summary")
|
|
166
|
+
return '<div class="table-text"><p><strong>%s</strong></p>%s</div>' % (
|
|
167
|
+
esc(s.text().strip() if s is not None else "Table as text"), self.kids(n))
|
|
168
|
+
if "in-bundle" in c: # a reference that is another paper of the bundle
|
|
169
|
+
a = n.find(lambda x: x.tag == "a")
|
|
170
|
+
to = self.book.bundle.get(a.attrs.get("href", "")) if a is not None else None
|
|
171
|
+
if not to: # a paper's own EPUB has no other paper to go to
|
|
172
|
+
return ""
|
|
173
|
+
return ' <em>In this book: <a href="../%s/title.xhtml">%s</a></em>' % (attr(to[0]), esc(to[1]))
|
|
174
|
+
if t == "a":
|
|
175
|
+
if "pdf-link" in c:
|
|
176
|
+
return ""
|
|
177
|
+
h = self.href(n.attrs.get("href", ""))
|
|
178
|
+
inner = self.kids(n)
|
|
179
|
+
return '<a href="%s">%s</a>' % (attr(h), inner) if h else inner
|
|
180
|
+
if t == "img":
|
|
181
|
+
src = self.book.image(n.attrs.get("src", ""))
|
|
182
|
+
if not src:
|
|
183
|
+
return ""
|
|
184
|
+
return '<img src="%s" alt="%s"%s/>' % (src, attr(clean(n.attrs.get("alt", ""))), "".join(
|
|
185
|
+
' %s="%s"' % (k, attr(n.attrs[k])) for k in ("width", "height") if re.fullmatch(r"\d+", n.attrs.get(k, ""))))
|
|
186
|
+
if t == "p" and any(isinstance(k, Node) and "chunk" in k.cls() for k in n.kids):
|
|
187
|
+
parts = [self.kids(k) if isinstance(k, Node) and "chunk" in k.cls() else self.node(k) for k in n.kids]
|
|
188
|
+
parts = [p for p in parts if p.strip()]
|
|
189
|
+
return "".join('<p%s>%s</p>' % (self.attrs(n) if i == 0 else ' class="cont"', p) for i, p in enumerate(parts))
|
|
190
|
+
if "tablewrap" in c or t == "details" or ("chunk" in c and t == "span"):
|
|
191
|
+
return self.kids(n)
|
|
192
|
+
if t not in KEEP:
|
|
193
|
+
return self.kids(n)
|
|
194
|
+
if t in VOID_OUT:
|
|
195
|
+
return "<%s/>" % t
|
|
196
|
+
keep_cls = ' class="eq"' if "eq" in c else ""
|
|
197
|
+
return "<%s%s>%s</%s>" % (t, self.attrs(n, keep_cls), self.kids(n), t)
|
|
198
|
+
|
|
199
|
+
def chapter(self, nodes):
|
|
200
|
+
body = "".join(self.node(k) for k in nodes)
|
|
201
|
+
if self.notes:
|
|
202
|
+
body += '<section epub:type="footnotes">%s</section>' % "".join(
|
|
203
|
+
'<aside epub:type="footnote" id="%s"><p><a href="#nref-%s">%s</a> %s</p></aside>' % (i, i, esc(s), h)
|
|
204
|
+
for i, s, h in self.notes)
|
|
205
|
+
return body
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def xhtml(title, body):
|
|
209
|
+
return ('<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html>\n'
|
|
210
|
+
'<html xmlns="%s" xmlns:epub="http://www.idpf.org/2007/ops" lang="en" xml:lang="en">\n'
|
|
211
|
+
'<head><meta charset="utf-8"/><title>%s</title><link rel="stylesheet" type="text/css" href="style.css"/></head>\n'
|
|
212
|
+
'<body>%s</body>\n</html>\n') % (XHTML_NS, esc(clean(title)), body)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def section_title(h):
|
|
216
|
+
return clean((h.attrs.get("data-title") or h.text()).strip()) or "Section"
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def build(page_html, meta, bundle=None):
|
|
220
|
+
"""(files, toc) for a finished page: files maps path in the book to bytes."""
|
|
221
|
+
root = parse_html(page_html)
|
|
222
|
+
main = root.find(lambda x: x.tag == "main")
|
|
223
|
+
if main is None:
|
|
224
|
+
raise ValueError("not a lit-reader page")
|
|
225
|
+
book = Book(meta, bundle)
|
|
226
|
+
title = clean(meta.get("title") or "Untitled paper")
|
|
227
|
+
|
|
228
|
+
head = main.find(lambda x: x.tag == "header")
|
|
229
|
+
front = []
|
|
230
|
+
if head is not None:
|
|
231
|
+
for k in head.children():
|
|
232
|
+
if "au-toggle" in k.cls() or k.tag == "button":
|
|
233
|
+
continue
|
|
234
|
+
for b in k.findall(lambda x: x.tag == "button"):
|
|
235
|
+
b.kids = []
|
|
236
|
+
for h in k.findall(lambda x: "au-rest" in x.cls()):
|
|
237
|
+
h.attrs.pop("hidden", None)
|
|
238
|
+
front.append(k)
|
|
239
|
+
|
|
240
|
+
chapters = [] # (file, title, nodes)
|
|
241
|
+
refs = None
|
|
242
|
+
cur = None
|
|
243
|
+
for k in main.children():
|
|
244
|
+
c = k.cls()
|
|
245
|
+
if k.tag == "header" or k.tag == "footer" or "done-box" in c or k.attrs.get("id") == "end-of-body":
|
|
246
|
+
continue
|
|
247
|
+
if "abstract" in c:
|
|
248
|
+
front.append(k)
|
|
249
|
+
continue
|
|
250
|
+
if "references" in c:
|
|
251
|
+
refs = k
|
|
252
|
+
continue
|
|
253
|
+
if k.tag == "h2" or cur is None:
|
|
254
|
+
carry = [] # a page that starts with this heading belongs to its chapter, not the end of the last one
|
|
255
|
+
while cur is not None and cur[2] and ("pg" in cur[2][-1].cls() or "orig-page" in cur[2][-1].cls()):
|
|
256
|
+
carry.insert(0, cur[2].pop())
|
|
257
|
+
cur = ("ch%02d.xhtml" % (len(chapters) + 1), section_title(k) if k.tag == "h2" else "Text", carry)
|
|
258
|
+
chapters.append(cur)
|
|
259
|
+
cur[2].append(k)
|
|
260
|
+
if len(chapters) > 1 and chapters[0][1] == "Text":
|
|
261
|
+
lead = " ".join(n.text() for n in chapters[0][2] if "pg" not in n.cls() and "orig-page" not in n.cls())
|
|
262
|
+
if len(lead.split()) < 30: # only a page number before the first heading: no chapter of its own
|
|
263
|
+
f, t, nodes = chapters[1]
|
|
264
|
+
chapters = [(chapters[0][0], t, chapters[0][2] + nodes)] + chapters[2:]
|
|
265
|
+
chapters = [("ch%02d.xhtml" % (i + 1), t, nodes) for i, (_, t, nodes) in enumerate(chapters)]
|
|
266
|
+
|
|
267
|
+
# where each id lives, so links can cross files
|
|
268
|
+
plan = [("title.xhtml", title, front)] + chapters
|
|
269
|
+
for f, _, nodes in plan:
|
|
270
|
+
for n in nodes:
|
|
271
|
+
for d in n.iter():
|
|
272
|
+
for key in ("id",):
|
|
273
|
+
if d.attrs.get(key):
|
|
274
|
+
book.where[fix_id(d.attrs[key])] = f
|
|
275
|
+
if "sn-mark" in d.cls():
|
|
276
|
+
book.where["nref-" + fix_id(d.attrs.get("data-for", ""))] = f
|
|
277
|
+
if refs is not None:
|
|
278
|
+
for d in refs.iter():
|
|
279
|
+
if d.attrs.get("id"):
|
|
280
|
+
book.where[fix_id(d.attrs["id"])] = "refs.xhtml"
|
|
281
|
+
|
|
282
|
+
files = {}
|
|
283
|
+
w = Writer(book, "title.xhtml")
|
|
284
|
+
kicker = head.find(lambda x: "kicker" in x.cls()) if head is not None else None
|
|
285
|
+
t_html = head.find(lambda x: x.tag == "h1") if head is not None else None
|
|
286
|
+
parts = ['<p class="kicker">%s</p>' % w.kids(kicker) if kicker is not None else "",
|
|
287
|
+
'<h1>%s</h1>' % (w.kids(t_html) if t_html is not None else esc(title))]
|
|
288
|
+
for k in front:
|
|
289
|
+
if k.tag in ("h1",) or "kicker" in k.cls():
|
|
290
|
+
continue
|
|
291
|
+
if "abstract" in k.cls():
|
|
292
|
+
parts.append(w.kids(k))
|
|
293
|
+
else:
|
|
294
|
+
cls = [x for x in ("authors", "meta", "part") if x in k.cls()]
|
|
295
|
+
inner = w.kids(k)
|
|
296
|
+
if inner.strip():
|
|
297
|
+
parts.append('<p%s>%s</p>' % (' class="%s"' % cls[0] if cls else "", inner))
|
|
298
|
+
foot = main.find(lambda x: x.tag == "footer")
|
|
299
|
+
if foot is not None:
|
|
300
|
+
parts.append('<p class="source">%s Converted to EPUB by lit-reader.</p>' % w.kids(foot))
|
|
301
|
+
files["title.xhtml"] = xhtml(title, "".join(parts) + w.chapter([]))
|
|
302
|
+
|
|
303
|
+
for f, t, nodes in chapters:
|
|
304
|
+
w = Writer(book, f)
|
|
305
|
+
files[f] = xhtml(t, w.chapter(nodes))
|
|
306
|
+
toc = [("title.xhtml", title)] + [(f, t) for f, t, _ in chapters]
|
|
307
|
+
if refs is not None:
|
|
308
|
+
w = Writer(book, "refs.xhtml")
|
|
309
|
+
items = refs.findall(lambda x: x.tag == "li")
|
|
310
|
+
body = '<h2>References</h2><ol class="refs">%s</ol>' % "".join("<li%s>%s</li>" % (w.attrs(li), w.kids(li)) for li in items)
|
|
311
|
+
files["refs.xhtml"] = xhtml("References", body + w.chapter([]))
|
|
312
|
+
toc.append(("refs.xhtml", "References"))
|
|
313
|
+
|
|
314
|
+
files["nav.xhtml"] = xhtml("Contents", (
|
|
315
|
+
'<nav epub:type="toc" id="toc"><h2>Contents</h2><ol>%s</ol></nav>' % "".join(
|
|
316
|
+
'<li><a href="%s">%s</a></li>' % (f, esc(t)) for f, t in toc)) + (
|
|
317
|
+
'<nav epub:type="page-list" hidden="hidden"><h2>Pages</h2><ol>%s</ol></nav>' % "".join(
|
|
318
|
+
'<li><a href="%s#%s">%s</a></li>' % (f, i, esc(l)) for f, i, l in book.pages) if book.pages else ""))
|
|
319
|
+
files["style.css"] = CSS
|
|
320
|
+
for name, kind, data in book.images.values():
|
|
321
|
+
files[name] = data
|
|
322
|
+
return files, toc
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
def identifier(meta):
|
|
326
|
+
if meta.get("doi"):
|
|
327
|
+
return "urn:doi:" + meta["doi"]
|
|
328
|
+
if meta.get("arxiv"):
|
|
329
|
+
return "urn:arxiv:" + meta["arxiv"]
|
|
330
|
+
return "urn:uuid:%s" % uuid.uuid5(uuid.NAMESPACE_URL, "lit-reader:" + str(meta.get("pid") or meta.get("title")))
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def opf(meta, files, toc):
|
|
334
|
+
media = {"xhtml": "application/xhtml+xml", "css": "text/css", "png": "image/png", "jpg": "image/jpeg", "gif": "image/gif"}
|
|
335
|
+
items, spine = [], []
|
|
336
|
+
order = [f for f, _ in toc]
|
|
337
|
+
for n, name in enumerate(sorted(files)):
|
|
338
|
+
ext = name.rsplit(".", 1)[-1]
|
|
339
|
+
props = []
|
|
340
|
+
if name == "nav.xhtml":
|
|
341
|
+
props.append("nav")
|
|
342
|
+
if ext == "xhtml" and "<math" in files[name]:
|
|
343
|
+
props.append("mathml")
|
|
344
|
+
items.append('<item id="f%d" href="%s" media-type="%s"%s/>' % (
|
|
345
|
+
n, attr(name), media[ext], ' properties="%s"' % " ".join(props) if props else ""))
|
|
346
|
+
ids = dict((name, "f%d" % n) for n, name in enumerate(sorted(files)))
|
|
347
|
+
spine = "".join('<itemref idref="%s"/>' % ids[f] for f in order)
|
|
348
|
+
creators = "".join('<dc:creator>%s</dc:creator>' % esc(clean(a)) for a in (meta.get("authors") or []))
|
|
349
|
+
date = '<dc:date>%s</dc:date>' % meta["year"] if meta.get("year") else ""
|
|
350
|
+
modified = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
351
|
+
return ('<?xml version="1.0" encoding="utf-8"?>\n'
|
|
352
|
+
'<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="uid" xml:lang="en">\n'
|
|
353
|
+
'<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">'
|
|
354
|
+
'<dc:identifier id="uid">%s</dc:identifier><dc:title>%s</dc:title>%s%s<dc:language>en</dc:language>'
|
|
355
|
+
'<meta property="dcterms:modified">%s</meta></metadata>\n'
|
|
356
|
+
'<manifest>%s</manifest>\n<spine>%s</spine>\n</package>\n') % (
|
|
357
|
+
esc(identifier(meta)), esc(clean(meta.get("title") or "Untitled paper")), creators, date, modified,
|
|
358
|
+
"".join(items), spine)
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
CONTAINER = ('<?xml version="1.0" encoding="utf-8"?>\n'
|
|
362
|
+
'<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">'
|
|
363
|
+
'<rootfiles><rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/></rootfiles>'
|
|
364
|
+
'</container>\n')
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def write_epub(page_html, meta, path):
|
|
368
|
+
files, toc = build(page_html, meta)
|
|
369
|
+
return write_zip(files, opf(meta, files, toc), path)
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def write_bundle(built, title, note, path, missing=()):
|
|
373
|
+
"""One book of several papers. `built` is a list of (page html, meta, page
|
|
374
|
+
name). Each paper keeps its own files in a folder of its own (p01/, p02/,
|
|
375
|
+
...), so its links and images work as in its own EPUB; a contents page
|
|
376
|
+
comes first, and the contents nest each paper's sections under it."""
|
|
377
|
+
bundle = {}
|
|
378
|
+
for i, (_, meta, name) in enumerate(built, 1):
|
|
379
|
+
bundle[name] = ("p%02d" % i, short_citation(meta)) # the paper as it's cited: "Tessler et al., 2024"
|
|
380
|
+
files, spine, nav, listing = {}, [], [], []
|
|
381
|
+
for page, meta, name in built:
|
|
382
|
+
folder = bundle[name][0]
|
|
383
|
+
try:
|
|
384
|
+
pf, toc = build(page, meta, bundle)
|
|
385
|
+
except ValueError as e:
|
|
386
|
+
print(" left out of the bundle EPUB: %s (%s)" % (meta.get("title"), e))
|
|
387
|
+
continue
|
|
388
|
+
for f, data in pf.items():
|
|
389
|
+
if f != "nav.xhtml":
|
|
390
|
+
files[folder + "/" + f] = data
|
|
391
|
+
spine += [folder + "/" + f for f, _ in toc]
|
|
392
|
+
t = clean(meta.get("title") or "Untitled paper")
|
|
393
|
+
nav.append('<li><a href="%s/title.xhtml">%s</a><ol>%s</ol></li>' % (folder, esc(t), "".join(
|
|
394
|
+
'<li><a href="%s/%s">%s</a></li>' % (folder, f, esc(st)) for f, st in toc[1:])))
|
|
395
|
+
who = ", ".join(clean(a) for a in (meta.get("authors") or [])[:3]) + (" et al." if len(meta.get("authors") or []) > 3 else "")
|
|
396
|
+
listing.append('<li><a href="%s/title.xhtml">%s</a>%s</li>' % (
|
|
397
|
+
folder, esc(t), esc(" · " + ", ".join(x for x in (who, str(meta.get("year") or "")) if x)) if who or meta.get("year") else ""))
|
|
398
|
+
if not spine:
|
|
399
|
+
raise ValueError("no paper could be added")
|
|
400
|
+
gone = ('<p class="meta">Not included, because %s couldn\'t be converted: %s.</p>' % (
|
|
401
|
+
"it" if len(missing) == 1 else "they", "; ".join(esc(clean(m)) for m in missing)) if missing else "")
|
|
402
|
+
files["index.xhtml"] = xhtml(title, '<h1>%s</h1>%s<ol>%s</ol>%s<p class="source">Made by lit-reader.</p>' % (
|
|
403
|
+
esc(clean(title)), '<p class="meta">%s</p>' % esc(clean(note)) if note else "", "".join(listing), gone))
|
|
404
|
+
files["style.css"] = CSS
|
|
405
|
+
files["nav.xhtml"] = xhtml("Contents", '<nav epub:type="toc" id="toc"><h2>Contents</h2><ol>%s</ol></nav>' % "".join(nav))
|
|
406
|
+
book = {"title": title, "authors": [], "pid": "bundle:" + "|".join(str(m.get("pid")) for _, m, _ in built)}
|
|
407
|
+
return write_zip(files, opf(book, files, [(f, "") for f in ["index.xhtml"] + spine]), path)
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def write_zip(files, package, path):
|
|
411
|
+
part = str(path) + ".part" # a failure leaves no half-written book where an e-reader would take it
|
|
412
|
+
with zipfile.ZipFile(part, "w") as z:
|
|
413
|
+
z.writestr(zipfile.ZipInfo("mimetype"), "application/epub+zip", compress_type=zipfile.ZIP_STORED)
|
|
414
|
+
z.writestr("META-INF/container.xml", CONTAINER, compress_type=zipfile.ZIP_DEFLATED)
|
|
415
|
+
z.writestr("OEBPS/content.opf", package, compress_type=zipfile.ZIP_DEFLATED)
|
|
416
|
+
for name, data in sorted(files.items()):
|
|
417
|
+
z.writestr("OEBPS/" + name, data, compress_type=zipfile.ZIP_DEFLATED)
|
|
418
|
+
os.replace(part, str(path))
|
|
419
|
+
return path
|
lit_reader/fonts/OFL.txt
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Copyright 2017 The Literata Project Authors (https://github.com/googlefonts/literata) Literata-Italic[opsz,wght].ttf: Copyright 2017 The Literata Project Authors (https://github.com/googlefonts/literata)
|
|
2
|
+
|
|
3
|
+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
|
4
|
+
This license is copied below, and is also available with a FAQ at:
|
|
5
|
+
http://scripts.sil.org/OFL
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
-----------------------------------------------------------
|
|
9
|
+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
|
10
|
+
-----------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
PREAMBLE
|
|
13
|
+
The goals of the Open Font License (OFL) are to stimulate worldwide
|
|
14
|
+
development of collaborative font projects, to support the font creation
|
|
15
|
+
efforts of academic and linguistic communities, and to provide a free and
|
|
16
|
+
open framework in which fonts may be shared and improved in partnership
|
|
17
|
+
with others.
|
|
18
|
+
|
|
19
|
+
The OFL allows the licensed fonts to be used, studied, modified and
|
|
20
|
+
redistributed freely as long as they are not sold by themselves. The
|
|
21
|
+
fonts, including any derivative works, can be bundled, embedded,
|
|
22
|
+
redistributed and/or sold with any software provided that any reserved
|
|
23
|
+
names are not used by derivative works. The fonts and derivatives,
|
|
24
|
+
however, cannot be released under any other type of license. The
|
|
25
|
+
requirement for fonts to remain under this license does not apply
|
|
26
|
+
to any document created using the fonts or their derivatives.
|
|
27
|
+
|
|
28
|
+
DEFINITIONS
|
|
29
|
+
"Font Software" refers to the set of files released by the Copyright
|
|
30
|
+
Holder(s) under this license and clearly marked as such. This may
|
|
31
|
+
include source files, build scripts and documentation.
|
|
32
|
+
|
|
33
|
+
"Reserved Font Name" refers to any names specified as such after the
|
|
34
|
+
copyright statement(s).
|
|
35
|
+
|
|
36
|
+
"Original Version" refers to the collection of Font Software components as
|
|
37
|
+
distributed by the Copyright Holder(s).
|
|
38
|
+
|
|
39
|
+
"Modified Version" refers to any derivative made by adding to, deleting,
|
|
40
|
+
or substituting -- in part or in whole -- any of the components of the
|
|
41
|
+
Original Version, by changing formats or by porting the Font Software to a
|
|
42
|
+
new environment.
|
|
43
|
+
|
|
44
|
+
"Author" refers to any designer, engineer, programmer, technical
|
|
45
|
+
writer or other person who contributed to the Font Software.
|
|
46
|
+
|
|
47
|
+
PERMISSION & CONDITIONS
|
|
48
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
49
|
+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
|
50
|
+
redistribute, and sell modified and unmodified copies of the Font
|
|
51
|
+
Software, subject to the following conditions:
|
|
52
|
+
|
|
53
|
+
1) Neither the Font Software nor any of its individual components,
|
|
54
|
+
in Original or Modified Versions, may be sold by itself.
|
|
55
|
+
|
|
56
|
+
2) Original or Modified Versions of the Font Software may be bundled,
|
|
57
|
+
redistributed and/or sold with any software, provided that each copy
|
|
58
|
+
contains the above copyright notice and this license. These can be
|
|
59
|
+
included either as stand-alone text files, human-readable headers or
|
|
60
|
+
in the appropriate machine-readable metadata fields within text or
|
|
61
|
+
binary files as long as those fields can be easily viewed by the user.
|
|
62
|
+
|
|
63
|
+
3) No Modified Version of the Font Software may use the Reserved Font
|
|
64
|
+
Name(s) unless explicit written permission is granted by the corresponding
|
|
65
|
+
Copyright Holder. This restriction only applies to the primary font name as
|
|
66
|
+
presented to the users.
|
|
67
|
+
|
|
68
|
+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
|
69
|
+
Software shall not be used to promote, endorse or advertise any
|
|
70
|
+
Modified Version, except to acknowledge the contribution(s) of the
|
|
71
|
+
Copyright Holder(s) and the Author(s) or with their explicit written
|
|
72
|
+
permission.
|
|
73
|
+
|
|
74
|
+
5) The Font Software, modified or unmodified, in part or in whole,
|
|
75
|
+
must be distributed entirely under this license, and must not be
|
|
76
|
+
distributed under any other license. The requirement for fonts to
|
|
77
|
+
remain under this license does not apply to any document created
|
|
78
|
+
using the Font Software.
|
|
79
|
+
|
|
80
|
+
TERMINATION
|
|
81
|
+
This license becomes null and void if any of the above conditions are
|
|
82
|
+
not met.
|
|
83
|
+
|
|
84
|
+
DISCLAIMER
|
|
85
|
+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
86
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
|
87
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
|
88
|
+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
|
89
|
+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
90
|
+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
|
91
|
+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
92
|
+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
|
93
|
+
OTHER DEALINGS IN THE FONT SOFTWARE.
|
|
Binary file
|
|
Binary file
|