# -*- coding: utf-8 -*- from collections.abc import Iterable from dataclasses import dataclass, field from typing import Dict, List, Optional class Attribs(Dict): def __repr__(self): if len(self) == 0: return "" html = [] for k, v in self.items(): if v is not None: html.append(f' {k}="{v}"') else: html.append(f" {k}") return "".join(html) @dataclass class Tag: contents: str attribs: Attribs = field(default_factory=Attribs) flat: bool = False @property def tagname(self): return type(self).__name__.lower() def get_contents(self): # import pudb; pudb.set_trace() separator = "" if self.flat else "\n" if isinstance(self.contents, Iterable) and not isinstance(self.contents, str): return separator.join([str(c) for c in self.contents if c is not None]) elif self.contents is None: return "" else: return str(self.contents) def __repr__(self): separator = "" if self.flat else "\n" html = [ f"<{self.tagname}{str(self.attribs)}>", self.get_contents(), f"{self.tagname}>", ] return separator.join(html) @dataclass class TagSingleton(Tag): def __repr__(self): return f"<{self.tagname}{self.attribs} />" @dataclass class Br(TagSingleton): pass class Td(Tag): pass # contents: str = "" # # def __init__(self, contents, *args, **kwargs): # self.contents = contents # super().__init__(*args, **kwargs) # # def __repr__(self): # html = [ # f"