HTMLZ output: Make renaming of image files happen in filename order. Fixes #2072405 [EPUB to HTMLZ Converion Image Order Issue](https://bugs.launchpad.net/calibre/+bug/2072405)

This commit is contained in:
Kovid Goyal 2024-07-07 12:53:09 +05:30
parent eebbbf4782
commit 4175ef18ad
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -79,31 +79,32 @@ class OEB2HTML:
return self.links[href] return self.links[href]
def map_resources(self, oeb_book): def map_resources(self, oeb_book):
for item in oeb_book.manifest: from operator import attrgetter
if item.media_type in OEB_IMAGES: images = sorted((item for item in oeb_book.manifest if item.media_type in OEB_IMAGES), key=attrgetter('href'))
if item.href not in self.images: for item in images:
ext = os.path.splitext(item.href)[1] if item.href not in self.images:
fname = f'{len(self.images)}{ext}' ext = os.path.splitext(item.href)[1]
fname = fname.zfill(10) fname = f'{len(self.images):06d}{ext}'
self.images[item.href] = fname self.images[item.href] = fname
if item in oeb_book.spine:
self.get_link_id(item.href) for item in oeb_book.spine:
root = item.data.find(XHTML('body')) self.get_link_id(item.href)
link_attrs = set(html.defs.link_attrs) root = item.data.find(XHTML('body'))
link_attrs.add(XLINK('href')) link_attrs = set(html.defs.link_attrs)
for el in root.iter(): link_attrs.add(XLINK('href'))
attribs = el.attrib for el in root.iter():
try: attribs = el.attrib
if not isinstance(el.tag, string_or_bytes): try:
continue if not isinstance(el.tag, string_or_bytes):
except:
continue continue
for attr in attribs: except Exception:
if attr in link_attrs: continue
href = item.abshref(attribs[attr]) for attr in attribs:
href, id = urldefrag(href) if attr in link_attrs:
if href in self.base_hrefs: href = item.abshref(attribs[attr])
self.get_link_id(href, id) href, id = urldefrag(href)
if href in self.base_hrefs:
self.get_link_id(href, id)
def rewrite_link(self, url, page=None): def rewrite_link(self, url, page=None):
if not page: if not page: