1
from __future__ import with_statement
3
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
4
__docformat__ = 'restructuredtext en'
10
from calibre.customize.conversion import OutputFormatPlugin
11
from calibre import CurrentDir
12
from calibre.customize.conversion import OptionRecommendation
14
from urllib import unquote
16
class OEBOutput(OutputFormatPlugin):
19
author = 'Kovid Goyal'
22
recommendations = set([('pretty_print', True, OptionRecommendation.HIGH)])
25
def convert(self, oeb_book, output_path, input_plugin, opts, log):
26
self.log, self.opts = log, opts
27
if not os.path.exists(output_path):
28
os.makedirs(output_path)
29
from calibre.ebooks.oeb.base import OPF_MIME, NCX_MIME, PAGE_MAP_MIME
30
with CurrentDir(output_path):
31
results = oeb_book.to_opf2(page_map=True)
32
for key in (OPF_MIME, NCX_MIME, PAGE_MAP_MIME):
33
href, root = results.pop(key, [None, None])
35
raw = etree.tostring(root, pretty_print=True,
36
encoding='utf-8', xml_declaration=True)
38
# Needed as I can't get lxml to output opf:role and
39
# not output <opf:metadata> as well
40
raw = re.sub(r'(<[/]{0,1})opf:', r'\1', raw)
41
with open(href, 'wb') as f:
44
for item in oeb_book.manifest:
45
path = os.path.abspath(unquote(item.href))
46
dir = os.path.dirname(path)
47
if not os.path.exists(dir):
49
with open(path, 'wb') as f: