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