From 52616a4994aa480a9623a11b9f556bcd0ec73a3e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 5 May 2025 13:36:37 +0530 Subject: [PATCH] News download: Fix Next/Previous links not working when the pointed to article failed to download --- src/calibre/web/feeds/news.py | 29 ++++++++++++++++++++++------- src/calibre/web/feeds/templates.py | 8 ++++---- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/calibre/web/feeds/news.py b/src/calibre/web/feeds/news.py index 563a70743c..32b4a7fa87 100644 --- a/src/calibre/web/feeds/news.py +++ b/src/calibre/web/feeds/news.py @@ -1863,13 +1863,28 @@ class BasicNewsRecipe(Recipe): return soup def internal_postprocess_book(self, oeb, opts, log): - if self.resolve_internal_links and self.article_url_map: - seen = set() - for item in oeb.spine: - for a in item.data.xpath('//*[local-name()="a" and @href]'): - if a.get('rel') == 'calibre-downloaded-from': - continue - url = a.get('href') + seen = set() + for i, item in enumerate(oeb.spine): + for a in item.data.xpath('//*[local-name()="a" and @href]'): + if (rel := a.get('rel')) == 'calibre-downloaded-from': + continue + url = a.get('href') + if not url: + continue + if rel in ('articlenextlink', 'articleprevlink'): + abshref = item.abshref(url) + if abshref not in oeb.manifest.hrefs: + if rel == 'articlenextlink': + nextitem = oeb.spine[i + 1] if i + 1 < len(oeb.spine) else None + else: + nextitem = oeb.spine[i - 1] if i else None + if nextitem is None: + a.text = None + a.attrib.pop('href') + else: + a.set('href', item.relhref(nextitem.href)) + continue + if self.resolve_internal_links and self.article_url_map: for curl in self.canonicalize_internal_url(url): articles = self.article_url_map.get(curl) if articles: diff --git a/src/calibre/web/feeds/templates.py b/src/calibre/web/feeds/templates.py index cd354cc000..c75d93481c 100644 --- a/src/calibre/web/feeds/templates.py +++ b/src/calibre/web/feeds/templates.py @@ -225,7 +225,7 @@ class NavBarTemplate(Template): up = '../..' if art == number_of_articles_in_feed - 1 else '..' href = f'{prefix}{up}/{next_art}/index.html' navbar.text = '| ' - navbar.append(A(_('Next'), href=href)) + navbar.append(A(_('Next'), href=href, rel='articlenextlink')) href = f'{prefix}../index.html#article_{art}' next(navbar.iterchildren(reversed=True)).tail = ' | ' navbar.append(A(_('Section menu'), href=href)) @@ -235,7 +235,7 @@ class NavBarTemplate(Template): if art > 0 and not bottom: href = f'{prefix}../article_{art - 1}/index.html' next(navbar.iterchildren(reversed=True)).tail = ' | ' - navbar.append(A(_('Previous'), href=href)) + navbar.append(A(_('Previous'), href=href, rel='articleprevlink')) next(navbar.iterchildren(reversed=True)).tail = ' | ' if not bottom: navbar.append(HR()) @@ -402,7 +402,7 @@ class TouchscreenNavBarTemplate(Template): navbar.append(BR()) # | Previous if art > 0: - link = A(attrs('article_link'),_('Previous'),href=f'{prefix}../article_{art - 1}/index.html') + link = A(attrs('article_link'),_('Previous'), rel='articleprevlink', href=f'{prefix}../article_{art - 1}/index.html') navbar_tr.append(TD(attrs('article_prev'),link)) else: navbar_tr.append(TD(attrs('article_prev'),'')) @@ -419,7 +419,7 @@ class TouchscreenNavBarTemplate(Template): else f'article_{art + 1}' up = '../..' if art == number_of_articles_in_feed - 1 else '..' - link = A(attrs('article_link'), _('Next'), href=f'{prefix}{up}/{next_art}/index.html') + link = A(attrs('article_link'), _('Next'), rel='articlenextlink', href=f'{prefix}{up}/{next_art}/index.html') navbar_tr.append(TD(attrs('article_next'),link)) navbar_t.append(navbar_tr) navbar.append(navbar_t)