Merge branch 'main' of https://gitea.hptrow.me/SethTrowbridge/pyx
This commit is contained in:
commit
c163ac2fc1
27
pyx.py
27
pyx.py
@ -1,6 +1,10 @@
|
|||||||
from typing import List, Tuple, Union
|
from typing import List, Tuple, Union, Optional
|
||||||
|
|
||||||
def MakeAttrs(css:str|Tuple[str, ...]|None, id:str|None, attributes: dict[str, str | None]|None) -> str:
|
Attributes = dict[str, Union[str, bool, None]]
|
||||||
|
Stringy = Union[str, Tuple[str, ...]]
|
||||||
|
Renderable = Union[str, 'Branch', 'Leaf']
|
||||||
|
|
||||||
|
def MakeAttrs(css:Optional[Stringy], id:Optional[str], attributes: Attributes) -> str:
|
||||||
if not attributes:
|
if not attributes:
|
||||||
attributes = {}
|
attributes = {}
|
||||||
if css:
|
if css:
|
||||||
@ -15,17 +19,17 @@ def MakeAttrs(css:str|Tuple[str, ...]|None, id:str|None, attributes: dict[str, s
|
|||||||
|
|
||||||
class Leaf():
|
class Leaf():
|
||||||
name:str = ""
|
name:str = ""
|
||||||
def __init__(self, name:str|None = None):
|
def __init__(self, name:Optional[str] = None):
|
||||||
if name:
|
if name:
|
||||||
self.name = name
|
self.name = name
|
||||||
self.attrs = None
|
self.attrs = None
|
||||||
|
|
||||||
def props(self, css:str|Tuple[str, ...]|None, id:str|None, attributes: dict[str, str | None]|None):
|
def props(self, css:Optional[Stringy], id:Optional[str], attributes: Attributes):
|
||||||
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
|
||||||
|
|
||||||
def __call__(self, css:str|None = None, id:str|None = None):
|
def __call__(self, css:Optional[str] = None, id:Optional[str] = None):
|
||||||
return self.props(css, id, {})
|
return self.props(css, id, {})
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
@ -33,29 +37,30 @@ class Leaf():
|
|||||||
|
|
||||||
class Branch(Leaf):
|
class Branch(Leaf):
|
||||||
name:str = ""
|
name:str = ""
|
||||||
def __init__(self, name:str|None = None):
|
|
||||||
|
def __init__(self, name:Optional[str] = None):
|
||||||
if name:
|
if name:
|
||||||
self.name = name
|
self.name = name
|
||||||
self.attrs = None
|
self.attrs = None
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f'<{self.name}{self.attrs or ""}></{self.name}>'
|
return f'<{self.name}{self.attrs or ""}></{self.name}>'
|
||||||
def __getitem__(self, key:Union[str, 'Branch', Leaf, Tuple[Union[str, 'Branch', Leaf], ...]]) -> str:
|
|
||||||
|
def __getitem__(self, key:Union[Renderable, Tuple[Renderable, ...]]) -> str:
|
||||||
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 = str(key)
|
children = str(key)
|
||||||
|
|
||||||
return f'<{self.name}{self.attrs or ""}>\n{children}\n</{self.name}>'
|
return f'<{self.name}{self.attrs or ""}>\n{children}\n</{self.name}>'
|
||||||
|
|
||||||
class IMGTag(Leaf):
|
class IMGTag(Leaf):
|
||||||
name = "img"
|
name = "img"
|
||||||
def __call__(self, css:str|None = None, id:str|None = None, src:str|None = None):
|
def __call__(self, css:Optional[Stringy] = None, id:Optional[str] = None, src:Optional[str] = None):
|
||||||
return self.props(css, id, {"src":src})
|
return self.props(css, id, {"src":src})
|
||||||
|
|
||||||
class ATag(Branch):
|
class ATag(Branch):
|
||||||
name = "a"
|
name = "a"
|
||||||
def __call__(self, css:str|None = None, id:str|None = None, href:str|None = None, target:str|None = None):
|
def __call__(self, css:Optional[Stringy] = None, id:Optional[str] = None, href:Optional[str] = None, target:Optional[str] = None):
|
||||||
return self.props(css, id, {"href":href, "target":target})
|
return self.props(css, id, {"href":href, "target":target})
|
||||||
|
|
||||||
IMG = IMGTag()
|
IMG = IMGTag()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from PYX import DIV, H1, H2, P, SPAN, BR, IMG, A
|
from pyx import DIV, H1, H2, P, SPAN, BR, IMG, A
|
||||||
|
|
||||||
print(
|
print(
|
||||||
DIV
|
DIV
|
||||||
|
Loading…
Reference in New Issue
Block a user