Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb3f0dc

Browse files
tranansoQuLogic
andauthored
[BUG] Fix redirect-from Sphinx extension (#27915)
* [BUG] Fix clear_doc for redirect-from domain * [BUG] Implement merge_domaindata Update merge_domaindata to match Sphinx example implementation https://github.com/sphinx-doc/sphinx/blob/5523c9bbe4d1415777669330411d4f00ad802f18/sphinx/ext/todo.py#L90-L92 * [BUG] Remove check for redirects that already exist Check causes build to fail when Sphinx uses a cached build environment. We can just rewrite the cached redirect. * [LINT] Fix linting issue * [ENH] Switch Directive to SphinxDirective, which has self.env * [BUG] Re-introduce sanity check * Added domain.redirects sanity check back in * Clear cached redirects from the domain * [BUG] Reintroduce merge_domaindata sanity check * Refactor * Revert changes to merge_domaindata * Cleanup code Implement suggested changes Co-authored-by: Elliott Sales de Andrade <[email protected]> --------- Co-authored-by: Elliott Sales de Andrade <[email protected]>
1 parent 9dd9cb4 commit bb3f0dc

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

doc/sphinxext/redirect_from.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"""
3434

3535
from pathlib import Path
36-
from docutils.parsers.rst import Directive
36+
from sphinx.util.docutils import SphinxDirective
3737
from sphinx.domains import Domain
3838
from sphinx.util import logging
3939

@@ -51,9 +51,9 @@
5151

5252

5353
def setup(app):
54-
RedirectFrom.app = app
5554
app.add_directive("redirect-from", RedirectFrom)
5655
app.add_domain(RedirectFromDomain)
56+
app.connect("builder-inited", _clear_redirects)
5757
app.connect("build-finished", _generate_redirects)
5858

5959
metadata = {'parallel_read_safe': True}
@@ -73,8 +73,8 @@ def redirects(self):
7373
"""The mapping of the redirects."""
7474
return self.data.setdefault('redirects', {})
7575

76-
def clear_doc(self, docnames):
77-
self.redirects.clear()
76+
def clear_doc(self, docname):
77+
self.redirects.pop(docname, None)
7878

7979
def merge_domaindata(self, docnames, otherdata):
8080
for src, dst in otherdata['redirects'].items():
@@ -86,15 +86,14 @@ def merge_domaindata(self, docnames, otherdata):
8686
f"{self.redirects[src]} and {otherdata['redirects'][src]}")
8787

8888

89-
class RedirectFrom(Directive):
89+
class RedirectFrom(SphinxDirective):
9090
required_arguments = 1
9191

9292
def run(self):
9393
redirected_doc, = self.arguments
94-
env = self.app.env
95-
domain = env.get_domain('redirect_from')
96-
current_doc = env.path2doc(self.state.document.current_source)
97-
redirected_reldoc, _ = env.relfn2path(redirected_doc, current_doc)
94+
domain = self.env.get_domain('redirect_from')
95+
current_doc = self.env.path2doc(self.state.document.current_source)
96+
redirected_reldoc, _ = self.env.relfn2path(redirected_doc, current_doc)
9897
if redirected_reldoc in domain.redirects:
9998
raise ValueError(
10099
f"{redirected_reldoc} is already noted as redirecting to "
@@ -119,3 +118,10 @@ def _generate_redirects(app, exception):
119118
logger.info('making refresh html file: %s redirect to %s', k, v)
120119
p.parent.mkdir(parents=True, exist_ok=True)
121120
p.write_text(html, encoding='utf-8')
121+
122+
123+
def _clear_redirects(app):
124+
domain = app.env.get_domain('redirect_from')
125+
if domain.redirects:
126+
logger.info('clearing cached redirects')
127+
domain.redirects.clear()

0 commit comments

Comments
 (0)