From 6fc3a4ddf3488f036a6fe51e6c48dc401b36fcb3 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sun, 22 Jan 2023 12:56:05 +0200 Subject: [PATCH] Infra: Use relative links to ease development (#2972) --- pep_sphinx_extensions/__init__.py | 5 +++-- pep_sphinx_extensions/pep_processor/parsing/pep_role.py | 4 ++++ .../pep_processor/transforms/pep_headers.py | 6 +++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pep_sphinx_extensions/__init__.py b/pep_sphinx_extensions/__init__.py index 83c1269ce..af53f240c 100644 --- a/pep_sphinx_extensions/__init__.py +++ b/pep_sphinx_extensions/__init__.py @@ -25,8 +25,9 @@ def _depart_maths(): def _update_config_for_builder(app: Sphinx) -> None: app.env.document_ids = {} # For PEPReferenceRoleTitleText + app.env.settings["builder"] = app.builder.name if app.builder.name == "dirhtml": - app.env.settings["pep_url"] = "/pep-{:0>4}" + app.env.settings["pep_url"] = "pep-{:0>4}" # internal_builder exists if Sphinx is run by build.py if "internal_builder" not in app.tags: @@ -46,7 +47,7 @@ def _post_build(app: Sphinx, exception: Exception | None) -> None: def setup(app: Sphinx) -> dict[str, bool]: """Initialize Sphinx extension.""" - environment.default_settings["pep_url"] = "/pep-{:0>4}.html" + environment.default_settings["pep_url"] = "pep-{:0>4}.html" environment.default_settings["halt_level"] = 2 # Fail on Docutils warning # Register plugin logic diff --git a/pep_sphinx_extensions/pep_processor/parsing/pep_role.py b/pep_sphinx_extensions/pep_processor/parsing/pep_role.py index 18ecdc982..be1683a33 100644 --- a/pep_sphinx_extensions/pep_processor/parsing/pep_role.py +++ b/pep_sphinx_extensions/pep_processor/parsing/pep_role.py @@ -15,6 +15,10 @@ class PEPRole(roles.ReferenceRole): prb = self.inliner.problematic(self.rawtext, self.rawtext, msg) return [prb], [msg] pep_base = self.inliner.document.settings.pep_url.format(pep_num) + if self.inliner.document.settings.builder == "dirhtml": + pep_base = "../" + pep_base + if "topic" in self.get_location(): + pep_base = "../" + pep_base if fragment: ref_uri = f"{pep_base}#{fragment}" else: diff --git a/pep_sphinx_extensions/pep_processor/transforms/pep_headers.py b/pep_sphinx_extensions/pep_processor/transforms/pep_headers.py index 487efee49..57997c6b0 100644 --- a/pep_sphinx_extensions/pep_processor/transforms/pep_headers.py +++ b/pep_sphinx_extensions/pep_processor/transforms/pep_headers.py @@ -137,7 +137,11 @@ class PEPHeaders(transforms.Transform): new_body = [] for topic_name in body.astext().split(","): if topic_name: - target = f"/topic/{topic_name.lower().strip()}/" + target = f"topic/{topic_name.lower().strip()}" + if self.document.settings.builder == "html": + target = f"{target}.html" + else: + target = f"../{target}/" new_body += [ nodes.reference("", topic_name, refuri=target), nodes.Text(", "),