From 594d89460bbef8e13a65949510a7d461874cc130 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 21 Aug 2018 13:39:17 +0530 Subject: [PATCH] Edit book: Fix some links not being adjusted when splitting HTML files. Fixes #1787892 [Footnote not updating when file split used in editor](https://bugs.launchpad.net/calibre/+bug/1787892) --- src/calibre/ebooks/oeb/polish/split.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/split.py b/src/calibre/ebooks/oeb/polish/split.py index 670fcc4703..f06e11f7b3 100644 --- a/src/calibre/ebooks/oeb/polish/split.py +++ b/src/calibre/ebooks/oeb/polish/split.py @@ -220,7 +220,7 @@ def split(container, name, loc_or_xpath, before=True, totals=None): bottom_name = container.href_to_name(manifest_item.get('href'), container.opf_name) # Fix links in the split trees - for r, rname, anchors in [(root1, bottom_name, anchors_in_bottom), (root2, name, anchors_in_top)]: + for r in (root1, root2): for a in r.xpath('//*[@href]'): url = a.get('href') if url.startswith('#'): @@ -229,8 +229,16 @@ def split(container, name, loc_or_xpath, before=True, totals=None): fname = container.href_to_name(url, name) if fname == name: purl = urlparse(url) - if purl.fragment in anchors: - a.set('href', '%s#%s' % (container.name_to_href(rname, name), purl.fragment)) + if purl.fragment in anchors_in_top: + if r is root2: + a.set('href', '%s#%s' % (container.name_to_href(name, bottom_name), purl.fragment)) + else: + a.set('href', '#' + purl.fragment) + elif purl.fragment in anchors_in_bottom: + if r is root1: + a.set('href', '%s#%s' % (container.name_to_href(bottom_name, name), purl.fragment)) + else: + a.set('href', '#' + purl.fragment) # Fix all links in the container that point to anchors in the bottom tree for fname, media_type in container.mime_map.iteritems():