vault backup: 2026-08-12 18:16:03
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
"""Remove YAML frontmatter from all markdown files in the repo.
|
||||
|
||||
Frontmatter is the block delimited by `---` at the very start of a file:
|
||||
---
|
||||
title: ...
|
||||
---
|
||||
The script strips that block (and any immediately following blank lines)
|
||||
from every .md file found recursively under the target directory.
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
FRONTMATTER_RE = re.compile(r"\A---\r?\n.*?\r?\n---\r?\n?", re.DOTALL)
|
||||
|
||||
|
||||
def strip_frontmatter(content: str) -> str:
|
||||
"""Return content with leading YAML frontmatter removed."""
|
||||
m = FRONTMATTER_RE.match(content)
|
||||
if not m:
|
||||
return content
|
||||
rest = content[m.end():]
|
||||
# Trim leading blank lines left behind after the frontmatter.
|
||||
return rest.lstrip("\r\n")
|
||||
|
||||
|
||||
def process_file(path: str, dry_run: bool = False) -> bool:
|
||||
"""Strip frontmatter from one file. Returns True if changed."""
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8-sig", newline="") as f:
|
||||
original = f.read()
|
||||
except (UnicodeDecodeError, OSError) as e:
|
||||
print(f" ! skip (read error): {path} ({e})")
|
||||
return False
|
||||
|
||||
updated = strip_frontmatter(original)
|
||||
if updated == original:
|
||||
return False
|
||||
|
||||
if dry_run:
|
||||
print(f" ~ would strip frontmatter: {path}")
|
||||
else:
|
||||
with open(path, "w", encoding="utf-8", newline="") as f:
|
||||
f.write(updated)
|
||||
print(f" ✓ stripped frontmatter: {path}")
|
||||
return True
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = [a for a in sys.argv[1:] if a != "--dry-run"]
|
||||
dry_run = "--dry-run" in sys.argv
|
||||
root = args[0] if args else os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
# Only process these subdirectories under root.
|
||||
target_dirs = ["agentss", "promps"]
|
||||
|
||||
print(f"Scanning: {root} (dirs: {', '.join(target_dirs)})"
|
||||
+ (" (dry run)" if dry_run else ""))
|
||||
changed = 0
|
||||
total = 0
|
||||
for sub in target_dirs:
|
||||
sub_root = os.path.join(root, sub)
|
||||
if not os.path.isdir(sub_root):
|
||||
print(f" ! directory not found, skipping: {sub_root}")
|
||||
continue
|
||||
for dirpath, _dirs, files in os.walk(sub_root):
|
||||
# Skip hidden dirs.
|
||||
if os.path.basename(dirpath).startswith("."):
|
||||
continue
|
||||
for name in files:
|
||||
if not name.lower().endswith(".md"):
|
||||
continue
|
||||
total += 1
|
||||
full = os.path.join(dirpath, name)
|
||||
if process_file(full, dry_run=dry_run):
|
||||
changed += 1
|
||||
|
||||
print(f"\nDone. {changed} of {total} markdown file(s) "
|
||||
f"{'would be ' if dry_run else ''}modified.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user