-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
x_ray_epub.py
192 lines (168 loc) · 7.49 KB
/
x_ray_epub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
import re
import shutil
import zipfile
from collections import defaultdict
from html import escape
from pathlib import Path
try:
from .mediawiki import MAX_EXLIMIT, SCORE_THRESHOLD
except ImportError:
from mediawiki import MAX_EXLIMIT, SCORE_THRESHOLD
NAMESPACES = {
'n': 'urn:oasis:names:tc:opendocument:xmlns:container',
'opf': 'http://www.idpf.org/2007/opf',
'ops': 'http://www.idpf.org/2007/ops',
None: 'http://www.w3.org/1999/xhtml'
}
class X_Ray_EPUB:
def __init__(self, book_path, search_people, mediawiki):
self.book_path = book_path
self.search_people = search_people
self.mediawiki = mediawiki
self.num_ents = 0
self.ent_dic = defaultdict(list)
self.entities = {}
self.pending_dic = {}
self.extract_folder = Path(book_path).with_name('extract')
if self.extract_folder.exists():
shutil.rmtree(self.extract_folder)
self.content_folder = None
self.xhtml_folder = None
def extract_epub(self):
from lxml import etree
with zipfile.ZipFile(self.book_path) as zf:
zf.extractall(self.extract_folder)
with self.extract_folder.joinpath(
'META-INF/container.xml').open('rb') as f:
root = etree.fromstring(f.read())
self.opf_path = root.find(
'.//n:rootfile', NAMESPACES).get("full-path")
content_folder = Path(self.opf_path).parent.name
if content_folder:
self.content_folder = content_folder
else:
for folder in ['OEBPS', 'epub']:
if self.extract_folder.joinpath(folder).is_dir():
self.content_folder = folder
with self.extract_folder.joinpath(self.opf_path).open('rb') as opf:
self.opf_root = etree.fromstring(opf.read())
item_path = 'opf:manifest/opf:item' \
'[@media-type="application/xhtml+xml"]'
for item in self.opf_root.findall(item_path, NAMESPACES):
if item.get('properties') == 'nav':
continue
xhtml = item.get("href")
xhtml_folder = Path(xhtml).parent.name
if xhtml_folder and xhtml_folder != self.xhtml_folder \
and xhtml_folder != self.content_folder:
self.xhtml_folder = xhtml_folder
if not xhtml.startswith(self.content_folder):
xhtml = f'{self.content_folder}/{xhtml}'
xhtml_path = self.extract_folder.joinpath(xhtml)
if xhtml_path.exists():
with xhtml_path.open() as f:
xhtml_str = f.read()
body_start = xhtml_str.index('<body')
body_end = xhtml_str.index('</body>') + len('</body>')
body_str = xhtml_str[body_start:body_end]
for m in re.finditer(r'>[^<]+<', body_str):
yield (m.group(0)[1:-1], (m.start() + 1, xhtml))
def search(self, name, is_person, sent, start, end, xhtml):
from rapidfuzz.process import extractOne
if (r := extractOne(
name, self.entities.keys(), score_cutoff=SCORE_THRESHOLD)):
ent_id = self.entities[r[0]]['id']
else:
ent_id = self.num_ents
self.num_ents += 1
self.entities[name] = {
'id': ent_id, 'summary': sent, 'quote': True}
if not is_person or self.search_people:
if name in self.mediawiki.cache_dic and \
self.mediawiki.cache_dic[name]:
self.update_summary(name, self.mediawiki.cache_dic[name])
else:
self.pending_dic[name] = None
if len(self.pending_dic) == MAX_EXLIMIT:
self.mediawiki.query(
self.pending_dic, self.update_summary)
self.pending_dic.clear()
self.ent_dic[xhtml_path].append((start, end, name, ent_id))
def update_summary(self, key, summary):
self.entities[key]['summary'] = summary
self.entities[key]['quote'] = False
def modify_epub(self):
self.insert_a_tags()
self.create_x_ray_page()
self.mediawiki.save_cache()
def insert_a_tags(self):
for xhtml, ent_list in self.ent_dic.items():
with self.extract_folder.joinpath(xhtml).open() as f:
xhtml_str = f.read()
body_start = xhtml_str.index('<body')
body_end = xhtml_str.index('</body>') + len('</body>')
body_str = xhtml_str[body_start:body_end]
s = ''
last_end = 0
for data in ent_list:
start, end, name, ent_id = data
s += body_str[last_end:start]
s += f'<a epub:type="noteref" href="x_ray.xhtml#{ent_id}">'
s += f'{name}</a>'
last_end = end
s += body_str[last_end:]
new_xhtml_str = xhtml_str[:body_start] + s + xhtml_str[body_end:]
with self.extract_folder.joinpath(xhtml).open('w') as f:
if NAMESPACES['ops'] not in new_xhtml_str:
# add epub namespace
new_xhtml_str = new_xhtml_str.replace(
f'xmlns="{NAMESPACES[None]}"',
f'xmlns="{NAMESPACES[None]}" '
f'xmlns:epub="{NAMESPACES["ops"]}"')
f.write(new_xhtml_str)
def create_x_ray_page(self):
from lxml import etree
s = '''
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:epub="http://www.idpf.org/2007/ops"
lang="en-US" xml:lang="en-US">
<head><title>X-Ray</title><meta charset="utf-8"/></head>
<body>
'''
for entity, data in self.entities.items():
s += f'''
<aside id="{data["id"]}" epub:type="footnote">
{escape(data["summary"])}
'''
if not data['quote']:
s += f'''
<a href="{self.mediawiki.source_link}{entity}">
{self.mediawiki.source_name}</a>
'''
s += '</aside>'
s += '</body></html>'
if self.xhtml_folder:
x_ray_href = f'{self.xhtml_folder}/x_ray.xhtml'
x_ray_path = self.extract_folder.joinpath(
f'{self.content_folder}/{x_ray_href}')
else:
x_ray_href = f'{self.content_folder}/x_ray.xhtml'
x_ray_path = self.extract_folder.joinpath(x_ray_href)
with x_ray_path.open('w') as f:
f.write(s)
manifest = self.opf_root.find('opf:manifest', NAMESPACES)
s = f'<item href="{x_ray_href}" id="x_ray.xhtml" '\
'media-type="application/xhtml+xml"/>'
manifest.append(etree.fromstring(s))
spine = self.opf_root.find('opf:spine', NAMESPACES)
s = '<itemref idref="x_ray.xhtml"/>'
spine.append(etree.fromstring(s))
with self.extract_folder.joinpath(self.opf_path).open('w') as f:
f.write(etree.tostring(self.opf_root, encoding=str))
self.book_path = Path(self.book_path)
shutil.make_archive(self.extract_folder, 'zip', self.extract_folder)
shutil.move(
self.extract_folder.with_suffix('.zip'),
self.book_path.with_name(f'{self.book_path.stem}_x_ray.epub'))
shutil.rmtree(self.extract_folder)