|
| 1 | +import os |
| 2 | +import pathlib |
| 3 | +import urllib |
| 4 | + |
| 5 | +import sphinx.application |
| 6 | +import sphinx.util.logging |
| 7 | + |
| 8 | + |
| 9 | +DOMAIN = "packaging.python.org" |
| 10 | + |
| 11 | + |
| 12 | +logger = sphinx.util.logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +def resolve_local_html_link(app: sphinx.application.Sphinx, url_path: str) -> str: |
| 16 | + """Takes path of a link pointing an HTML render of the current project, |
| 17 | + and returns local path of the referenced document. |
| 18 | +
|
| 19 | + Support links to renders from both the `html` and `dirhtml` builders. |
| 20 | +
|
| 21 | + Example: |
| 22 | +
|
| 23 | + .. code-block:: python |
| 24 | +
|
| 25 | + >>> resolve_local_html_link('https://packaging.python.org/en/latest/flow/') |
| 26 | + '{srcdir}/flow.rst' |
| 27 | + >>> resolve_local_html_link('https://packaging.python.org/en/latest/flow.html') |
| 28 | + '{srcdir}/flow.rst' |
| 29 | + >>> resolve_local_html_link('https://packaging.python.org/en/latest/specifications/schemas/') |
| 30 | + '{srcdir}/specifications/schemas/index.rst' |
| 31 | + >>> resolve_local_html_link('https://packaging.python.org/en/latest/specifications/schemas/build-details-v1.0.schema.json') |
| 32 | + '{html_extra_path0}/specifications/schemas/build-details-v1.0.schema.json' |
| 33 | +
|
| 34 | + """ |
| 35 | + # Search for document in html_extra_path |
| 36 | + for entry in app.config.html_extra_path: |
| 37 | + candidate = (app.confdir / entry / url_path).resolve() |
| 38 | + if candidate.is_dir(): |
| 39 | + candidate = candidate / "index.html" |
| 40 | + if candidate.exists(): |
| 41 | + return os.fspath(candidate) |
| 42 | + # Convert html path to source path |
| 43 | + url_path = url_path.removesuffix("/") # Normalize |
| 44 | + if url_path.endswith(".html"): |
| 45 | + document = url_path.removesuffix(".html") |
| 46 | + elif (candidate := f"{url_path}/index") in app.project.docnames: |
| 47 | + document = candidate |
| 48 | + else: |
| 49 | + document = url_path |
| 50 | + return app.env.doc2path(document) |
| 51 | + |
| 52 | + |
| 53 | +def rewrite_local_uri(app: sphinx.application.Sphinx, uri: str) -> str: |
| 54 | + """Replace remote URIs targeting https://packaging.python.org/en/latest/... |
| 55 | + with local ones, so that local changes are taken into account by linkcheck. |
| 56 | +
|
| 57 | + Additionally, resolve local relative links to html_extra_path. |
| 58 | + """ |
| 59 | + local_uri = uri |
| 60 | + parsed = urllib.parse.urlparse(uri) |
| 61 | + # Links to https://packaging.python.org/en/latest/... |
| 62 | + if parsed.hostname == DOMAIN and parsed.path.startswith("/en/latest/"): |
| 63 | + document = parsed.path.removeprefix("/en/latest/") |
| 64 | + local_uri = resolve_local_html_link(app, document) |
| 65 | + logger.verbose( |
| 66 | + f"{uri!s} is a remote URL that points to local sources, " |
| 67 | + "replacing it with a local URL in linkcheck to take new changes " |
| 68 | + "into account (pass -vv for more info)" |
| 69 | + ) |
| 70 | + logger.debug(f"Replacing linkcheck URL {uri!r} with {local_uri!r}") |
| 71 | + # Local relative links |
| 72 | + if not parsed.scheme and not parsed.netloc and parsed.path: |
| 73 | + full_path = pathlib.Path(app.env.docname).parent / parsed.path |
| 74 | + local_uri = resolve_local_html_link(app, os.fspath(full_path)) |
| 75 | + if local_uri != uri: |
| 76 | + logger.verbose(f"Local linkcheck URL {uri!r} resolved as {local_uri!r}") |
| 77 | + return local_uri |
| 78 | + |
| 79 | + |
| 80 | +def setup(app: sphinx.application.Sphinx) -> dict[str, bool]: |
| 81 | + app.connect("linkcheck-process-uri", rewrite_local_uri) |
| 82 | + |
| 83 | + return { |
| 84 | + "parallel_read_safe": True, |
| 85 | + "parallel_write_safe": True, |
| 86 | + } |
0 commit comments