Files
cibuildwheel/docs/main.py
T

31 lines
1.0 KiB
Python
Raw Normal View History

2024-05-13 23:40:34 -04:00
import os
2025-06-01 01:34:20 -04:00
import re
import subprocess
2024-05-13 23:40:34 -04:00
import sysconfig
from typing import Any
2025-06-01 01:34:20 -04:00
import rich.console
import rich.text
def define_env(env: Any) -> None:
"Hook function for mkdocs-macros"
@env.macro # type: ignore[untyped-decorator]
def subprocess_run(*args: str) -> str:
"Run a subprocess and return the stdout"
2024-05-13 23:40:34 -04:00
env = os.environ.copy()
scripts = sysconfig.get_path("scripts")
2025-06-01 01:34:20 -04:00
env.pop("NO_COLOR", None)
2024-05-13 23:40:34 -04:00
env["PATH"] = f"{scripts}{os.pathsep}{env.get('PATH', '')}"
2025-06-01 01:34:20 -04:00
env["PYTHON_COLORS"] = "1"
output = subprocess.run(args, check=True, capture_output=True, text=True, env=env).stdout
rich_text = rich.text.Text.from_ansi(output)
console = rich.console.Console(record=True, force_terminal=True)
console.print(rich_text)
page = console.export_html(inline_styles=True)
result = re.search(r"<body.*?>(.*?)</body>", page, re.DOTALL | re.IGNORECASE)
assert result
txt = result.group(1).strip()
return txt.replace("code ", 'code class="nohighlight" ')