Files
ChronoCCG/add_name_frontmatter.py
T

63 lines
1.5 KiB
Python

import os
CARD_ROOT = "chrono.docs"
CATEGORIES = ["Agent", "Spell", "Immortalized", "Token"]
card_files = []
for cat in CATEGORIES:
cat_path = os.path.join(CARD_ROOT, cat)
if os.path.isdir(cat_path):
for root, dirs, files in os.walk(cat_path):
for f in sorted(files):
if f.endswith(".md"):
card_files.append(os.path.join(root, f))
card_files.sort()
print(f"Found {len(card_files)} card files")
updated = 0
skipped = 0
for filepath in card_files:
name = os.path.basename(filepath).replace(".md", "")
with open(filepath, "r", encoding="utf-8") as f:
content = f.read()
if content.count("---") < 2:
print(f"SKIP (no frontmatter): {filepath}")
continue
front = content.split("---")[1]
has_name = False
for line in front.split("\n"):
if line.startswith("name:"):
has_name = True
break
if has_name:
skipped += 1
continue
lines = content.split("\n")
first_sep = None
for i, line in enumerate(lines):
if line.strip() == "---":
first_sep = i
break
if first_sep is None:
print(f"SKIP (no ---): {filepath}")
continue
new_lines = lines[:first_sep + 1]
new_lines.append(f"name: {name}")
new_lines.extend(lines[first_sep + 1:])
with open(filepath, "w", encoding="utf-8") as f:
f.write("\n".join(new_lines))
updated += 1
print(f"Done. Updated: {updated}, Skipped (already had): {skipped}")