MOBI Input:Fix passthrough of TOC to conversion pipeline

This commit is contained in:
Kovid Goyal 2009-04-01 20:42:02 -07:00
parent b2e8618354
commit d7e9ca4bee
3 changed files with 15 additions and 9 deletions

View File

@ -339,7 +339,7 @@ OptionRecommendation(name='language',
trimmer = ManifestTrimmer() trimmer = ManifestTrimmer()
trimmer(self.oeb, self.opts) trimmer(self.oeb, self.opts)
self.log.info('Creating %s output...'%self.output_plugin.name) self.log.info('Creating %s...'%self.output_plugin.name)
self.output_plugin.convert(self.oeb, self.output, self.input_plugin, self.opts, self.output_plugin.convert(self.oeb, self.output, self.input_plugin, self.opts,
self.log) self.log)

View File

@ -266,12 +266,14 @@ class MobiReader(object):
parse_cache[htmlfile] = root parse_cache[htmlfile] = root
self.htmlfile = htmlfile self.htmlfile = htmlfile
ncx = cStringIO.StringIO() ncx = cStringIO.StringIO()
opf = self.create_opf(htmlfile, guide, root) opf, ncx_manifest_entry = self.create_opf(htmlfile, guide, root)
self.created_opf_path = os.path.splitext(htmlfile)[0]+'.opf' self.created_opf_path = os.path.splitext(htmlfile)[0]+'.opf'
opf.render(open(self.created_opf_path, 'wb'), ncx) opf.render(open(self.created_opf_path, 'wb'), ncx,
ncx_manifest_entry=ncx_manifest_entry)
ncx = ncx.getvalue() ncx = ncx.getvalue()
if ncx: if ncx:
open(os.path.splitext(htmlfile)[0]+'.ncx', 'wb').write(ncx) ncx_path = os.path.join(os.path.dirname(htmlfile), 'toc.ncx')
open(ncx_path, 'wb').write(ncx)
with open('styles.css', 'wb') as s: with open('styles.css', 'wb') as s:
s.write(self.base_css_rules+'\n\n') s.write(self.base_css_rules+'\n\n')
@ -284,8 +286,9 @@ class MobiReader(object):
if self.book_header.exth is not None or self.embedded_mi is not None: if self.book_header.exth is not None or self.embedded_mi is not None:
self.log.debug('Creating OPF...') self.log.debug('Creating OPF...')
ncx = cStringIO.StringIO() ncx = cStringIO.StringIO()
opf = self.create_opf(htmlfile, guide, root) opf, ncx_manifest_entry = self.create_opf(htmlfile, guide, root)
opf.render(open(os.path.splitext(htmlfile)[0]+'.opf', 'wb'), ncx) opf.render(open(os.path.splitext(htmlfile)[0]+'.opf', 'wb'), ncx,
ncx_manifest_entry )
ncx = ncx.getvalue() ncx = ncx.getvalue()
if ncx: if ncx:
open(os.path.splitext(htmlfile)[0]+'.ncx', 'wb').write(ncx) open(os.path.splitext(htmlfile)[0]+'.ncx', 'wb').write(ncx)
@ -434,7 +437,10 @@ class MobiReader(object):
for ref in opf.guide: for ref in opf.guide:
if ref.type.lower() == 'toc': if ref.type.lower() == 'toc':
toc = ref.href() toc = ref.href()
ncx_manifest_entry = None
if toc: if toc:
ncx_manifest_entry = 'toc.ncx'
elems = root.xpath('//*[@id="%s"]'%toc.partition('#')[-1]) elems = root.xpath('//*[@id="%s"]'%toc.partition('#')[-1])
tocobj = None tocobj = None
ent_pat = re.compile(r'&(\S+?);') ent_pat = re.compile(r'&(\S+?);')
@ -461,7 +467,7 @@ class MobiReader(object):
if tocobj is not None: if tocobj is not None:
opf.set_toc(tocobj) opf.set_toc(tocobj)
return opf return opf, ncx_manifest_entry
def sizeof_trailing_entries(self, data): def sizeof_trailing_entries(self, data):
@ -589,7 +595,7 @@ def get_metadata(stream):
if mr.book_header.exth is None: if mr.book_header.exth is None:
mi = MetaInformation(mr.name, [_('Unknown')]) mi = MetaInformation(mr.name, [_('Unknown')])
else: else:
mi = mr.create_opf('dummy.html') mi = mr.create_opf('dummy.html')[0]
try: try:
if hasattr(mr.book_header.exth, 'cover_offset'): if hasattr(mr.book_header.exth, 'cover_offset'):
cover_index = mr.book_header.first_image_index + \ cover_index = mr.book_header.first_image_index + \

View File

@ -44,7 +44,7 @@ class OEBOutput(OutputFormatPlugin):
else: else:
raw = etree.tostring(raw, encoding='utf-8', raw = etree.tostring(raw, encoding='utf-8',
pretty_print=opts.pretty_print) pretty_print=opts.pretty_print)
raw = raw + '<?xml version="1.0" encoding="utf-8" ?>\n' raw = '<?xml version="1.0" encoding="utf-8" ?>\n'+raw
if isinstance(raw, unicode): if isinstance(raw, unicode):
raw = raw.encode('utf-8') raw = raw.encode('utf-8')
with open(path, 'wb') as f: with open(path, 'wb') as f: