mirror of
https://github.com/astral-sh/ty.git
synced 2025-08-04 02:48:36 +00:00
Restore readme transformation script (#330)
This commit is contained in:
parent
43694cc492
commit
ab530d992d
2 changed files with 61 additions and 0 deletions
45
scripts/transform_readme.py
Normal file
45
scripts/transform_readme.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
"""Transform the README.md to support a specific deployment target.
|
||||
|
||||
By default, we assume that our README.md will be rendered on GitHub. However,
|
||||
PyPI includes the README with different rendering.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import urllib.parse
|
||||
from pathlib import Path
|
||||
|
||||
import tomllib
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Modify the README.md to support PyPI."""
|
||||
# Read the current version from the `dist-workspace.toml`.
|
||||
with Path("dist-workspace.toml").open(mode="rb") as fp:
|
||||
# Parse the TOML.
|
||||
dist_workspace = tomllib.load(fp)
|
||||
if "workspace" in dist_workspace and "version" in dist_workspace["workspace"]:
|
||||
version = dist_workspace["workspace"]["version"]
|
||||
else:
|
||||
raise ValueError("Version not found in dist-workspace.toml")
|
||||
|
||||
# Replace any relative URLs (e.g., `[PIP_COMPATIBILITY.md`) with absolute URLs.
|
||||
def replace(match: re.Match) -> str:
|
||||
url = match.group(1)
|
||||
if not url.startswith("http"):
|
||||
url = urllib.parse.urljoin(
|
||||
f"https://github.com/astral-sh/uv/blob/{version}/README.md", url
|
||||
)
|
||||
return f"]({url})"
|
||||
|
||||
content = re.sub(
|
||||
r"]\(([^)]+)\)", replace, Path("README.md").read_text(encoding="utf8")
|
||||
)
|
||||
|
||||
with Path("README.md").open("w", encoding="utf8") as fp:
|
||||
fp.write(content)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue