-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclean_llms_md.py
More file actions
65 lines (51 loc) · 2.01 KB
/
Copy pathclean_llms_md.py
File metadata and controls
65 lines (51 loc) · 2.01 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
#!/usr/bin/env python3
"""Strip residual block-level HTML wrappers from generated .llms.md sidecars.
The gfm writer + the simplify_tables_gfm.lua filter already linearize tables and
remove styling, but Quarto injects crossref float wrappers (`<div id="tbl-…">` …
`</div>`) and the occasional layout `<div style="overflow-x:…">` AFTER the pandoc
filters run, so they can't be removed at filter level. These bare wrapper lines are
pure noise in the plain-text companion the RAG ingests. This post-render pass removes
standalone block-level `<div>`/`</div>` lines (inline tags like <sub>/<sup> in prose
are left intact).
Idempotent. Usage:
python3 clean_llms_md.py _site # walk *.llms.md under a directory
python3 clean_llms_md.py a.llms.md b.md # specific files
"""
import re
import sys
from pathlib import Path
# A line that is ONLY an opening/closing <div …> (optionally indented). Block-level
# wrapper noise — never matches inline tags mid-prose.
_BARE_DIV = re.compile(r"^[ \t]*</?div\b[^>]*>[ \t]*$")
def clean_text(text: str) -> str:
out = [ln for ln in text.split("\n") if not _BARE_DIV.match(ln)]
cleaned = "\n".join(out)
# collapse the blank-line runs a removed wrapper can leave behind (3+ → 2)
cleaned = re.sub(r"\n{3,}", "\n\n", cleaned)
return cleaned
def clean_file(path: Path) -> bool:
original = path.read_text(encoding="utf-8")
cleaned = clean_text(original)
if cleaned != original:
path.write_text(cleaned, encoding="utf-8")
return True
return False
def _iter_targets(args):
for a in args:
p = Path(a)
if p.is_dir():
yield from p.rglob("*.llms.md")
elif p.exists():
yield p
def main(argv) -> int:
if not argv:
print(__doc__)
return 1
changed = 0
for path in _iter_targets(argv):
if clean_file(path):
changed += 1
print(f"clean_llms_md: cleaned {changed} file(s)")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))