* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.14.6 → v0.14.7](https://github.com/astral-sh/ruff-pre-commit/compare/v0.14.6...v0.14.7) - [github.com/pre-commit/mirrors-mypy: v1.18.2 → v1.19.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.18.2...v1.19.0) * chore: update for newer mypy Signed-off-by: Henry Schreiner <henryfs@princeton.edu> --------- Signed-off-by: Henry Schreiner <henryfs@princeton.edu> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Henry Schreiner <henryfs@princeton.edu>
31 lines
1.0 KiB
Python
31 lines
1.0 KiB
Python
import os
|
|
import re
|
|
import subprocess
|
|
import sysconfig
|
|
from typing import Any
|
|
|
|
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"
|
|
env = os.environ.copy()
|
|
scripts = sysconfig.get_path("scripts")
|
|
env.pop("NO_COLOR", None)
|
|
env["PATH"] = f"{scripts}{os.pathsep}{env.get('PATH', '')}"
|
|
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" ')
|