2024-05-13 23:40:34 -04:00
|
|
|
import os
|
2025-06-01 01:34:20 -04:00
|
|
|
import re
|
2021-06-22 13:18:22 +01:00
|
|
|
import subprocess
|
2024-05-13 23:40:34 -04:00
|
|
|
import sysconfig
|
2021-06-22 13:18:22 +01:00
|
|
|
from typing import Any
|
|
|
|
|
|
2025-06-01 01:34:20 -04:00
|
|
|
import rich.console
|
|
|
|
|
import rich.text
|
|
|
|
|
|
2021-06-22 13:18:22 +01:00
|
|
|
|
|
|
|
|
def define_env(env: Any) -> None:
|
|
|
|
|
"Hook function for mkdocs-macros"
|
|
|
|
|
|
2025-12-02 18:06:04 -05:00
|
|
|
@env.macro # type: ignore[untyped-decorator]
|
2021-06-22 13:18:22 +01:00
|
|
|
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" ')
|