forked from apeyser/tab2opf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tab2opf.py
executable file
·307 lines (249 loc) · 8.42 KB
/
tab2opf.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python3
# coding: utf-8
# Copyright (C) 2007 - Klokan Petr Přidal (www.klokan.cz)
# Copyright (C) 2015 - Alexander Peyser (github.com/apeyser)
# Copyright (C) 2020 - Nicolas Garanis (github.com/nyg)
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import argparse
import importlib
import os
from contextlib import contextmanager
from itertools import islice, count, groupby
import uuid
# Define command-line arguments.
parser = argparse.ArgumentParser("tab2opf")
parser.add_argument("-v", "--verbose", help="print verbose output", action="store_true")
parser.add_argument("-m", "--module", help="import module for mapping, getkey, getdef")
parser.add_argument("-s", "--source", metavar='LANG', default="en", help="source language (e.g. en, fr, de)")
parser.add_argument("-t", "--target", metavar='LANG', default="en", help="target language (e.g. en, fr, de)")
parser.add_argument("-o", "--title", required=True, help="title of the dictionary")
parser.add_argument("file", help="input tab file")
args = parser.parse_args()
VERBOSE = args.verbose
MODULE = args.module
SRC_LANG = args.source
TRG_LANG = args.target
DICT_TITLE = args.title
TAB_FILE = args.file
# Stop with the encoding -- it's broken anyhow
# in the kindles and undefined.
def normalize_letter(ch):
try:
ch = mapping[ch]
except KeyError:
pass
return ch
def normalize_unicode(text):
return ''.join(normalize_letter(c) for c in text)
def loadmember(mod, attr, dfault):
if hasattr(mod, attr):
print("Loading {} from {}".format(attr, mod.__name__))
globals()[attr] = getattr(mod, attr)
else:
globals()[attr] = dfault
def importmod():
global MODULE
if MODULE is None:
mod = None
else:
mod = importlib.import_module(MODULE)
print("Loading methods from: {}".format(mod.__file__))
loadmember(mod, 'getkey', lambda key: key)
loadmember(mod, 'getdef', lambda dfn: dfn)
loadmember(mod, 'mapping', {})
importmod()
# add a single [term, definition]
# to defs[key]
# r is a tab split line
def readkey(r, defs):
try:
term, defn = r.split('\t', 1)
except ValueError:
print("Bad line: '{}'".format(r))
raise
term = term.strip()
defn = getdef(defn)
nkey = normalize_unicode(term)
key = getkey(nkey)
key = key. \
replace('"', "'"). \
replace('<', '\\<'). \
replace('>', '\\>'). \
lower().strip()
nkey = nkey. \
replace('"', "'"). \
replace('<', '\\<'). \
replace('>', '\\>'). \
lower().strip()
if key == '':
raise Exception("Missing key {}".format(term))
if defn == '':
raise Exception("Missing definition {}".format(term))
if VERBOSE:
print(key, ":", term)
ndef = [term, defn, key == nkey]
if key in defs:
defs[key].append(ndef)
else:
defs[key] = [ndef]
# Skip empty lines and lines that only have a comment
def inclline(s):
s = s.lstrip()
return len(s) != 0 and s[0] != '#'
# Iterate over FILENAME, reading lines of
# term {tab} definition
# skips empty lines and commented out lines
def readkeys():
if VERBOSE:
print("Reading {}".format(TAB_FILE))
with open(TAB_FILE, 'r', encoding='utf-8') as fr:
defns = {}
for r in filter(inclline, fr):
readkey(r, defns)
return defns
# Write to key file {name}{n}.html
# put the body inside the context manager
# The onclick here gives a kindlegen warning
# but appears to be necessary to actually
# have a lookup dictionary
@contextmanager
def writekeyfile(i):
fname = 'dictionary-{}-{}-{}.html'.format(SRC_LANG, TRG_LANG, i).lower()
if VERBOSE:
print("Key file: {}".format(fname))
with open(fname, 'w', encoding="utf-8") as to:
to.write("""<html xmlns:mbp="https://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf"
xmlns:idx="https://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<mbp:pagebreak/>
<mbp:frameset>
<mbp:slave-frame display="bottom" device="all" breadth="auto" leftmargin="0" rightmargin="0" bottommargin="0" topmargin="0">
<div align="center" bgcolor="yellow">
<a onclick="index_search()">Dictionary Search</a>
</div>
</mbp:slave-frame>
<mbp:pagebreak/>
""")
try:
yield to
finally:
to.write("""
</mbp:frameset>
</body>
</html>
""")
# Order definitions by keys, then by whether the key
# matches the original term, then by length of term
# then alphabetically
def keyf(defn):
term = defn[0]
if defn[2]:
l = 0
else:
l = len(term)
return l, term
# Write into to the key, definition pairs
# key -> [[term, defn, key==term]]
def writekey(to, key, defn):
terms = iter(sorted(defn, key=keyf))
for term, g in groupby(terms, key=lambda d: d[0]):
to.write(
"""
<idx:entry name="word" scriptable="yes">
<idx:orth value="{key}"><strong>{term}</strong></idx:orth>
""".format(term=term, key=key))
to.write('; '.join(ndefn for _, ndefn, _ in g))
to.write(
""" </idx:entry>
"""
)
if VERBOSE:
print(key)
# Write all the keys, where defns is a map of
# key --> [[term, defn, key==term]...]
# and name is the basename
# The files are split so that there are no more than
# 10,000 keys written to each file (why?? I dunno)
#
# Returns the number of files.
def writekeys(defns):
keyit = iter(sorted(defns))
for j in count():
keys = list(islice(keyit, 10000))
if len(keys) == 0:
break
else:
with writekeyfile(j) as to:
for key in keys:
writekey(to, key, defns[key])
return j
# After writing keys, the opf that references all the key files
# is constructed.
# openopf wraps the contents of writeopf
@contextmanager
def openopf():
fname = 'dictionary-{}-{}.opf'.format(SRC_LANG, TRG_LANG).lower()
if VERBOSE: print("Opf: {}".format(fname))
with open(fname, 'w') as to:
to.write("""<?xml version="1.0" encoding="UTF-8"?>
<package version="2.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="uid">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:identifier id="uid">{uuid}</dc:identifier>
<dc:title>{title}</dc:title>
<dc:language>{source}</dc:language>
<x-metadata>
<DictionaryInLanguage>{source}</DictionaryInLanguage>
<DictionaryOutLanguage>{target}</DictionaryOutLanguage>
</x-metadata>
</metadata>
<manifest>""".format(uuid=uuid.uuid4(), source=SRC_LANG, target=TRG_LANG, title=DICT_TITLE))
yield to
to.write("""
<guide>
<reference type="search" title="Dictionary Search" onclick= "index_search()"/>
</guide>
</package>
""")
# Write the opf that describes all the key files
def writeopf(ndicts, name):
with openopf() as to:
for i in range(ndicts):
to.write("""
<item id="dictionary{ndict}" href="dictionary-{src}-{trg}-{ndict}.html" media-type="application/xhtml+xml"/>""".format(ndict=i, src=SRC_LANG, trg=TRG_LANG))
to.write("""
</manifest>
<spine>""")
for i in range(ndicts):
to.write("""
<itemref idref="dictionary{ndict}"/>""".format(ndict=i))
to.write("""
</spine>
""")
######################################################
# main
######################################################
print("Reading keys…")
defns = readkeys()
name = os.path.splitext(os.path.basename(TAB_FILE))[0]
print("Writing keys…")
ndicts = writekeys(defns)
print("Writing opf…")
writeopf(ndicts, name)
print("Done.")