Skip to content

Commit 84f3475

Browse files
committed
Add markdown-code-runner workflow for auto-updating docs
- Add docs/run_markdown_code_runner.py script to process all docs - Add GitHub workflow to run on push/PR and auto-commit changes
1 parent dcac32c commit 84f3475

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: markdown-code-runner
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
markdown-code-runner:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Check out repository
14+
uses: actions/checkout@v4
15+
with:
16+
persist-credentials: false
17+
fetch-depth: 0
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v5
21+
with:
22+
python-version: "3.12"
23+
24+
- name: Install uv
25+
uses: astral-sh/setup-uv@v4
26+
27+
- name: Run markdown-code-runner
28+
env:
29+
PYTHONPATH: ${{ github.workspace }}/custom_components:${{ github.workspace }}
30+
run: |
31+
uv sync --group docs
32+
uv run python docs/run_markdown_code_runner.py
33+
34+
- name: Commit updated docs
35+
id: commit
36+
run: |
37+
git add -A
38+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
39+
git config --local user.name "github-actions[bot]"
40+
if git diff --quiet && git diff --staged --quiet; then
41+
echo "No changes in docs, skipping commit."
42+
echo "commit_status=skipped" >> $GITHUB_ENV
43+
else
44+
git commit -m "Update auto-generated docs"
45+
echo "commit_status=committed" >> $GITHUB_ENV
46+
fi
47+
48+
- name: Push changes
49+
if: env.commit_status == 'committed'
50+
uses: ad-m/github-push-action@master
51+
with:
52+
github_token: ${{ secrets.GITHUB_TOKEN }}
53+
branch: ${{ github.head_ref }}

docs/run_markdown_code_runner.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python3
2+
# ruff: noqa: T201, S603, S607
3+
"""Update all markdown files that use markdown-code-runner for auto-generation.
4+
5+
Run from repo root: uv run python docs/run_markdown_code_runner.py
6+
"""
7+
8+
from __future__ import annotations
9+
10+
import subprocess
11+
import sys
12+
from pathlib import Path
13+
14+
15+
def find_markdown_files_with_code_blocks(docs_dir: Path) -> list[Path]:
16+
"""Find all markdown files containing markdown-code-runner markers."""
17+
files_with_code = []
18+
for md_file in docs_dir.rglob("*.md"):
19+
content = md_file.read_text()
20+
if "<!-- CODE:START -->" in content:
21+
files_with_code.append(md_file)
22+
return sorted(files_with_code)
23+
24+
25+
def run_markdown_code_runner(files: list[Path], repo_root: Path) -> bool:
26+
"""Run markdown-code-runner on all files. Returns True if all succeeded."""
27+
if not files:
28+
print("No files with CODE:START markers found.")
29+
return True
30+
31+
print(f"Found {len(files)} file(s) with auto-generated content:")
32+
for f in files:
33+
print(f" - {f.relative_to(repo_root)}")
34+
print()
35+
36+
all_success = True
37+
for file in files:
38+
rel_path = file.relative_to(repo_root)
39+
print(f"Updating {rel_path}...", end=" ", flush=True)
40+
result = subprocess.run(
41+
["markdown-code-runner", str(file)],
42+
check=False,
43+
capture_output=True,
44+
text=True,
45+
)
46+
if result.returncode == 0:
47+
print("✓")
48+
else:
49+
print("✗")
50+
print(f" Error: {result.stderr}")
51+
all_success = False
52+
53+
return all_success
54+
55+
56+
def main() -> int:
57+
"""Main entry point."""
58+
repo_root = Path(__file__).parent.parent
59+
60+
# Also check README.md at repo root
61+
files = find_markdown_files_with_code_blocks(repo_root / "docs")
62+
readme = repo_root / "README.md"
63+
if readme.exists() and "<!-- CODE:START -->" in readme.read_text():
64+
files.append(readme)
65+
66+
success = run_markdown_code_runner(files, repo_root)
67+
return 0 if success else 1
68+
69+
70+
if __name__ == "__main__":
71+
sys.exit(main())

0 commit comments

Comments
 (0)