Edit Book: Fix import of DOCX files that contain no Table of Contents not working. Fixes #1327522 [error editor while importing docx](https://bugs.launchpad.net/calibre/+bug/1327522)

This commit is contained in:
Kovid Goyal 2014-06-07 16:47:48 +05:30
parent 7f97e4213a
commit 0b0f6abe42
3 changed files with 12 additions and 8 deletions

View File

@ -343,8 +343,11 @@ class Convert(object):
opf.create_spine(['index.html'])
if self.cover_image is not None:
opf.guide.set_cover(self.cover_image)
with open(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(os.path.join(self.dest_dir, 'toc.ncx'), 'wb') as ncx:
toc_file = os.path.join(self.dest_dir, 'toc.ncx')
with open(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(toc_file, 'wb') as ncx:
opf.render(of, ncx, 'toc.ncx')
if os.path.getsize(toc_file) == 0:
os.remove(toc_file)
return os.path.join(self.dest_dir, 'metadata.opf')
def read_block_anchors(self, doc):

View File

@ -661,7 +661,7 @@ class Container(object): # {{{
package.append(item)
return item
def generate_item(self, name, id_prefix=None, media_type=None):
def generate_item(self, name, id_prefix=None, media_type=None, unique_href=True):
'''Add an item to the manifest with href derived from the given
name. Ensures uniqueness of href and id automatically. Returns
generated item.'''
@ -681,10 +681,11 @@ class Container(object): # {{{
def exists(h):
return self.exists(self.href_to_name(h, self.opf_name))
c = 0
while href in all_names or exists(href):
c += 1
href = '%s_%d.%s'%(base, c, ext)
if unique_href:
c = 0
while href in all_names or exists(href):
c += 1
href = '%s_%d.%s'%(base, c, ext)
manifest = self.opf_xpath('//opf:manifest')[0]
item = manifest.makeelement(OPF('item'),
id=item_id, href=href)

View File

@ -23,10 +23,10 @@ def auto_fill_manifest(container):
for name, mt in container.mime_map.iteritems():
if name not in manifest_name_map and not container.ok_to_be_unmanifested(name):
mitem = container.generate_item(name)
mitem = container.generate_item(name, unique_href=False)
gname = container.href_to_name(mitem.get('href'), container.opf_name)
if gname != name:
raise ValueError('This should never happen (gname=%r, name=%r)' % (gname, name))
raise ValueError('This should never happen (gname=%r, name=%r, href=%r)' % (gname, name, mitem.get('href')))
manifest_name_map[name] = mitem.get('id')
manifest_id_map[mitem.get('id')] = name