Compare commits

..

No commits in common. "main" and "css-capture-rework" have entirely different histories.

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

View File

@ -1,12 +0,0 @@
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 ]
]}')

View File

@ -1,20 +0,0 @@
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,9 +1,11 @@
from pyx import DIV, H1, P, IMG, A from pyx import DIV, H1, P, IMG, A, HTML, HEAD, BODY, STYLE, Capture
from user_styles import UserMadeSheet from user_styles import UserMadeSheet
def ViewFunction(name:str):
return DIV[ output = Capture(
H1[f'Welcome, {name}!'], DIV
[
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")
@ -19,3 +21,12 @@ def ViewFunction(name:str):
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 ]
]}')