-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Doc implement reredirects #19456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+135
−0
Merged
Doc implement reredirects #19456
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| """ | ||
| Redirecting old docs to new location | ||
| ==================================== | ||
|
|
||
| If an rst file is moved or its content subsumed in a different file, it | ||
| is desireable to redirect the old file to the new or existing file. This | ||
| extension enables this with a simple html refresh. | ||
|
|
||
| For example suppose ``doc/topic/old-page.rst`` is removed and its content | ||
| included in ``doc/topic/new-page.rst``. We use the ``redirect_from`` | ||
| directive in ``doc/topic/new-page.rst``:: | ||
|
|
||
| .. redirect-from:: /topic/old-page | ||
|
|
||
| This creates in the build directory a file ``build/html/topic/old-page.html`` | ||
| that contains a relative refresh:: | ||
|
|
||
| <html> | ||
| <head> | ||
| <meta http-equiv="refresh" content="0; url=new-page.html"> | ||
| </head> | ||
| </html> | ||
|
|
||
| If you need to redirect across subdirectory trees, that works as well. For | ||
| instance if ``doc/topic/subdir1/old-page.rst`` is now found at | ||
| ``doc/topic/subdir2/new-page.rst`` then ``new-page.rst`` just lists the | ||
| full path:: | ||
|
|
||
| .. redirect-from:: /topic/subdir1/old-page.rst | ||
|
|
||
| """ | ||
|
|
||
| from pathlib import Path | ||
| from docutils.parsers.rst import Directive | ||
| from sphinx.util import logging | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| HTML_TEMPLATE = """<html> | ||
| <head> | ||
| <meta http-equiv="refresh" content="0; url={v}"> | ||
| </head> | ||
| </html> | ||
| """ | ||
|
|
||
|
|
||
| def setup(app): | ||
| RedirectFrom.app = app | ||
| app.add_directive("redirect-from", RedirectFrom) | ||
| app.connect("build-finished", _generate_redirects) | ||
|
|
||
|
|
||
| class RedirectFrom(Directive): | ||
| required_arguments = 1 | ||
| redirects = {} | ||
|
|
||
| def run(self): | ||
| redirected_doc, = self.arguments | ||
| env = self.app.env | ||
| builder = self.app.builder | ||
| current_doc = env.path2doc(self.state.document.current_source) | ||
| redirected_reldoc, _ = env.relfn2path(redirected_doc, current_doc) | ||
| if redirected_reldoc in self.redirects: | ||
| raise ValueError( | ||
| f"{redirected_reldoc} is already noted as redirecting to " | ||
| f"{self.redirects[redirected_reldoc]}") | ||
| self.redirects[redirected_reldoc] = builder.get_relative_uri( | ||
| redirected_reldoc, current_doc) | ||
| return [] | ||
|
|
||
|
|
||
| def _generate_redirects(app, exception): | ||
| builder = app.builder | ||
| if builder.name != "html" or exception: | ||
| return | ||
| for k, v in RedirectFrom.redirects.items(): | ||
| p = Path(app.outdir, k + builder.out_suffix) | ||
| if p.is_file(): | ||
| logger.warning(f'A redirect-from directive is trying to create ' | ||
| f'{p}, but that file already exists (perhaps ' | ||
| f'you need to run "make clean")') | ||
| else: | ||
| p.parent.mkdir(parents=True, exist_ok=True) | ||
| with p.open("x") as file: | ||
| logger.info('making refresh html file: ' + k + | ||
| ' redirect to ' + v) | ||
|
QuLogic marked this conversation as resolved.
Outdated
|
||
| file.write(HTML_TEMPLATE.format(v=v)) | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.