From 25fe93efe558cbfec74558afc4b08bb8c6702c94 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Thu, 14 Aug 2025 15:42:02 -0400 Subject: [PATCH] backet syntax! --- py_jsx.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/py_jsx.py b/py_jsx.py index a09246d..3729834 100644 --- a/py_jsx.py +++ b/py_jsx.py @@ -6,6 +6,18 @@ class StrArgsToStr(Protocol): ClassArg = str|List[str]|Tuple[str, ...]|None +class ProxyDict(dict): + + name:str + attrs:str + + def __init__(self, name:str, attrs:str): + self.name = name + self.attrs = attrs + def __getitem__(self, key:str|Tuple[str, ...]): + return f'<{self.name}{self.attrs}>{"".join(key) if isinstance(key, tuple) else key}' + + def MakeAttrs(classes:ClassArg, id:str|None, attributes: dict[str, str | None]) -> str: if classes: @@ -20,10 +32,8 @@ def MakeAttrs(classes:ClassArg, id:str|None, attributes: dict[str, str | None]) attrList.append(f'{key}="{value}"') return " ".join(attrList) -def ElWrapped(name: str, classes:ClassArg = None, id:str|None = None, attrs: dict[str, str | None] = {}, children:ClassArg = ()) -> StrArgsToStr: - def Wrapped(*children:str): - return f'<{name}{MakeAttrs(classes, id, attrs)}>{" ".join(children)}' - return Wrapped +def ElWrapped(name: str, classes:ClassArg = None, id:str|None = None, attrs: dict[str, str | None] = {}, children:ClassArg = ()) -> ProxyDict: + return ProxyDict(name, MakeAttrs(classes, id, attrs)) def ElInline(name: str, classes:ClassArg = None, id:str|None = None, attrs: dict[str, str | None] = {}) -> str: return f'<{name} {MakeAttrs(classes, id, attrs)}/>' @@ -38,7 +48,7 @@ def SimpleInline(name:str): return ElInline(name, classes, id) return proxy -def A(classes:ClassArg = None, id:str|None = None, href:str|None = None, target:str|None = None) -> StrArgsToStr: +def A(classes:ClassArg = None, id:str|None = None, href:str|None = None, target:str|None = None): return ElWrapped("a", classes, id, {"href":href, "target":id}) def IMG(classes:ClassArg = None, id:str|None = None, src:str|None = None): @@ -53,7 +63,7 @@ SPAN = SimpleWrapped("span") print( DIV(classes="outer") - ( - A(classes="CTA Orange", href="www.link")("click") - ) + [ + A(classes="CTA Orange", href="www.link")["click"] + ] ) \ No newline at end of file