|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.11" |
| 3 | +# dependencies = ["pyyaml"] |
| 4 | +# /// |
| 5 | +"""Generate the README Inputs table from action.yml. |
| 6 | +
|
| 7 | +Replaces tj-actions/auto-doc: parses the action's inputs and rewrites the |
| 8 | +GitHub-flavoured Markdown table between the AUTO-DOC-INPUT markers in README.md. |
| 9 | +""" |
| 10 | + |
| 11 | +import pathlib |
| 12 | +import re |
| 13 | + |
| 14 | +import yaml |
| 15 | + |
| 16 | +ROOT = pathlib.Path(__file__).parent |
| 17 | +ACTION = ROOT / "action.yml" |
| 18 | +README = ROOT / "README.md" |
| 19 | +START = "<!-- AUTO-DOC-INPUT:START - Do not remove or modify this section -->" |
| 20 | +END = "<!-- AUTO-DOC-INPUT:END -->" |
| 21 | + |
| 22 | + |
| 23 | +def render_description(text: str) -> str: |
| 24 | + """Render an action.yml description as a single Markdown table cell. |
| 25 | +
|
| 26 | + Wrapped prose lines are joined with spaces; `*`-prefixed lines (e.g. the |
| 27 | + list of actions) become `<br>`-separated bullets so they render as a list |
| 28 | + inside the cell rather than a run of literal asterisks. |
| 29 | + """ |
| 30 | + parts: list[str] = [] |
| 31 | + for raw in text.strip().splitlines(): |
| 32 | + line = raw.strip() |
| 33 | + if not line: |
| 34 | + continue |
| 35 | + if line.startswith("* "): |
| 36 | + parts.append("<br>• " + line[2:].strip()) |
| 37 | + elif parts: |
| 38 | + parts[-1] += " " + line |
| 39 | + else: |
| 40 | + parts.append(line) |
| 41 | + return "".join(parts) |
| 42 | + |
| 43 | + |
| 44 | +def render_table(inputs: dict) -> str: |
| 45 | + rows = [ |
| 46 | + "| Input | Type | Required | Default | Description |", |
| 47 | + "| --- | --- | --- | --- | --- |", |
| 48 | + ] |
| 49 | + for name in sorted(inputs): |
| 50 | + spec = inputs[name] or {} |
| 51 | + required = "true" if spec.get("required") else "false" |
| 52 | + default = spec.get("default") |
| 53 | + default_cell = f"`{default}`" if default not in (None, "") else "" |
| 54 | + description = render_description(str(spec.get("description", ""))) |
| 55 | + rows.append(f"| `{name}` | string | {required} | {default_cell} | {description} |") |
| 56 | + return "\n".join(rows) |
| 57 | + |
| 58 | + |
| 59 | +def main() -> None: |
| 60 | + action = yaml.safe_load(ACTION.read_text(encoding="utf-8")) |
| 61 | + table = render_table(action.get("inputs") or {}) |
| 62 | + block = f"{START}\n\n{table}\n\n{END}" |
| 63 | + |
| 64 | + readme = README.read_text(encoding="utf-8") |
| 65 | + pattern = re.escape(START) + r".*?" + re.escape(END) |
| 66 | + if not re.search(pattern, readme, flags=re.DOTALL): |
| 67 | + raise SystemExit("AUTO-DOC-INPUT markers not found in README.md") |
| 68 | + |
| 69 | + new = re.sub(pattern, lambda _: block, readme, flags=re.DOTALL) |
| 70 | + README.write_text(new, encoding="utf-8", newline="\n") |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + main() |
0 commit comments