nice
This commit is contained in:
parent
0d3fae94bd
commit
dd440fa779
86
css.py
86
css.py
@ -2,23 +2,6 @@ from dataclasses import dataclass
|
|||||||
from typing import Optional, Tuple, Union, Dict, List
|
from typing import Optional, Tuple, Union, Dict, List
|
||||||
import random
|
import random
|
||||||
|
|
||||||
css_final:List[str] = []
|
|
||||||
css_lines:List[str] = []
|
|
||||||
|
|
||||||
def css_class(css_context:str):
|
|
||||||
global css_lines
|
|
||||||
output = f'{css_context}{{ {"".join(css_lines)} }}'
|
|
||||||
css_lines = []
|
|
||||||
css_final.append(output)
|
|
||||||
|
|
||||||
def css_write(str:str):
|
|
||||||
css_lines.append(str)
|
|
||||||
|
|
||||||
def css_dump():
|
|
||||||
global css_final
|
|
||||||
output = "".join(css_final)
|
|
||||||
css_final = []
|
|
||||||
return output
|
|
||||||
class Unit():
|
class Unit():
|
||||||
unit = "px"
|
unit = "px"
|
||||||
def __init__(self, amount:float):
|
def __init__(self, amount:float):
|
||||||
@ -45,7 +28,6 @@ class Cluster():
|
|||||||
pass
|
pass
|
||||||
def __repr__(self)->str:
|
def __repr__(self)->str:
|
||||||
print("repr called on cluster")
|
print("repr called on cluster")
|
||||||
#todo: flesh this out / provide overrides for each inheriting class
|
|
||||||
return self.render()
|
return self.render()
|
||||||
|
|
||||||
def render(self)->str:
|
def render(self)->str:
|
||||||
@ -54,7 +36,7 @@ class Cluster():
|
|||||||
value = self.__getattribute__(self.map[key])
|
value = self.__getattribute__(self.map[key])
|
||||||
if value:
|
if value:
|
||||||
output.append(f'{key}:{value};')
|
output.append(f'{key}:{value};')
|
||||||
return "\n".join(output)
|
return "".join(output)
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Font(Cluster):
|
class Font(Cluster):
|
||||||
@ -83,40 +65,80 @@ class XForm(Space):
|
|||||||
class Chainer():
|
class Chainer():
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.accessed = 0
|
self.size = 0
|
||||||
|
self.dump_id:int = -1
|
||||||
|
self.log:List[str] = []
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __getitem__(self, args:Union[Cluster, Tuple[Cluster, ...]]):
|
def __getitem__(self, args:Union[Cluster, Tuple[Cluster, ...]]):
|
||||||
if isinstance(args, tuple):
|
print("bracket access", args)
|
||||||
children = f'\n'.join(k.render() for k in args)
|
|
||||||
|
styledata = "".join(c.render() for c in args) if isinstance(args, tuple) else args.render()
|
||||||
|
if self.size:
|
||||||
|
self.log.append(f'\n@media(min-width:{self.size}px){{ {styledata} }}')
|
||||||
else:
|
else:
|
||||||
children = args.render()
|
self.log.append(styledata)
|
||||||
if self.accessed:
|
|
||||||
css_write(f'@media(max-width:{self.accessed}px){{ {children} }}')
|
|
||||||
else:
|
|
||||||
css_write(children)
|
|
||||||
self.accessed = 0
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __call__(self, key:int):
|
def __call__(self, key:int):
|
||||||
self.accessed = key
|
print("call access", key)
|
||||||
|
self.size = key
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def dump(self, log:List[str], fullName:str, dump_id:int):
|
||||||
|
if self.dump_id != dump_id:
|
||||||
|
log.append(f'.{fullName} {{\n {"".join(self.log)} \n}}')
|
||||||
|
self.dump_id = dump_id
|
||||||
|
self.log = []
|
||||||
|
self.size = 0
|
||||||
|
|
||||||
CSS = Chainer()
|
class Kickoff():
|
||||||
|
def __getitem__(self, args:Union[Cluster, Tuple[Cluster, ...]]):
|
||||||
|
return Chainer().__getitem__(args)
|
||||||
|
|
||||||
|
def __call__(self, key:int):
|
||||||
|
return Chainer().__call__(key)
|
||||||
|
|
||||||
|
CSS = Kickoff()
|
||||||
|
|
||||||
|
def TAG(*styles:Chainer):
|
||||||
|
for item in styles:
|
||||||
|
print(item)
|
||||||
|
|
||||||
##########################
|
##########################
|
||||||
|
|
||||||
|
class Writer:
|
||||||
|
dump_id = 0
|
||||||
|
log:List[str] = []
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def start(cls):
|
||||||
|
cls.dump_id += 1
|
||||||
|
cls.log.clear()
|
||||||
|
print("Writer reset, dump_id:", cls.dump_id)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def capture(cls, fullName:str, chainer:Chainer):
|
||||||
|
chainer.dump(cls.log, fullName, Writer.dump_id)
|
||||||
|
|
||||||
class _SHEET(type):
|
class _SHEET(type):
|
||||||
last_accessed:Optional[str] = None
|
|
||||||
def __getattribute__(cls, name:str):
|
def __getattribute__(cls, name:str):
|
||||||
if name != "__name__" and name != "__module__":
|
if name != "__name__" and name != "__module__":
|
||||||
css_class(f'.{cls.__module__}.{cls.__name__}.{name}')
|
print("class name access", name)
|
||||||
|
value = super().__getattribute__(name)
|
||||||
|
if isinstance(value, Chainer):
|
||||||
|
fullName = f'{cls.__module__}_{cls.__name__}_{name}'
|
||||||
|
Writer.capture(fullName, value)
|
||||||
|
return fullName
|
||||||
|
|
||||||
return super().__getattribute__(name)
|
return super().__getattribute__(name)
|
||||||
|
|
||||||
class SHEET(metaclass=_SHEET):
|
class SHEET(metaclass=_SHEET):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class UserMadeSheet(SHEET):
|
class UserMadeSheet(SHEET):
|
||||||
|
|
||||||
Paragraph=CSS[
|
Paragraph=CSS[
|
||||||
Font( size=PX(10) ),
|
Font( size=PX(10) ),
|
||||||
](512)[
|
](512)[
|
||||||
|
21
scratch.py
21
scratch.py
@ -1,21 +0,0 @@
|
|||||||
|
|
||||||
from dataclasses import dataclass, field
|
|
||||||
from typing import Optional, Dict, Tuple, List
|
|
||||||
|
|
||||||
|
|
||||||
class Bracket():
|
|
||||||
def __init__(self, *args: str):
|
|
||||||
self.children = list(args)
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class StyledTag(Bracket):
|
|
||||||
style: Optional[str] = None
|
|
||||||
other: Optional[str] = None
|
|
||||||
|
|
||||||
|
|
||||||
def Test(*children:str, css:Optional[str]=None, ):
|
|
||||||
print(css)
|
|
||||||
print(children)
|
|
||||||
|
|
||||||
|
|
||||||
Test("itme1", "item2", css="yo")
|
|
8
sheet.py
8
sheet.py
@ -1,3 +1,5 @@
|
|||||||
from css import UserMadeSheet, css_dump
|
from css import UserMadeSheet, TAG, Writer
|
||||||
print(UserMadeSheet.Paragraph, UserMadeSheet.Anchor)
|
|
||||||
print(css_dump())
|
print(TAG(UserMadeSheet.Paragraph))
|
||||||
|
|
||||||
|
print(Writer.log)
|
||||||
|
@ -3,12 +3,8 @@ from pyx import DIV, H1, H2, P, SPAN, BR, IMG, A
|
|||||||
print(
|
print(
|
||||||
DIV
|
DIV
|
||||||
[
|
[
|
||||||
H1("Welcome!"),
|
H1["Welcome!"],
|
||||||
P(),
|
A(css="www.site.com", href="www.site.com")
|
||||||
IMG(id="header.png"),
|
|
||||||
A(css="test"),
|
|
||||||
A["other?"],
|
|
||||||
A(css="www.site.com")
|
|
||||||
[
|
[
|
||||||
IMG(id="image.png")
|
IMG(id="image.png")
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user