|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Builds prompts/INDEX.md with a table of all prompt files. |
| 4 | +Run from repo root: python scripts/build_index.py |
| 5 | +""" |
| 6 | + |
| 7 | +import pathlib |
| 8 | +import re |
| 9 | +import textwrap |
| 10 | +from urllib.parse import quote |
| 11 | + |
| 12 | +README_FILE = pathlib.Path("README.md") |
| 13 | + |
| 14 | +PROMPTS_DIR = pathlib.Path("prompts") |
| 15 | +INDEX_FILE = PROMPTS_DIR / "INDEX.md" |
| 16 | +INDEX_HTML = pathlib.Path("index.html") |
| 17 | + |
| 18 | +CATEGORY_NAMES = { |
| 19 | + "writing_editing": "Writing\u00A0&\u00A0Editing", |
| 20 | + "programming": "Programming", |
| 21 | + "prompt_generation": "Prompt\u00A0Generation", |
| 22 | + "medical": "Medical", |
| 23 | + "finance": "Finance", |
| 24 | + "miscellaneous": "Miscellaneous", |
| 25 | +} |
| 26 | + |
| 27 | +def extract_title(path: pathlib.Path) -> str: |
| 28 | + """Grab first non‑empty heading line or fallback to filename.""" |
| 29 | + for line in path.read_text(encoding="utf‑8").splitlines(): |
| 30 | + if m := re.match(r"#\s*(.+)", line): |
| 31 | + return m.group(1).strip() |
| 32 | + return path.stem.replace("_", " ") |
| 33 | + |
| 34 | + |
| 35 | +def build_site_sections() -> str: |
| 36 | + """Return HTML sections listing prompts per category.""" |
| 37 | + sections = [] |
| 38 | + for category_path in sorted(PROMPTS_DIR.iterdir()): |
| 39 | + if not category_path.is_dir(): |
| 40 | + continue |
| 41 | + files = sorted(f for f in category_path.iterdir() if f.is_file()) |
| 42 | + if not files: |
| 43 | + continue |
| 44 | + title = CATEGORY_NAMES.get( |
| 45 | + category_path.name, category_path.name.replace("_", " ").title() |
| 46 | + ) |
| 47 | + sections.append( |
| 48 | + f" <!-- {title.upper()} ({len(files)}) -->\n" |
| 49 | + f" <section id=\"{category_path.name}\"><h2 class=\"section-heading\">{title}</h2><ul class=\"grid sm:grid-cols-2 lg:grid-cols-3 gap-3\">" |
| 50 | + ) |
| 51 | + for f in files: |
| 52 | + if f.suffix == ".md": |
| 53 | + rel = f.relative_to(PROMPTS_DIR).as_posix() |
| 54 | + url = quote(f"prompts/{rel}", safe="/") |
| 55 | + text = extract_title(f) |
| 56 | + else: |
| 57 | + first = f.read_text(encoding="utf-8").strip().splitlines()[0] |
| 58 | + m = re.match(r"\[(.+?)\]\((.+?)\)", first) |
| 59 | + if m: |
| 60 | + text, url = m.groups() |
| 61 | + else: |
| 62 | + text = f.stem |
| 63 | + rel = f.relative_to(PROMPTS_DIR).as_posix() |
| 64 | + url = quote(f"prompts/{rel}", safe="/") |
| 65 | + sections.append( |
| 66 | + f" <li><a href=\"{url}\" class=\"prompt-link block p-4 rounded\">{text}</a></li>" |
| 67 | + ) |
| 68 | + sections.append(" </ul></section>") |
| 69 | + sections.append("") |
| 70 | + return "\n".join(sections).rstrip() |
| 71 | + |
| 72 | + |
| 73 | +def update_index_html(sections_html: str) -> None: |
| 74 | + text = INDEX_HTML.read_text(encoding="utf-8") |
| 75 | + pattern = re.compile( |
| 76 | + r"(<!-- BEGIN AUTO-GENERATED -->)(.*?)(<!-- END AUTO-GENERATED -->)", re.S |
| 77 | + ) |
| 78 | + new_text = pattern.sub(f"\\1\n{sections_html}\n\\3", text) |
| 79 | + INDEX_HTML.write_text(new_text, encoding="utf-8") |
| 80 | + |
| 81 | + |
| 82 | +def build_catalogue_table() -> str: |
| 83 | + """Return markdown table rows summarizing prompt counts per category.""" |
| 84 | + rows = [] |
| 85 | + total = 0 |
| 86 | + for category_path in sorted(PROMPTS_DIR.iterdir()): |
| 87 | + if not category_path.is_dir(): |
| 88 | + continue |
| 89 | + files = sorted(category_path.rglob("*.md")) |
| 90 | + if not files: |
| 91 | + continue |
| 92 | + count = len(files) |
| 93 | + total += count |
| 94 | + title = CATEGORY_NAMES.get( |
| 95 | + category_path.name, category_path.name.replace("_", " ").title() |
| 96 | + ) |
| 97 | + examples = [] |
| 98 | + for f in files[:2]: |
| 99 | + rel = f.relative_to(PROMPTS_DIR).as_posix() |
| 100 | + url = quote(rel, safe="/") |
| 101 | + examples.append(f"[{extract_title(f)}](prompts/{url})") |
| 102 | + rows.append(f"| {title} | **{count}** | {' • '.join(examples)} |") |
| 103 | + rows.append(f"| **Total** | **{total}** | — |") |
| 104 | + return "\n".join(rows) |
| 105 | + |
| 106 | + |
| 107 | +def update_readme(table_rows: str) -> None: |
| 108 | + text = README_FILE.read_text(encoding="utf-8") |
| 109 | + pattern = re.compile( |
| 110 | + r"(<!-- AUTO\u2011GENERATED:.*?-->)(.*?)(<!-- /AUTO\u2011GENERATED -->)", |
| 111 | + re.S, |
| 112 | + ) |
| 113 | + replacement = ("\\1\n| Category | Prompts | Example\u00A0links |\n" |
| 114 | + "| -------- | ------: | ------------- |\n" |
| 115 | + f"{table_rows}\n\\3") |
| 116 | + new_text = pattern.sub(replacement, text) |
| 117 | + README_FILE.write_text(new_text, encoding="utf-8") |
| 118 | + |
| 119 | +def main() -> None: |
| 120 | + files = sorted(PROMPTS_DIR.rglob("*.md")) |
| 121 | + rows = [] |
| 122 | + for f in files: |
| 123 | + if f.name == "INDEX.md": |
| 124 | + continue |
| 125 | + rel_path = f.relative_to(PROMPTS_DIR).as_posix() |
| 126 | + url = quote(rel_path, safe="/") # “ ” → %20, “/” untouched |
| 127 | + rows.append(f"| [{rel_path}]({url}) | {extract_title(f)} |") |
| 128 | + |
| 129 | + header = textwrap.dedent("""\ |
| 130 | + <!--- AUTO‑GENERATED: do not edit manually. Run scripts/build_index.py --> |
| 131 | + # Prompt Index |
| 132 | +
|
| 133 | + | Path | Title | |
| 134 | + |------|-------| |
| 135 | + """) |
| 136 | + INDEX_FILE.write_text(header + "\n".join(rows) + "\n", encoding="utf‑8") |
| 137 | + print(f"Generated {INDEX_FILE} with {len(rows)} entries") |
| 138 | + |
| 139 | + table = build_catalogue_table() |
| 140 | + update_readme(table) |
| 141 | + |
| 142 | + sections = build_site_sections() |
| 143 | + update_index_html(sections) |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + main() |
0 commit comments