forked from jgoguen/calibre-kobo-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
199 lines (169 loc) · 7.74 KB
/
common.py
File metadata and controls
199 lines (169 loc) · 7.74 KB
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
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
"""Common functions and variables needed by more than one plugin."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
__license__ = "GPL v3"
__copyright__ = "2013, Joel Goguen <jgoguen@jgoguen.ca>"
__docformat__ = "markdown en"
# Be careful editing this! This file has to work in multiple packages at once,
# so don't import anything from calibre_plugins
import os
import re
from calibre.constants import config_dir
from calibre.ebooks.metadata.book.base import Metadata
from calibre.ebooks.metadata.book.base import NULL_VALUES
from calibre.ebooks.oeb.polish.container import EpubContainer
from calibre.ebooks.oeb.polish.container import OPF_NAMESPACES
from calibre.ptempfile import PersistentTemporaryFile
from calibre.utils.logging import default_log
from lxml.etree import _Element
kobo_js_re = re.compile(r".*/?kobo.*\.js$", re.IGNORECASE)
XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace"
configdir = os.path.join(config_dir, "plugins") # type: str
reference_kepub = os.path.join(configdir, "reference.kepub.epub") # type: str
plugin_version = (3, 1, 5)
plugin_minimum_calibre_version = (2, 60, 0)
# The logic here to detect a cover image is mostly duplicated from
# metadata/writer.py. Updates to the logic here probably need an accompanying
# update over there.
def modify_epub(
container, # type: EpubContainer
filename, # type: str
metadata=None, # type: Optional[Metadata]
opts={}, # type: Dict[str, Union[str, bool]]
): # type: (...) -> None
"""Modify the ePub file to make it KePub-compliant."""
# Search for the ePub cover
# TODO: Refactor out cover detection logic so it can be directly used in
# metadata/writer.py
found_cover = False # type: bool
opf = container.opf # type: _Element
cover_meta_node_list = opf.xpath(
'./opf:metadata/opf:meta[@name="cover"]', namespaces=OPF_NAMESPACES
) # List[_Element]
if len(cover_meta_node_list) > 0:
default_log("Found meta node with name=cover")
cover_meta_node = cover_meta_node_list[0] # type: _Element
cover_id = cover_meta_node.attrib.get("content", None)
if cover_id:
default_log("Found cover image ID '{0}'".format(cover_id))
cover_node_list = opf.xpath(
'./opf:manifest/opf:item[@id="{0}"]'.format(cover_id),
namespaces=OPF_NAMESPACES,
) # type: List[_Element]
if len(cover_node_list) > 0:
default_log("Found an item node with cover ID")
cover_node = cover_node_list[0] # type: _Element
if cover_node.attrib.get("properties", "") != "cover-image":
default_log("Setting cover-image property")
cover_node.set("properties", "cover-image")
container.dirty(container.opf_name)
else:
default_log("Item node is already set as cover-image")
found_cover = True
# It's possible that the cover image can't be detected this way. Try
# looking for the cover image ID in the OPF manifest.
if not found_cover:
default_log("Looking for cover image in OPF manifest")
node_list = opf.xpath(
"./opf:manifest/opf:item[(translate(@id, "
+ "'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"
+ '="cover" or starts-with(translate(@id, '
+ "'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"
+ ', "cover")) and starts-with(@media-type, "image")]',
namespaces=OPF_NAMESPACES,
) # type: List[_Element]
if len(node_list) > 0:
default_log(
"Found {0:d} nodes, assuming the first is the "
"right node".format(len(node_list))
)
node = node_list[0] # type: _Element
if node.attrib.get("properties", "") != "cover-image":
default_log("Setting cover-image property")
node.set("properties", "cover-image")
container.dirty(container.opf_name)
else:
default_log("Item node is already set as cover-image")
found_cover = True
# Because of the changes made to the markup here, cleanup needs to be done
# before any other content file processing
container.forced_cleanup()
if opts.get("clean_markup", False):
container.clean_markup()
# Hyphenate files?
if opts.get("no-hyphens", False):
nohyphen_css = PersistentTemporaryFile(
suffix="_nohyphen", prefix="kepub_"
) # type: PersistentTemporaryFile
nohyphen_css.write(get_resources("css/no-hyphens.css")) # noqa: F821
nohyphen_css.close()
css_path = os.path.basename(
container.copy_file_to_container(
nohyphen_css.name, name="kte-css/no-hyphens.css"
)
) # type: str
container.add_content_file_reference("kte-css/{0}".format(css_path))
os.unlink(nohyphen_css.name)
elif opts.get("hyphenate", False):
if metadata and metadata.language == NULL_VALUES["language"]:
default_log.warning(
"Hyphenation is enabled but not overriding content file "
"language. Hyphenation may use the wrong dictionary."
)
hyphen_css = PersistentTemporaryFile(
suffix="_hyphenate", prefix="kepub_"
) # type: PersistentTemporaryFile
hyphen_css.write(get_resources("css/hyphenation.css")) # noqa: F821
hyphen_css.close()
css_path = os.path.basename(
container.copy_file_to_container(
hyphen_css.name, name="kte-css/hyphenation.css"
)
) # type: str
container.add_content_file_reference("kte-css/{0}".format(css_path))
os.unlink(hyphen_css.name)
# Now smarten punctuation
if opts.get("smarten_punctuation", False):
container.smarten_punctuation()
if opts.get("extended_kepub_features", True):
if metadata is not None:
default_log(
"Adding extended Kobo features to {0} by {1}".format(
metadata.title, " and ".join(metadata.authors)
)
)
# Add the Kobo span tags
container.add_kobo_spans()
# Add the Kobo style hacks div tags
container.add_kobo_divs()
# Check to see if there's already a kobo*.js in the ePub
skip_js = False # type: str
for name in container.name_path_map:
if kobo_js_re.match(name):
skip_js = True
break
if not skip_js:
if os.path.isfile(reference_kepub):
reference_container = EpubContainer(reference_kepub, default_log)
for name in reference_container.name_path_map:
if kobo_js_re.match(name):
jsname = container.copy_file_to_container(
os.path.join(reference_container.root, name), name="kobo.js"
)
container.add_content_file_reference(jsname)
break
# Add the Kobo style hacks
stylehacks_css = PersistentTemporaryFile(suffix="_stylehacks", prefix="kepub_")
stylehacks_css.write(get_resources("css/style-hacks.css")) # noqa: F821
stylehacks_css.close()
css_path = os.path.basename(
container.copy_file_to_container(
stylehacks_css.name, name="kte-css/stylehacks.css"
)
)
container.add_content_file_reference("kte-css/{0}".format(css_path))
os.unlink(filename)
container.commit(filename)