ty/scripts/transform_readme.py

59 lines
1.9 KiB
Python
Raw Permalink Normal View History

"""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 tomllib
import urllib.parse
from pathlib import Path
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")
content = Path("README.md").read_text(encoding="utf8")
Add repair of the benchmark images to the PyPI readme transform (#1947) ```diff diff --git a/README.md b/README.md index 2114b0b..9d1eca4 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ An extremely fast Python type checker and language server, written in Rust. <br /> <p align="center"> - <img alt="Shows a bar chart with benchmark results." width="500px" src="./docs/assets/ty-benchmark-cli.svg"> + <img alt="Shows a bar chart with benchmark results." width="500px" src="https://raw.githubusercontent.com/astral-sh/ty/0.0.1-alpha.35/docs/assets/ty-benchmark-cli.svg"> </p> <p align="center"> @@ -47,9 +47,9 @@ To learn more about using ty, see the [documentation](https://docs.astral.sh/ty/ ## Installation -To install ty, see the [installation](./installation.md) documentation. +To install ty, see the [installation](https://github.com/astral-sh/ty/blob/0.0.1-alpha.35/installation.md) documentation. -To add the ty language server to your editor, see the [editor integration](./editors.md) guide. +To add the ty language server to your editor, see the [editor integration](https://github.com/astral-sh/ty/blob/0.0.1-alpha.35/editors.md) guide. ## Getting help @@ -65,7 +65,7 @@ at this time. Please [open pull requests](https://github.com/astral-sh/ruff/pull to anything in the `ruff` submodule (which includes all of the Rust source code). See the -[contributing guide](./CONTRIBUTING.md) for more details. +[contributing guide](https://github.com/astral-sh/ty/blob/0.0.1-alpha.35/CONTRIBUTING.md) for more details. ## FAQ @@ -85,7 +85,7 @@ Just "ty", please. ## License -ty is licensed under the MIT license ([LICENSE](LICENSE) or +ty is licensed under the MIT license ([LICENSE](https://github.com/astral-sh/ty/blob/0.0.1-alpha.35/LICENSE) or <https://opensource.org/licenses/MIT>). Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in ty ```
2025-12-17 01:24:58 +08:00
# Replace relative src="./..." attributes with absolute GitHub raw URLs.
def replace_src(match: re.Match) -> str:
path = match.group(1).lstrip("./")
return f'src="https://raw.githubusercontent.com/astral-sh/ty/{version}/{path}"'
content = re.sub(r'src="(\./[^"]+)"', replace_src, content)
# Replace any relative URLs (e.g., `[CONTRIBUTING.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/ty/blob/{version}/README.md", url
)
return f"]({url})"
content = re.sub(r"]\(([^)]+)\)", replace, content)
# Replace any GitHub admonitions
def replace(match: re.Match) -> str:
name = match.group(1)
return f"> {name}:"
content = re.sub(r"> \[\!(\w*)\]", replace, content)
with Path("README.md").open("w", encoding="utf8") as fp:
fp.write(content)
if __name__ == "__main__":
main()