class extensions started

This commit is contained in:
Seth Trowbridge 2025-08-15 14:37:35 -04:00
parent 9e0af0b509
commit b68b443071

View File

@ -1,6 +1,4 @@
from typing import List, Protocol, Tuple, Union from typing import List, Tuple, Union
render_depth:int = 0
def MakeAttrs(css:str|Tuple[str, ...]|None, id:str|None, attributes: dict[str, str | None]|None) -> str: def MakeAttrs(css:str|Tuple[str, ...]|None, id:str|None, attributes: dict[str, str | None]|None) -> str:
if not attributes: if not attributes:
@ -13,16 +11,17 @@ def MakeAttrs(css:str|Tuple[str, ...]|None, id:str|None, attributes: dict[str, s
for key, value in attributes.items(): for key, value in attributes.items():
if value: if value:
attrList.append(f'{key}="{value}"') attrList.append(f'{key}="{value}"')
return " ".join(attrList) return " " + " ".join(attrList)
class ComplexProxyLeaf(dict): class ComplexProxyLeaf():
name:str = "" name:str = ""
attrs:str = "" def __init__(self, name:str|None = None):
def __init__(self, name:str): if name:
self.name = name self.name = name
self.attrs = None
def props(self, css:str|Tuple[str, ...]|None, id:str|None, attributes: dict[str, str | None]|None): def props(self, css:str|Tuple[str, ...]|None, id:str|None, attributes: dict[str, str | None]|None):
clone = clone = self.__class__(self.name) clone = self.__class__(self.name)
clone.attrs = MakeAttrs(css, id, attributes) clone.attrs = MakeAttrs(css, id, attributes)
return clone return clone
@ -30,37 +29,42 @@ class ComplexProxyLeaf(dict):
return self.props(css, id, {}) return self.props(css, id, {})
def __repr__(self) -> str: def __repr__(self) -> str:
return f'<{self.name} {self.attrs}/>' return f'<{self.name}{self.attrs or ""}/>'
class ComplexProxyBranch(ComplexProxyLeaf): class ComplexProxyBranch(ComplexProxyLeaf):
name:str = "" name:str = ""
attrs:str = "" def __init__(self, name:str|None = None):
def __init__(self, name:str): if name:
self.name = name self.name = name
self.attrs = None
def __call__(self, css:str|None = None, id:str|None = None):
return self.props(css, id, None)
def __repr__(self) -> str: def __repr__(self) -> str:
return f'<{self.name} {self.attrs}></{self.name}>' return f'<{self.name}{self.attrs or ""}></{self.name}>'
def __getitem__(self, key:Union[str, 'ComplexProxyBranch', ComplexProxyLeaf, Tuple[Union[str, 'ComplexProxyBranch', ComplexProxyLeaf], ...]]) -> str: def __getitem__(self, key:Union[str, 'ComplexProxyBranch', ComplexProxyLeaf, Tuple[Union[str, 'ComplexProxyBranch', ComplexProxyLeaf], ...]]) -> str:
global render_depth
render_depth = render_depth + 1
if isinstance(key, tuple): if isinstance(key, tuple):
children = f'\n'.join(str(k) for k in key) children = f'\n'.join(str(k) for k in key)
else: else:
children = key children = str(key)
render_depth = render_depth - 1 return f'<{self.name}{self.attrs or ""}>\n{children}\n</{self.name}>'
return f'<{self.name} {self.attrs}>\n{children}\n</{self.name}>' class IMGTag(ComplexProxyLeaf):
name = "img"
def __call__(self, css:str|None = None, id:str|None = None, src:str|None = None):
return self.props(css, id, {"src":src})
class ATag(ComplexProxyBranch):
name = "a"
def __call__(self, css:str|None = None, id:str|None = None, href:str|None = None, target:str|None = None):
return self.props(css, id, {"href":href, "target":target})
IMG = ComplexProxyLeaf("img") IMG = IMGTag()
A = ATag()
BR = ComplexProxyLeaf("br") BR = ComplexProxyLeaf("br")
A = ComplexProxyBranch("a") DIV = ComplexProxyBranch("div")
print( print(
A DIV
[ [
"click here", "click here",
BR, BR,