-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Restore linkcheck for specs #1950
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
Open
FFY00
wants to merge
3
commits into
pypa:main
Choose a base branch
from
FFY00:linkcheck-local-remote
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+92
−4
Open
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import os | ||
| import pathlib | ||
| import urllib | ||
|
|
||
| import sphinx.application | ||
| import sphinx.util.logging | ||
|
|
||
|
|
||
| DOMAIN = "packaging.python.org" | ||
|
|
||
|
|
||
| logger = sphinx.util.logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def resolve_local_html_link(app: sphinx.application.Sphinx, url_path: str) -> str: | ||
| """Takes path of a link pointing an HTML render of the current project, | ||
| and returns local path of the referenced document. | ||
|
|
||
| Support links to renders from both the `html` and `dirhtml` builders. | ||
|
|
||
| Example: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| >>> resolve_local_html_link('https://packaging.python.org/en/latest/flow/') | ||
| '{srcdir}/flow.rst' | ||
| >>> resolve_local_html_link('https://packaging.python.org/en/latest/flow.html') | ||
| '{srcdir}/flow.rst' | ||
| >>> resolve_local_html_link('https://packaging.python.org/en/latest/specifications/schemas/') | ||
| '{srcdir}/specifications/schemas/index.rst' | ||
| >>> resolve_local_html_link('https://packaging.python.org/en/latest/specifications/schemas/build-details-v1.0.schema.json') | ||
| '{html_extra_path0}/specifications/schemas/build-details-v1.0.schema.json' | ||
|
|
||
| """ | ||
| # Search for document in html_extra_path | ||
| for entry in app.config.html_extra_path: | ||
| candidate = (app.confdir / entry / url_path).resolve() | ||
| if candidate.is_dir(): | ||
| candidate = candidate / "index.html" | ||
| if candidate.exists(): | ||
| return os.fspath(candidate) | ||
| # Convert html path to source path | ||
| url_path = url_path.removesuffix("/") # Normalize | ||
| if url_path.endswith(".html"): | ||
| document = url_path.removesuffix(".html") | ||
| elif (candidate := f"{url_path}/index") in app.project.docnames: | ||
| document = candidate | ||
| else: | ||
| document = url_path | ||
| return app.env.doc2path(document) | ||
|
|
||
|
|
||
| def rewrite_local_uri(app: sphinx.application.Sphinx, uri: str) -> str: | ||
| """Replace remote URIs targeting https://packaging.python.org/en/latest/... | ||
| with local ones, so that local changes are taken into account by linkcheck. | ||
|
|
||
| Additionally, resolve local relative links to html_extra_path. | ||
| """ | ||
| local_uri = uri | ||
| parsed = urllib.parse.urlparse(uri) | ||
| # Links to https://packaging.python.org/en/latest/... | ||
| if parsed.hostname == DOMAIN and parsed.path.startswith("/en/latest/"): | ||
| document = parsed.path.removeprefix("/en/latest/") | ||
| local_uri = resolve_local_html_link(app, document) | ||
| logger.verbose( | ||
| f"{uri!s} is a remote URL that points to local sources, " | ||
| "replacing it with a local URL in linkcheck to take new changes " | ||
| "into account (pass -vv for more info)" | ||
| ) | ||
| logger.debug(f"Replacing linkcheck URL {uri!r} with {local_uri!r}") | ||
| # Local relative links | ||
| if not parsed.scheme and not parsed.netloc and parsed.path: | ||
| full_path = pathlib.Path(app.env.docname).parent / parsed.path | ||
| local_uri = resolve_local_html_link(app, os.fspath(full_path)) | ||
| if local_uri != uri: | ||
| logger.verbose(f"Local linkcheck URL {uri!r} resolved as {local_uri!r}") | ||
| return local_uri | ||
|
|
||
|
|
||
| def setup(app: sphinx.application.Sphinx) -> dict[str, bool]: | ||
| app.connect("linkcheck-process-uri", rewrite_local_uri) | ||
|
|
||
| return { | ||
| "parallel_read_safe": True, | ||
| "parallel_write_safe": True, | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo?