Repository navigation
Expand file tree
/
Copy pathsplit.py
More file actions
executable file
·99 lines (73 loc) · 2.71 KB
/
Copy pathsplit.py
File metadata and controls
executable file
·99 lines (73 loc) · 2.71 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
"""
CodeX — split a combined `codex-complete.md` into individual project files.
Usage:
python3 split.py [source.md]
The source markdown is expected to contain sections in either of these forms:
📄 FILE: NAME
```<lang>
<contents>
```
## 📄 FILE: NAME
```<lang>
<contents>
```
Each section's fenced code block is written verbatim to ./NAME.
Everything before the first "FILE:" marker is ignored (it is the prose/README body).
If no source file is given, it defaults to `codex-complete.md` in the current directory.
"""
from __future__ import annotations
import re
import sys
from pathlib import Path
# Matches a heading line that declares a file to emit.
FILE_MARKER = re.compile(r"^\s*(?:#{1,6}\s*)?📄\s*FILE:\s*(?P<name>\S+)\s*$")
# Matches the opening or closing fence of a fenced code block.
FENCE = re.compile(r"^```")
# Names that must never be written outside the current directory.
UNSAFE = re.compile(r"(^/|(^|/)\.\.(/|$))")
def split(source: Path, out_dir: Path) -> list[str]:
lines = source.read_text(encoding="utf-8").splitlines()
written: list[str] = []
i = 0
while i < len(lines):
match = FILE_MARKER.match(lines[i])
if not match:
i += 1
continue
name = match.group("name").strip().strip("`")
i += 1
# Skip blank lines between the marker and the opening fence.
while i < len(lines) and not lines[i].strip():
i += 1
if i >= len(lines) or not FENCE.match(lines[i]):
print(f" ! no code block after 'FILE: {name}', skipping")
continue
i += 1 # consume opening fence
body: list[str] = []
while i < len(lines) and not FENCE.match(lines[i]):
body.append(lines[i])
i += 1
i += 1 # consume closing fence
if UNSAFE.search(name):
print(f" ! refusing unsafe path: {name}")
continue
target = out_dir / name
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text("\n".join(body) + "\n", encoding="utf-8")
written.append(name)
print(f" + {name}")
return written
def main() -> int:
source = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("codex-complete.md")
if not source.is_file():
print(f"error: source not found: {source}", file=sys.stderr)
print("hint: save the combined guide as codex-complete.md first", file=sys.stderr)
return 1
out_dir = Path(".").resolve()
print(f"splitting {source} -> {out_dir}")
written = split(source, out_dir)
print(f"done: {len(written)} file(s) written")
return 0
if __name__ == "__main__":
raise SystemExit(main())