import os import re import glob files = glob.glob('templates/*/detail.html') for filepath in files: with open(filepath, 'r') as f: content = f.read() # Find the entity name from the default_fields check entity_match = re.search(r'{% elif ([a-z_]+)\.default_fields is not none %}', content) if not entity_match: continue entity = entity_match.group(1) # 1. Update tags tag_search = r"{% elif " + entity + r"\.default_fields is not none %}\s*{% if 'special::tags' in " + entity + r"\.default_fields %}checked{% endif %}\s*{% else %}\s*checked\s*{% endif %}>\s*" tag_replace = r"{% elif " + entity + r".default_fields is not none %}\n {% if 'special::tags' in " + entity + r".default_fields %}checked{% endif %}\n {% endif %}>\n " content = re.sub(tag_search, tag_replace, content) # 2. Update dt/dd blocks def dt_replacer(match): prefix_quote = match.group(1) # e.g. "wardrobe::" or "{{ section }}::" if '{{ section }}' in prefix_quote: prefix_logic = "(section ~ '::' ~ key)" else: p = prefix_quote.replace('::', '') prefix_logic = "('" + p + "::' ~ key)" res = f"{{% set is_default = {entity}.default_fields is not none and {prefix_logic} in {entity}.default_fields %}}\n" res += f"