Files
rick 0489a2dbdc fix(1 Capable): restore mechanics destroyed by broken rewrite-mechanics.py
The prior editorial commit used a positional-arg version of
rewrite-mechanics.py (overwritten by Expert-tier agent) which wrote
the literal flag text "-m" or "--subhead" as mechanics content.

Fixed rewrite-mechanics.py to use argparse with -m/--mechanics and
-s/--subhead flags (with positional fallback). Restored correct
mechanics for all 32 affected items and reapplied all editorial fixes.

Made-with: Cursor
2026-04-17 15:14:43 +02:00

119 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""Rewrite front-side mechanics text and/or subhead in an existing SVG card.
Usage:
python3 scripts/rewrite-mechanics.py "path/to/Card.svg" --mechanics "New mechanics..."
python3 scripts/rewrite-mechanics.py "path/to/Card.svg" --subhead "throwable (60')"
python3 scripts/rewrite-mechanics.py "path/to/Card.svg" --mechanics "..." --subhead "..."
python3 scripts/rewrite-mechanics.py "path/to/Card.svg" "New mechanics..." (positional fallback)
Uses the same set_rich_text() pipeline as generate-card.py for mechanics text,
handling **bold** and *italic* markup, line wrapping, and tspan layout.
"""
import sys
import os
import re
import argparse
import xml.etree.ElementTree as ET
sys.path.insert(0, os.path.dirname(__file__))
from importlib.machinery import SourceFileLoader
gc = SourceFileLoader("generate_card", os.path.join(os.path.dirname(__file__), "generate-card.py")).load_module()
SVG_NS = "http://www.w3.org/2000/svg"
ET.register_namespace("", SVG_NS)
for prefix, uri in [
("xlink", "http://www.w3.org/1999/xlink"),
("inkscape", "http://www.inkscape.org/namespaces/inkscape"),
("sodipodi", "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"),
("svg", SVG_NS),
]:
ET.register_namespace(prefix, uri)
MECHANICS_WRAP_WIDTH = 42
def find_mechanics_element(root):
"""Locate the front-side mechanics text element (shape-inside, x_offset < 50)."""
texts = []
for child in root:
tag = child.tag.split("}")[-1] if "}" in child.tag else child.tag
if tag == "text":
texts.append(child)
elif tag == "g":
for sub in child:
sub_tag = sub.tag.split("}")[-1] if "}" in sub.tag else sub.tag
if sub_tag == "text":
texts.append(sub)
for t in texts:
style = t.get("style", "")
transform = t.get("transform", "")
if "shape-inside" in style and "matrix" in transform:
m = re.search(r"matrix\([^,]+,[^,]+,[^,]+,[^,]+,([^,]+)", transform)
if m:
x_offset = float(m.group(1))
if x_offset < 50:
return t
return None
def find_subhead_element(root):
"""Locate the subhead tspan by scanning for known subhead text patterns."""
known_subheads = {
"potion", "wondrous item", "oil", "scroll", "salve / poultice",
"medical supply", "ammunition", "consumable", "consumable (apply)",
"consumable (crush)", "consumable (inhale)", "consumable (ingest)",
"consumable (break)", "single-use weapon",
}
for elem in root.iter():
tag = elem.tag.split("}")[-1] if "}" in elem.tag else elem.tag
if tag == "tspan" and elem.text:
t = elem.text.strip().lower()
if t in known_subheads or t.startswith("throwable") or t.startswith("thrown weapon"):
return elem
return None
def rewrite(svg_path, new_mechanics=None, new_subhead=None):
tree = ET.parse(svg_path)
root = tree.getroot()
changes = []
if new_mechanics:
mech_el = find_mechanics_element(root)
if mech_el is None:
print(f"ERROR: No mechanics text element found in {svg_path}", file=sys.stderr)
sys.exit(1)
gc.set_text_content(mech_el, new_mechanics, wrap_width=MECHANICS_WRAP_WIDTH)
changes.append("mechanics")
if new_subhead:
sub_el = find_subhead_element(root)
if sub_el is None:
print(f"ERROR: No subhead element found in {svg_path}", file=sys.stderr)
sys.exit(1)
sub_el.text = new_subhead
changes.append("subhead")
tree.write(svg_path, xml_declaration=True, encoding="unicode")
print(f"OK ({', '.join(changes)}): {svg_path}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Rewrite mechanics/subhead in SVG card")
parser.add_argument("svg", help="Path to SVG file")
parser.add_argument("positional_mechanics", nargs="?", default=None,
help="New mechanics text (positional fallback)")
parser.add_argument("--mechanics", "-m",
help="New mechanics text (with **bold** and *italic* markup)")
parser.add_argument("--subhead", "-s", help="New subhead text")
args = parser.parse_args()
mechanics = args.mechanics or args.positional_mechanics
if not mechanics and not args.subhead:
print("ERROR: Provide --mechanics and/or --subhead", file=sys.stderr)
sys.exit(1)
rewrite(args.svg, mechanics, args.subhead)