Repository: psf/black
File: docs/conf.py (Python)

Code before:
from pathlib import Path

from sphinx.application import Sphinx

CURRENT_DIR = Path(__file__).parent


def make_pypi_svg(version: str) -> None:
    template: Path = CURRENT_DIR / "_static" / "pypi_template.svg"
    target: Path = CURRENT_DIR / "_static" / "pypi.svg"
    with open(str(template), encoding="utf8") as f:
        svg: str = string.Template(f.read()).substitute(version=version)
    with open(str(target), "w", encoding="utf8") as f:
        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:
        full_match, number = match.group(0, 1)
        url = f"{base_url}{number}"
        return f"[{full_match}]({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")

# -- Project information -----------------------------------------------------

Review comment (suggestion):
```suggestion
        return f"([#{number}]({url}))"
```
Let's not link the parentheses.

Code after:
from pathlib import Path

from sphinx.application import Sphinx

CURRENT_DIR = Path(__file__).parent


def make_pypi_svg(version: str) -> None:
    template: Path = CURRENT_DIR / "_static" / "pypi_template.svg"
    target: Path = CURRENT_DIR / "_static" / "pypi.svg"
    with open(str(template), encoding="utf8") as f:
        svg: str = string.Template(f.read()).substitute(version=version)
    with open(str(target), "w", encoding="utf8") as f:
        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")

# -- Project information -----------------------------------------------------