from dataclasses import dataclass from typing import TYPE_CHECKING, Iterable from elements import PuppygirlElement, SlotName, TemplateAttr if TYPE_CHECKING: from elements import Element, TemplateInstance from protocols import Clonable from puppytype import RenderableElement, TemplateDict @dataclass class PuppygirlTag(PuppygirlElement): name: str = "puppygirl-tag" def apply_template(self, element: "Element", template: "Clonable[TemplateInstance]") -> Iterable["Element"]: instance = template.clone() for content in element.find_all(recursive=False): instance.insert_content(content) del content[SlotName] instance.remove_slots() return instance.value def render(self, element: "Element", templates: "TemplateDict") -> "RenderableElement": if not element.has_attr(TemplateAttr): return element template = templates.get(element[TemplateAttr]) return self.apply_template(element, template)