From d428b95fc12dbfed2308563f88045619ddc322aa Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 20 Jul 2025 19:57:50 +0530 Subject: [PATCH] Fix #2117336 [Another page-list in EPUB 3 nav can be broken by conversion bug](https://bugs.launchpad.net/calibre/+bug/2117336) --- src/calibre/ebooks/oeb/polish/toc.py | 3 ++- src/calibre/ebooks/oeb/transforms/split.py | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/toc.py b/src/calibre/ebooks/oeb/polish/toc.py index 0a519a2283..37fcbea54e 100644 --- a/src/calibre/ebooks/oeb/polish/toc.py +++ b/src/calibre/ebooks/oeb/polish/toc.py @@ -703,7 +703,8 @@ def ensure_container_has_nav(container, lang=None, previous_nav=None): tocname = nav_name container.apply_unique_properties(tocname, 'nav') if tocname is None: - item = container.generate_item('nav.xhtml', id_prefix='nav') + name = previous_nav[0] if previous_nav is not None else 'nav.xhtml' + item = container.generate_item(name, id_prefix='nav') item.set('properties', 'nav') tocname = container.href_to_name(item.get('href'), base=container.opf_name) if previous_nav is not None: diff --git a/src/calibre/ebooks/oeb/transforms/split.py b/src/calibre/ebooks/oeb/transforms/split.py index 1a37e2ae23..a01f03af34 100644 --- a/src/calibre/ebooks/oeb/transforms/split.py +++ b/src/calibre/ebooks/oeb/transforms/split.py @@ -65,14 +65,15 @@ class Split: self.log('Splitting markup on page breaks and flow limits, if any...') self.opts = opts self.map = {} - nav_href = getattr(opts, 'epub3_nav_href', '') + self.nav_href = getattr(opts, 'epub3_nav_href', '') + self.existing_nav = getattr(opts, 'epub3_nav_parsed', None) output_supports_nav = False with suppress(Exception): output_supports_nav = int(opts.epub_version) >= 3 def is_nav(item): - ans = item.href == nav_href and output_supports_nav + ans = item.href == self.nav_href and output_supports_nav if ans: - self.log(f'Not splitting {nav_href} as it is the EPUB3 nav document') + self.log(f'Not splitting {self.nav_href} as it is the EPUB3 nav document') return ans for item in list(self.oeb.manifest.items): if item.spine_position is not None and etree.iselement(item.data) and not is_nav(item): @@ -171,10 +172,20 @@ class Split: ''' Fix references to the split files in other content files. ''' + seen = set() for item in self.oeb.manifest: if etree.iselement(item.data): self.current_item = item rewrite_links(item.data, self.rewrite_links) + seen.add(item.data) + if self.existing_nav is not None and self.existing_nav not in seen: + seen.add(self.existing_nav) + from calibre.ebooks.oeb.base import rel_href + class FakeManifestItem: + href = self.nav_href + def abshref(self): return self.href + def relhref(self, href): return rel_href(self.href, href) + rewrite_links(self.existing_nav, self.rewrite_links) def rewrite_links(self, url): href, frag = urldefrag(url)