File size: 1,395 Bytes
3249e1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Build prompts.json for the Prompt Library tab from ../../images/prompt-pack.md.

Run locally:  python build_prompts.py [path/to/prompt-pack.md]
Each fenced code block becomes a card: {section, label, prompt}.
"""
import json, re, sys, pathlib

def parse(md: str):
    cards, section, label = [], "General", ""
    lines = md.splitlines()
    i = 0
    while i < len(lines):
        ln = lines[i]
        if ln.startswith("#"):
            section = ln.lstrip("#").strip()
            label = ""
        elif re.match(r"^\*\*[^*]+\*\*\s*$", ln.strip()):
            label = ln.strip().strip("*").strip()
        elif ln.strip().startswith("```"):
            buf = []
            i += 1
            while i < len(lines) and not lines[i].strip().startswith("```"):
                buf.append(lines[i]); i += 1
            text = "\n".join(buf).strip()
            if text:
                cards.append({"section": section, "label": label or "Prompt", "prompt": text})
        i += 1
    return cards

if __name__ == "__main__":
    here = pathlib.Path(__file__).parent
    src = pathlib.Path(sys.argv[1]) if len(sys.argv) > 1 else here.parent.parent / "images" / "prompt-pack.md"
    cards = parse(src.read_text(encoding="utf-8"))
    (here / "prompts.json").write_text(json.dumps(cards, indent=1, ensure_ascii=False), encoding="utf-8")
    print(f"wrote {len(cards)} prompt cards from {src}")