Download scripts/render_readme_tables.py from cy0307/awesome-loop-engineering: direct link, hf CLI and curl.
- Browser
- Download file 14.3 kB
-
https://huggingface.co/datasets/cy0307/awesome-loop-engineering/resolve/main/scripts/render_readme_tables.py
- Command line
-
hf download hf://datasets/cy0307/awesome-loop-engineering/scripts/render_readme_tables.py
-
curl -L -o render_readme_tables.py https://huggingface.co/datasets/cy0307/awesome-loop-engineering/resolve/main/scripts/render_readme_tables.py
14.3 kB
| #!/usr/bin/env python3 | |
| """Render README resources as compact, metadata-rich Markdown tables.""" | |
| from __future__ import annotations | |
| import argparse | |
| import sys | |
| from collections import Counter | |
| from pathlib import Path | |
| from export_resource_dataset import README, TYPE_MARKERS, iter_rows | |
| TABLE_HEADERS = ("Resource", "Published at", "Contribution", "Evidence") | |
| LEGACY_TABLE_HEADERS = { | |
| TABLE_HEADERS, | |
| ("Resource", "Published at", "Key feature"), | |
| ("Resource", "Publication / source", "Key feature"), | |
| } | |
| SUMMARY_START = "<!-- resource-type-summary:start -->" | |
| SUMMARY_END = "<!-- resource-type-summary:end -->" | |
| EVIDENCE_START = "<!-- evidence-label-legend:start -->" | |
| EVIDENCE_END = "<!-- evidence-label-legend:end -->" | |
| PROJECT_GITHUB_REPO = "chaoyue0307/awesome-loop-engineering" | |
| # GitHub truncates a README beyond 500 KiB (512,000 bytes) when displaying it: "any content | |
| # beyond 500 KiB will be truncated" (docs.github.com, About READMEs). The rendered output is | |
| # byte-identical to README.md and CI runs --check on every push, so the committed file can never | |
| # exceed this budget; the remaining 12,288 bytes are margin, not headroom to grow into. | |
| README_RENDER_BUDGET_BYTES = 488 * 1024 | |
| TYPE_DESCRIPTIONS = { | |
| "Paper": "Academic paper, preprint, or technical report", | |
| "Blog": "Essay, field note, article, or practitioner write-up", | |
| "Docs": "Official product, API, SDK, or platform documentation", | |
| "Tool": "Repository, framework, SDK, runtime, or implementation", | |
| "Benchmark": "Benchmark, eval suite, leaderboard, or evaluation dataset", | |
| "Pattern": "Operational playbook or reusable workflow", | |
| "Template": "Template, checklist, schema, guide, or contribution artifact", | |
| "List": "Adjacent directory, ecosystem map, or reading list", | |
| "Critique": "Risk analysis, limitation, caveat, or skeptical take", | |
| } | |
| GITHUB_OWNER_LABELS = { | |
| "cloudflare": "Cloudflare", | |
| "vercel": "Vercel", | |
| } | |
| EVIDENCE_LABELS = { | |
| "research-paper": "Research paper", | |
| "research-preprint": "Research preprint", | |
| "official-documentation": "Official documentation", | |
| "technical-documentation": "Technical documentation", | |
| "source-implementation": "Source implementation", | |
| "implementation": "Implementation", | |
| "benchmark": "Benchmark or evaluation", | |
| "repository-native": "Project artifact", | |
| "reusable-artifact": "Reusable artifact", | |
| "operational-pattern": "Operational pattern", | |
| "practitioner-analysis": "Practitioner analysis", | |
| "risk-analysis": "Risk analysis", | |
| "curated-index": "Discovery index", | |
| "curated-source": "Background source", | |
| } | |
| EVIDENCE_NOTES = { | |
| "research-paper": "Published or accepted research record", | |
| "research-preprint": "Preprint; inspect methods and evaluation", | |
| "official-documentation": "Primary product or standard behavior", | |
| "technical-documentation": "Technical reference from the source", | |
| "source-implementation": "Inspectable source and runtime behavior", | |
| "implementation": "Working implementation or runtime", | |
| "benchmark": "Repeatable tasks, scores, or evaluation data", | |
| "repository-native": "Schema, example, guide, or repository documentation", | |
| "reusable-artifact": "Adaptable template, schema, or guide", | |
| "operational-pattern": "Transferable operating practice", | |
| "practitioner-analysis": "Experience-backed implementation context", | |
| "risk-analysis": "Failure modes, limits, or adoption cautions", | |
| "curated-index": "Broader ecosystem coverage for finding related work", | |
| "curated-source": "Background context for comparison", | |
| } | |
| def escape_cell(value: str) -> str: | |
| return value.replace("|", r"\|") | |
| def author_summary(authors: str) -> str: | |
| names = [name.strip() for name in authors.split(";") if name.strip()] | |
| if not names: | |
| return "" | |
| if len(names) == 1: | |
| return names[0] | |
| if len(names) == 2: | |
| return f"{names[0]} & {names[1]}" | |
| return f"{names[0]} et al." | |
| def repository_artifact_label(url: str) -> str: | |
| path = url.split("#", 1)[0] | |
| if path.startswith("patterns/"): | |
| return "Pattern library" | |
| if path.startswith("templates/"): | |
| return "Template library" | |
| if path.startswith("schemas/"): | |
| return "Schema" | |
| if path.startswith("examples/runnable/"): | |
| return "Runnable example" | |
| if path.startswith("examples/"): | |
| return "Example library" | |
| if path.startswith("gallery/"): | |
| return "Community gallery" | |
| if path.startswith("docs/"): | |
| return "Project website" | |
| if path.startswith("scripts/"): | |
| return "Repository utility" | |
| if path.startswith("meta/"): | |
| return "Project operations guide" | |
| if path.startswith("posts/"): | |
| return "Project article" | |
| return "Project documentation" | |
| def publication_cell(row: dict[str, str]) -> str: | |
| year = row["publication_year"] | |
| resource_type = row["resource_type"] | |
| details: list[str] = [] | |
| github_repo = row["github_repo"] | |
| if row["url_kind"] != "external": | |
| source = row["publication_venue"] or row["publisher"] or "GitHub" | |
| if resource_type == "Paper" and row["authors"]: | |
| details.append(author_summary(row["authors"])) | |
| else: | |
| details.append(repository_artifact_label(row["url"])) | |
| elif github_repo.lower() == PROJECT_GITHUB_REPO: | |
| source = row["publication_venue"] or row["publisher"] or "GitHub" | |
| elif github_repo: | |
| source = row["publisher"] or "GitHub" | |
| owner, separator, repository = github_repo.partition("/") | |
| owner = GITHUB_OWNER_LABELS.get(owner, owner) | |
| # Licenses stay in the dataset and Resource Atlas with the other repository statistics. | |
| # Rendering them here cost about 2.3 KB against the GitHub render budget, and they | |
| # appeared or vanished between audits depending on GitHub API rate limits. | |
| details.append(f"{owner}{separator}{repository}") | |
| else: | |
| source = row["publication_venue"] or row["publisher"] or row["domain"] or "Source not stated" | |
| if resource_type in {"Paper", "Benchmark", "Blog", "Critique"}: | |
| author = author_summary(row["authors"]) | |
| if author: | |
| details.append(author) | |
| primary = ( | |
| f"**{escape_cell(year)}** · {escape_cell(source)}" | |
| if year | |
| else f"**{escape_cell(source)}**" | |
| ) | |
| if details: | |
| secondary = " · ".join(escape_cell(detail) for detail in details) | |
| return f"{primary}<br><sub>{secondary}</sub>" | |
| return primary | |
| SOURCE_STATUS_MARKS = {"restricted": "\u2021", "unavailable": "\u2020"} | |
| SOURCE_STATUS_FOOTNOTES = { | |
| "restricted": "The linked source required access at the latest check", | |
| "unavailable": "The linked source was unavailable at the latest check", | |
| } | |
| def status_key(row: dict[str, str]) -> str | None: | |
| """Bucket a row's source status into the footnote it needs, or None when it needs none.""" | |
| status = row["source_status"] | |
| if status == "restricted": | |
| return "restricted" | |
| if status not in {"ok", "local_ok"}: | |
| return "unavailable" | |
| return None | |
| def evidence_cell(row: dict[str, str]) -> str: | |
| # The per-class note is constant for every row of a class, so it is defined once in the | |
| # evidence-label legend rather than repeated on each row; repeating it cost roughly 40 KB | |
| # against the GitHub render budget. Only per-row exceptions still carry an inline note. | |
| evidence = row["evidence_class"] | |
| label = EVIDENCE_LABELS.get(evidence, evidence.replace("-", " ").title()) | |
| # The status note is constant for every row that carries it, so it is defined once as a | |
| # legend footnote rather than repeated inline, the same way the per-class note already is. | |
| # Repeating it inline cost about 750 bytes against the GitHub render budget. | |
| mark = SOURCE_STATUS_MARKS.get(status_key(row)) | |
| if mark is None: | |
| return f"**{escape_cell(label)}**" | |
| return f"**{escape_cell(label)}** {mark}" | |
| def resource_cells(row: dict[str, str]) -> tuple[str, str, str, str]: | |
| title = escape_cell(row["title"]) | |
| url = escape_cell(row["url"]) | |
| annotation = escape_cell(row["annotation"]) | |
| marker = escape_cell(row["marker"]) | |
| resource_type = escape_cell(row["resource_type"]) | |
| # The type label is a constant function of the marker (the mapping is 1:1) and is defined | |
| # once in the Resource Type Legend, so it is not repeated on every row; repeating it cost | |
| # roughly 19 KB against the GitHub render budget. Model-layer rows keep their scope note, | |
| # which is the only part of the old subtype that was not derivable from the marker. | |
| resource = f"{marker} **[{title}]({url})**" | |
| if row.get("section") == "Model-Level Recurrence": | |
| resource += f"<br><sub>{escape_cell('Model layer · Adjacent foundation')}</sub>" | |
| return resource, publication_cell(row), annotation, evidence_cell(row) | |
| def markdown_table(headers: tuple[str, ...], data: list[tuple[str, ...]]) -> list[str]: | |
| # Aligned padding added over 200 KB and caused GitHub to truncate the README. | |
| def format_row(row: tuple[str, ...]) -> str: | |
| return "| " + " | ".join(row) + " |" | |
| separator = tuple("---" for _ in headers) | |
| return [format_row(headers), format_row(separator), *(format_row(row) for row in data)] | |
| def is_resource_table_header(line: str) -> bool: | |
| if not line.startswith("|"): | |
| return False | |
| cells = tuple(cell.strip() for cell in line.strip().strip("|").split("|")) | |
| return cells in LEGACY_TABLE_HEADERS | |
| def is_table_separator(line: str) -> bool: | |
| if not line.startswith("|"): | |
| return False | |
| cells = tuple(cell.strip() for cell in line.strip().strip("|").split("|")) | |
| valid_lengths = {len(headers) for headers in LEGACY_TABLE_HEADERS} | |
| return len(cells) in valid_lengths and all( | |
| len(cell) >= 3 and not cell.strip("-") for cell in cells | |
| ) | |
| def summary_lines(rows: list[dict[str, str]]) -> list[str]: | |
| counts = Counter(row["resource_type"] for row in rows) | |
| data = [] | |
| for resource_type, marker in TYPE_MARKERS.items(): | |
| data.append( | |
| ( | |
| f"{marker} **{resource_type}**", | |
| str(counts[resource_type]), | |
| TYPE_DESCRIPTIONS[resource_type], | |
| ) | |
| ) | |
| return [SUMMARY_START, *markdown_table(("Type", "Rows", "Includes"), data), SUMMARY_END] | |
| def replace_summary(lines: list[str], rows: list[dict[str, str]]) -> list[str]: | |
| try: | |
| start = lines.index(SUMMARY_START) | |
| end = lines.index(SUMMARY_END, start + 1) | |
| except ValueError as error: | |
| raise RuntimeError("README resource-type summary markers are missing") from error | |
| return lines[:start] + summary_lines(rows) + lines[end + 1 :] | |
| def evidence_legend_lines(rows: list[dict[str, str]]) -> list[str]: | |
| """Define each evidence label once, since rows no longer repeat the per-class note.""" | |
| counts = Counter(row["evidence_class"] for row in rows) | |
| data = [] | |
| for evidence, label in EVIDENCE_LABELS.items(): | |
| if not counts[evidence]: | |
| continue | |
| data.append( | |
| ( | |
| f"**{escape_cell(label)}**", | |
| str(counts[evidence]), | |
| escape_cell(EVIDENCE_NOTES.get(evidence, "Inspect the linked source")), | |
| ) | |
| ) | |
| lines = [EVIDENCE_START, *markdown_table(("Evidence label", "Rows", "What it means"), data)] | |
| present = {key for key in (status_key(row) for row in rows) if key is not None} | |
| for key, mark in SOURCE_STATUS_MARKS.items(): | |
| if key in present: | |
| lines.append("") | |
| break | |
| for key, mark in SOURCE_STATUS_MARKS.items(): | |
| if key in present: | |
| lines.append(f"{mark} {SOURCE_STATUS_FOOTNOTES[key]}.") | |
| lines.append(EVIDENCE_END) | |
| return lines | |
| def replace_evidence_legend(lines: list[str], rows: list[dict[str, str]]) -> list[str]: | |
| try: | |
| start = lines.index(EVIDENCE_START) | |
| end = lines.index(EVIDENCE_END, start + 1) | |
| except ValueError as error: | |
| raise RuntimeError("README evidence-label legend markers are missing") from error | |
| return lines[:start] + evidence_legend_lines(rows) + lines[end + 1 :] | |
| def render_readme(readme_path: Path = README) -> str: | |
| rows = iter_rows(readme_path) | |
| rows_by_line = {int(row["source_line"]): row for row in rows} | |
| lines = readme_path.read_text(encoding="utf-8").splitlines() | |
| output: list[str] = [] | |
| index = 0 | |
| while index < len(lines): | |
| line = lines[index] | |
| if is_resource_table_header(line): | |
| index += 1 | |
| if index < len(lines) and is_table_separator(lines[index]): | |
| index += 1 | |
| continue | |
| row = rows_by_line.get(index + 1) | |
| if row: | |
| run: list[tuple[str, ...]] = [] | |
| while index < len(lines): | |
| row = rows_by_line.get(index + 1) | |
| if not row: | |
| break | |
| run.append(resource_cells(row)) | |
| index += 1 | |
| output.extend(markdown_table(TABLE_HEADERS, run)) | |
| continue | |
| output.append(line) | |
| index += 1 | |
| output = replace_summary(output, rows) | |
| output = replace_evidence_legend(output, rows) | |
| return "\n".join(output) + "\n" | |
| def main() -> int: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--check", action="store_true", help="fail when README tables are stale") | |
| args = parser.parse_args() | |
| rendered = render_readme() | |
| rendered_bytes = len(rendered.encode("utf-8")) | |
| if rendered_bytes > README_RENDER_BUDGET_BYTES: | |
| print( | |
| f"README is {rendered_bytes:,} bytes; keep it below the " | |
| f"{README_RENDER_BUDGET_BYTES:,}-byte GitHub render budget", | |
| file=sys.stderr, | |
| ) | |
| return 1 | |
| current = README.read_text(encoding="utf-8") | |
| if args.check: | |
| if rendered != current: | |
| print("README resource tables are stale; run scripts/render_readme_tables.py", file=sys.stderr) | |
| return 1 | |
| return 0 | |
| README.write_text(rendered, encoding="utf-8") | |
| print(f"Rendered {len(iter_rows())} resource rows in {README.name}") | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |