Skip to content

Commit

Permalink
Convert PR numbers in docs/change_log to clickable links (#4346)
Browse files Browse the repository at this point in the history
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)`.
  • Loading branch information
sumezulike committed May 4, 2024
1 parent 75eb557 commit f22b243
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
#

import os
import re
import string
from importlib.metadata import version
from pathlib import Path

from sphinx.application import Sphinx

CURRENT_DIR = Path(__file__).parent


Expand All @@ -29,6 +32,36 @@ def make_pypi_svg(version: str) -> None:
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
# sphinxcontrib-programoutput on Windows.
os.putenv("pythonioencoding", "utf-8")
Expand Down

0 comments on commit f22b243

Please sign in to comment.