2
# vim:fileencoding=utf-8
3
from __future__ import (unicode_literals, division, absolute_import,
7
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
11
from calibre.ptempfile import TemporaryDirectory
12
from calibre.ebooks.conversion.plumber import Plumber
13
from calibre.ebooks.oeb.polish.container import Container, OEB_DOCS, OEB_STYLES
14
from calibre.ebooks.epub import initialize_container
16
from calibre.utils.logging import default_log
18
IMPORTABLE = {'htm', 'xhtml', 'html', 'xhtm', 'docx'}
20
def auto_fill_manifest(container):
21
manifest_id_map = container.manifest_id_map
22
manifest_name_map = {v:k for k, v in manifest_id_map.iteritems()}
24
for name, mt in container.mime_map.iteritems():
25
if name not in manifest_name_map and not container.ok_to_be_unmanifested(name):
26
mitem = container.generate_item(name)
27
gname = container.href_to_name(mitem.get('href'), container.opf_name)
29
raise ValueError('This should never happen (gname=%r, name=%r)' % (gname, name))
30
manifest_name_map[name] = mitem.get('id')
31
manifest_id_map[mitem.get('id')] = name
33
def import_book_as_epub(srcpath, destpath, log=default_log):
34
if not destpath.lower().endswith('.epub'):
35
raise ValueError('Can only import books into the EPUB format, not %s' % (os.path.basename(destpath)))
36
with TemporaryDirectory('eei') as tdir:
37
tdir = os.path.abspath(os.path.realpath(tdir)) # Needed to handle the multiple levels of symlinks for /tmp on OS X
38
plumber = Plumber(srcpath, tdir, log)
39
plumber.setup_options()
40
if srcpath.lower().endswith('.opf'):
41
plumber.opts.dont_package = True
42
if hasattr(plumber.opts, 'no_process'):
43
plumber.opts.no_process = True
44
plumber.input_plugin.for_viewer = True
45
with plumber.input_plugin, open(plumber.input, 'rb') as inf:
46
pathtoopf = plumber.input_plugin(inf, plumber.opts, plumber.input_fmt, log, {}, tdir)
47
if hasattr(pathtoopf, 'manifest'):
48
from calibre.ebooks.oeb.iterator.book import write_oebbook
49
pathtoopf = write_oebbook(pathtoopf, tdir)
51
c = Container(tdir, pathtoopf, log)
53
# Auto fix all HTML/CSS
54
for name, mt in c.mime_map.iteritems():
55
if mt in set(OEB_DOCS) | set(OEB_STYLES):
60
zf = initialize_container(destpath, opf_name=c.opf_name)
62
for name in c.name_path_map:
63
zf.writestr(name, c.raw_data(name, decode=False))
65
if __name__ == '__main__':
66
import_book_as_epub(sys.argv[-2], sys.argv[-1])