Convert PR numbers in docs/change_log to clickable links (#4346)

Uses the sphinx include-read event to regex replace all occurrences of a PR number `(#X)` with a link `[(#X)](https://github.com/psf/black/pull/X)`.
This commit is contained in:
Samson Umezulike 2024-05-04 17:03:50 +02:00 committed by GitHub
parent 75eb55764e
commit f22b2437d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,10 +13,13 @@
# #
import os import os
import re
import string import string
from importlib.metadata import version from importlib.metadata import version
from pathlib import Path from pathlib import Path
from sphinx.application import Sphinx
CURRENT_DIR = Path(__file__).parent CURRENT_DIR = Path(__file__).parent
@ -29,6 +32,36 @@ def make_pypi_svg(version: str) -> None:
f.write(svg) f.write(svg)
def replace_pr_numbers_with_links(content: str) -> str:
"""Replaces all PR numbers with the corresponding GitHub link."""
base_url = "https://github.com/psf/black/pull/"
pr_num_regex = re.compile(r"\(#(\d+)\)")
def num_to_link(match: re.Match[str]) -> str:
number = match.group(1)
url = f"{base_url}{number}"
return f"([#{number}]({url}))"
return pr_num_regex.sub(num_to_link, content)
def handle_include_read(
app: Sphinx,
relative_path: Path,
parent_docname: str,
content: list[str],
) -> None:
"""Handler for the include-read sphinx event."""
if parent_docname == "change_log":
content[0] = replace_pr_numbers_with_links(content[0])
def setup(app: Sphinx) -> None:
"""Sets up a minimal sphinx extension."""
app.connect("include-read", handle_include_read)
# Necessary so Click doesn't hit an encode error when called by # Necessary so Click doesn't hit an encode error when called by
# sphinxcontrib-programoutput on Windows. # sphinxcontrib-programoutput on Windows.
os.putenv("pythonioencoding", "utf-8") os.putenv("pythonioencoding", "utf-8")