/
lib.py
88 lines (83 loc) · 2.61 KB
/
lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from metapensiero.pj.api import translates
from yattag import Doc, indent
import json
import inspect
class FuncObj(str):
def __new__(cls, func, *args, **kwargs):
name = func.__name__ + '()'
obj = str.__new__(cls, name)
obj.source = translates(
inspect.getsource(func).split('\n', 1)[1],
enable_es6=True)[0]
return obj
def add(self, _doc=None):
if not _doc:
global doc
_doc = doc
_doc.asis(self.source)
def jsfy(klass):
def wrapper(func):
value = FuncObj(func)
klass.local[func.__name__] = value
return value
return wrapper
def domfy(klass):
def wrapper(func):
_doc, _tag, _text, _line = Doc().ttl()
func(_doc, _tag, _text, _line)
value = _doc.getvalue()
klass.var[func.__name__] = value
return value
return wrapper
class Container(str):
var = {}
local = {}
def __new__(cls, *args, **kwargs):
t = str.__new__(cls, *args, **kwargs)
obj = str.__new__(cls, t.__str__())
return obj
def render(self, doc, _, text, line):
pass
def __str__(self):
doc, _, text, line = Doc().ttl()
hold = {}
if len(self.var) + len(self.local) > 0:
with _('script'):
for k, v in self.var.items():
doc.asis('var ' + k + ' = ' + json.dumps(v) + ';')
for k, v in self.local.items():
v.add(doc)
self.render(doc, _, text, line)
result = doc.getvalue()
for k, v in hold.items():
globals()[k] = v
return result
class Html(str):
var = {}
local = {}
def __new__(cls, *args, **kwargs):
t = str.__new__(cls, *args, **kwargs)
obj = str.__new__(cls, t.__str__())
return obj
def header(self, doc, _, text, line):
pass
def body(self, doc, _, text, line):
pass
def __str__(self):
hold = {}
doc, _, text, line = Doc().ttl()
with _('html'):
with _('header'):
if len(self.var) + len(self.local) > 0:
with _('script'):
for k, v in self.var.items():
doc.asis('var ' + k + ' = ' + json.dumps(v) + ';')
for k, v in self.local.items():
v.add(doc)
self.header(doc, _, text, line)
with _('body'):
self.body(doc, _, text, line)
result = doc.getvalue()
for k, v in hold.items():
globals()[k] = v
return result