#!/usr/bin/env python3 """Rewrite lore text in an existing SVG card without regenerating the full card. Usage: python3 scripts/rewrite-lore.py "path/to/Card.svg" "New lore text here..." Uses the same format_lore_text() and set_text_content() pipeline as generate-card.py to avoid the tspan space-loss bug (SKILL.md lesson #13). """ import sys import os import re 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) def find_lore_element(root): """Locate the lore text element using the same heuristic as generate-card.py.""" 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 rewrite(svg_path, new_lore): tree = ET.parse(svg_path) root = tree.getroot() lore_el = find_lore_element(root) if lore_el is None: print(f"ERROR: No lore text element found in {svg_path}", file=sys.stderr) sys.exit(1) formatted = gc.format_lore_text(new_lore) gc.set_text_content(lore_el, formatted, wrap_width=42) tree.write(svg_path, xml_declaration=True, encoding="unicode") print(f"OK: {svg_path}") if __name__ == "__main__": if len(sys.argv) < 3: print("Usage: rewrite-lore.py ", file=sys.stderr) sys.exit(1) rewrite(sys.argv[1], sys.argv[2])