Arbitrary keys bug? (#457)

This commit is contained in:
corwinmacmillan
2025-10-22 18:17:11 -04:00
committed by GitHub
parent 21f96a5ed1
commit 24f221827a

View File

@@ -220,7 +220,8 @@ class TypstFile(TemplatedFile):
section_title=section.title,
)
else:
placeholder_value = getattr(entry, placeholder_key, None)
arbitrary_keys = getattr(entry, "model_extra", None)
placeholder_value = arbitrary_keys.get(lowercase_placeholder_key, None) if isinstance(arbitrary_keys, dict) else None
placeholders[placeholder_key] = (
placeholder_value if placeholder_value != "None" else None
@@ -788,11 +789,13 @@ def replace_placeholders_with_actual_values(
The string with actual values.
"""
for placeholder, value in placeholders.items():
if value:
text = text.replace(placeholder, str(value))
# Use regex only for whole-word placeholders like DATE, NAME, etc.
if re.fullmatch(r"\w+", placeholder): # e.g., "DATE", "NAME"
pattern = rf"\b{placeholder}\b"
text = re.sub(pattern, str(value or ""), text)
else:
text = text.replace(placeholder, "")
# Fall back to literal replacement if placeholder is not a word (e.g., "{name}")
text = text.replace(placeholder, str(value or ""))
return text