Edit book: When autofixing file extensions <-> mimetype mismatch, if the file in question is int he spine, chage the file extension rather than the mimetype, as that is more likely to be the correct fix.

This commit is contained in:
Kovid Goyal 2014-02-16 12:46:33 +05:30
parent 0e3e6aaa36
commit 634003eea4

View File

@ -125,18 +125,33 @@ class MimetypeMismatch(BaseError):
' The recommended mimetype for files with the extension "{2}" is {3}.' ' The recommended mimetype for files with the extension "{2}" is {3}.'
' You should change either the file extension or the mimetype in the OPF.').format( ' You should change either the file extension or the mimetype in the OPF.').format(
name, opf_mt, ext, ext_mt) name, opf_mt, ext, ext_mt)
self.INDIVIDUAL_FIX = _('Change the mimetype for this file in the OPF to %s') % ext_mt if opf_mt in OEB_DOCS and name in {n for n, l in container.spine_names}:
self.INDIVIDUAL_FIX = _('Change the file extension to .xhtml')
self.change_ext_to = 'xhtml'
else:
self.INDIVIDUAL_FIX = _('Change the mimetype for this file in the OPF to %s') % ext_mt
self.change_ext_to = None
def __call__(self, container): def __call__(self, container):
changed = False changed = False
for item in container.opf_xpath('//opf:manifest/opf:item[@href and @media-type="%s"]' % self.opf_mt): if self.change_ext_to is not None:
name = container.href_to_name(item.get('href'), container.opf_name) from calibre.ebooks.oeb.polish.replace import rename_files
if name == self.file_name: new_name = self.file_name.rpartition('.')[0] + '.' + self.change_ext_to
changed = True c = 0
item.set('media-type', self.ext_mt) while container.has_name(new_name):
container.mime_map[name] = self.ext_mt c += 1
if changed: new_name = self.file_name.rpartition('.')[0] + ('%d.' % c) + self.change_ext_to
container.dirty(container.opf_name) rename_files(container, {self.file_name:new_name})
changed = True
else:
for item in container.opf_xpath('//opf:manifest/opf:item[@href and @media-type="%s"]' % self.opf_mt):
name = container.href_to_name(item.get('href'), container.opf_name)
if name == self.file_name:
changed = True
item.set('media-type', self.ext_mt)
container.mime_map[name] = self.ext_mt
if changed:
container.dirty(container.opf_name)
return changed return changed
def check_mimetypes(container): def check_mimetypes(container):