This commit is contained in:
Seth Trowbridge 2025-08-22 09:26:18 -04:00
parent 61fac50e20
commit 74980d2c1b
5 changed files with 62 additions and 34 deletions

View File

@ -1,4 +1,4 @@
<!doctype html><html> <!DOCTYPE html><html>
<head> <head>
<style> <style>
.user_styles_UserMadeSheet_Anchor { .user_styles_UserMadeSheet_Anchor {
@ -14,7 +14,7 @@
<body> <body>
<div> <div>
<h1> <h1>
Welcome! Welcome, seth!
</h1> </h1>
<a href="www.site.com" class="user_styles_UserMadeSheet_Anchor"> <a href="www.site.com" class="user_styles_UserMadeSheet_Anchor">
<img src="image.png"/> <img src="image.png"/>

41
pyx.py
View File

@ -1,4 +1,4 @@
from typing import Any, List, Tuple, Union, Optional from typing import List, Tuple, Union, Optional
from css import Chainer, Writer from css import Chainer, Writer
from dataclasses import dataclass from dataclasses import dataclass
@ -35,39 +35,46 @@ class Leaf():
def __call__(self, css:Optional[TAGGED] = None, id:Optional[str] = None): def __call__(self, css:Optional[TAGGED] = None, id:Optional[str] = None):
return self.props(css, id, {}) return self.props(css, id, {})
@classmethod
def template(cls, name:str, attrs:Optional[str] = None):
return f'<{name}{attrs or ""}/>'
def __repr__(self) -> str: def __repr__(self) -> str:
return f'<{self.name}{self.attrs or ""}/>' return __class__.template(self.name, self.attrs)
class Branch(Leaf): class Branch(Leaf):
name:str = ""
def __init__(self, name:Optional[str] = None): def __init__(self, name:str):
if name: self.name = name
self.name = name
self.attrs = None self.attrs = None
@classmethod
def template(cls, name:str, attrs:Optional[str] = None, children:Optional[str] = None):
return f'<{name}{attrs or ""}>\n{children}\n</{name}>'
def __repr__(self) -> str: def __repr__(self) -> str:
return f'<{self.name}{self.attrs or ""}></{self.name}>' return self.__class__.template(self.name, self.attrs)
def __getitem__(self, key:Union[Renderable, Tuple[Renderable, ...]]) -> Renderable: def __getitem__(self, key:Union[Renderable, Tuple[Renderable, ...]]) -> Renderable:
if isinstance(key, tuple): children = f'\n'.join(str(k) for k in key) if isinstance(key, tuple) else str(key)
children = f'\n'.join(str(k) for k in key) return self.__class__.template(self.name, self.attrs, children)
else:
children = str(key)
return f'<{self.name}{self.attrs or ""}>\n{children}\n</{self.name}>'
class IMGTag(Leaf): class IMGTag(Leaf):
name = "img"
def __call__(self, css:Optional[TAGGED] = None, id:Optional[str] = None, src:Optional[str] = None): def __call__(self, css:Optional[TAGGED] = 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"
def __call__(self, css:Optional[TAGGED] = None, id:Optional[str] = None, href:Optional[str] = None, target:Optional[str] = None): def __call__(self, css:Optional[TAGGED] = 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})
class HTMLTag(Branch):
@classmethod
def template(cls, name:str, attrs:Optional[str] = None, children:Optional[str] = None):
return f'<!DOCTYPE html><{name}{attrs or ""}>\n{children}\n</{name}>'
IMG = IMGTag()
A = ATag() IMG = IMGTag("img")
A = ATag("a")
BR = Leaf("br") BR = Leaf("br")
HR = Leaf("hr") HR = Leaf("hr")
DIV = Branch("div") DIV = Branch("div")
@ -80,7 +87,7 @@ H3 = Branch("h3")
H4 = Branch("h4") H4 = Branch("h4")
SECTION = Branch("section") SECTION = Branch("section")
MAIN = Branch("main") MAIN = Branch("main")
HTML = Branch("html") HTML = HTMLTag("html")
HEAD = Branch("head") HEAD = Branch("head")
BODY = Branch("body") BODY = Branch("body")
STYLE = Branch("style") STYLE = Branch("style")

12
route.py Normal file
View File

@ -0,0 +1,12 @@
from pyx import HTML, HEAD, BODY, STYLE, Capture
from user_template import ViewFunction
output = Capture(ViewFunction(name="seth"))
with open("example.html", "w") as file:
file.write(f'{HTML[
HEAD[
STYLE[ output.css ]
],
BODY[ output.html ]
]}')

20
scratch.py Normal file
View File

@ -0,0 +1,20 @@
from typing import Callable, TypeVar, ParamSpec, Optional
P = ParamSpec('P') # captures the parameter types
R = TypeVar('R') # original return type
NewR = TypeVar('NewR') # new return type
def transform_return_type(func: Callable[P, R]) -> Callable[P, str]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> str:
# You'd do something meaningful here
result = func(*args, **kwargs)
return str(result) # Just an example transformation
return wrapper
@transform_return_type
def idk(test:Optional[str] = None)->int:
return 7
test = idk(test="hey")

View File

@ -1,11 +1,9 @@
from pyx import DIV, H1, P, IMG, A, HTML, HEAD, BODY, STYLE, Capture from pyx import DIV, H1, P, IMG, A
from user_styles import UserMadeSheet from user_styles import UserMadeSheet
def ViewFunction(name:str):
output = Capture( return DIV[
DIV H1[f'Welcome, {name}!'],
[
H1["Welcome!"],
A(css=(UserMadeSheet.Anchor), href="www.site.com") A(css=(UserMadeSheet.Anchor), href="www.site.com")
[ [
IMG(src="image.png") IMG(src="image.png")
@ -21,12 +19,3 @@ output = Capture(
deserunt mollit anim id est laborum.""" deserunt mollit anim id est laborum."""
] ]
] ]
)
with open("example.html", "w") as file:
file.write(f'<!doctype html>{HTML[
HEAD[
STYLE[ output.css ]
],
BODY[ output.html ]
]}')