transcript 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- transcript/__init__.py +112 -0
- transcript/builders/__init__.py +1 -0
- transcript/builders/html/__init__.py +6 -0
- transcript/builders/html/__init__.pyi +796 -0
- transcript/builders/html/args.py +25 -0
- transcript/builders/html/attrs.py +189 -0
- transcript/builders/html/html.py +81 -0
- transcript/builders/jinja.py +228 -0
- transcript/builders/js.py +280 -0
- transcript/builders/scss.py +393 -0
- transcript/builders/xml.py +166 -0
- transcript/core/__init__.py +14 -0
- transcript/core/ai.py +116 -0
- transcript/core/config.py +29 -0
- transcript/core/container.py +120 -0
- transcript/core/factory.py +180 -0
- transcript/core/fingerprint.py +265 -0
- transcript/core/meta.py +41 -0
- transcript/core/registers.py +53 -0
- transcript/core/stubs.py +19 -0
- transcript/io/__init__.py +12 -0
- transcript/io/file.py +40 -0
- transcript/io/hub.py +191 -0
- transcript/io/json_data.py +185 -0
- transcript/io/load.py +154 -0
- transcript/io/renderer.py +49 -0
- transcript/presets/__init__.py +63 -0
- transcript/presets/forms.py +108 -0
- transcript/presets/html.py +247 -0
- transcript/presets/jinja.py +119 -0
- transcript/presets/scss.py +221 -0
- transcript/presets/tailwind.py +392 -0
- transcript/project.py +223 -0
- transcript/purge.py +171 -0
- transcript/py.typed +0 -0
- transcript-0.4.0.dist-info/METADATA +201 -0
- transcript-0.4.0.dist-info/RECORD +40 -0
- transcript-0.4.0.dist-info/WHEEL +5 -0
- transcript-0.4.0.dist-info/licenses/LICENSE +21 -0
- transcript-0.4.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from transcript.builders.xml import XMLArg, normalize_attr_name
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class HTMLArg(XMLArg):
|
|
5
|
+
"""Atrybut znacznika HTML zgodny z zapisem XML/HTML.
|
|
6
|
+
|
|
7
|
+
Przykłady::
|
|
8
|
+
|
|
9
|
+
HTMLArg('id', 'hero')
|
|
10
|
+
HTMLArg('class', 'btn', 'btn-primary')
|
|
11
|
+
HTMLArg('disabled')
|
|
12
|
+
HTMLArg(class_='card') # nie — użyj nazwy jako pierwszego argumentu
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
@classmethod
|
|
16
|
+
def classes(cls, *names: str) -> 'HTMLArg':
|
|
17
|
+
return cls('class', *names)
|
|
18
|
+
|
|
19
|
+
@classmethod
|
|
20
|
+
def data(cls, key: str, value: str) -> 'HTMLArg':
|
|
21
|
+
name = key if key.startswith('data-') else f'data-{key}'
|
|
22
|
+
return cls(name, value)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
__all__ = ['HTMLArg', 'normalize_attr_name']
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
from transcript.builders.html.args import HTMLArg
|
|
4
|
+
|
|
5
|
+
HTMLAttr = HTMLArg
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
HTML_ATTR_EXCEPTIONS = {
|
|
9
|
+
"HTMLAttrAcceptCharset": "accept-charset",
|
|
10
|
+
"HTMLAttrHttpEquiv": "http-equiv",
|
|
11
|
+
"HTMLAttrCrossOrigin": "crossorigin", # niby camel, ale w HTML zapisane razem
|
|
12
|
+
"HTMLAttrReferrerPolicy": "referrerpolicy",
|
|
13
|
+
"HTMLAttrAutoComplete": "autocomplete",
|
|
14
|
+
"HTMLAttrFormAction": "formaction",
|
|
15
|
+
"HTMLAttrFormEncType": "formenctype",
|
|
16
|
+
"HTMLAttrFormMethod": "formmethod",
|
|
17
|
+
"HTMLAttrFormNoValidate": "formnovalidate",
|
|
18
|
+
"HTMLAttrFormTarget": "formtarget",
|
|
19
|
+
"HTMLAttrImgSrcSet": "srcset",
|
|
20
|
+
"HTMLAttrImgSizes": "sizes", # obrazki <img>
|
|
21
|
+
"HTMLAttrInputMode": "inputmode",
|
|
22
|
+
"HTMLAttrMinLength": "minlength",
|
|
23
|
+
"HTMLAttrMaxLength": "maxlength",
|
|
24
|
+
"HTMLAttrNoValidate": "novalidate",
|
|
25
|
+
"HTMLAttrReadOnly": "readonly",
|
|
26
|
+
"HTMLAttrRowSpan": "rowspan",
|
|
27
|
+
"HTMLAttrColSpan": "colspan",
|
|
28
|
+
"HTMLAttrUseMap": "usemap",
|
|
29
|
+
"HTMLAttrContentEditable": "contenteditable",
|
|
30
|
+
"HTMLAttrTabIndex": "tabindex",
|
|
31
|
+
"HTMLAttrSpellCheck": "spellcheck",
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class HTMLAttrBase(ABC):
|
|
36
|
+
|
|
37
|
+
name:str
|
|
38
|
+
|
|
39
|
+
def __init__(self, value:str|None = None):
|
|
40
|
+
cls_name = self.__class__.__name__
|
|
41
|
+
if cls_name in HTML_ATTR_EXCEPTIONS:
|
|
42
|
+
self.name = HTML_ATTR_EXCEPTIONS[cls_name]
|
|
43
|
+
else:
|
|
44
|
+
self.name = cls_name[8:].lower()
|
|
45
|
+
self.value:str|None = value
|
|
46
|
+
|
|
47
|
+
def __bool__(self):
|
|
48
|
+
return True if self.value is not None else False
|
|
49
|
+
|
|
50
|
+
@abstractmethod
|
|
51
|
+
def _valid(self):
|
|
52
|
+
pass
|
|
53
|
+
|
|
54
|
+
def build(self):
|
|
55
|
+
return f'{self.name}=\"{self.value}\"'
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
ATTRS_LIST = {
|
|
59
|
+
'accept': 'form, input',
|
|
60
|
+
'accept-charset': 'form',
|
|
61
|
+
'accesskey': 'global',
|
|
62
|
+
'action': 'form',
|
|
63
|
+
'allow': 'iframe',
|
|
64
|
+
'alpha': 'input',
|
|
65
|
+
'alt': 'area, img, input',
|
|
66
|
+
'as': 'link',
|
|
67
|
+
'async': 'script',
|
|
68
|
+
'autocapitalize': 'global',
|
|
69
|
+
'autocomplete': 'form, input, select, textarea',
|
|
70
|
+
'autoplay': 'audio, video',
|
|
71
|
+
'background': 'body, table, td, th', # lepiej użyć css background-image
|
|
72
|
+
'bgcolor': 'body, col, colgroup, table, tbody, tfoot, td, th, tr', # lepiej użyć css background-color
|
|
73
|
+
'border': 'img, object, table', # lepiej użyć css border
|
|
74
|
+
'capture': 'input',
|
|
75
|
+
'charset': 'meta',
|
|
76
|
+
'checked': 'input',
|
|
77
|
+
'cite': 'blockquote, del, ins, q',
|
|
78
|
+
'class': 'global',
|
|
79
|
+
'color': 'font, hr', # lepiej użyć css color
|
|
80
|
+
'colorspace': 'input',
|
|
81
|
+
'cols': 'textarea',
|
|
82
|
+
'colspan': 'td, th',
|
|
83
|
+
'content': 'meta',
|
|
84
|
+
'contenteditable': 'global',
|
|
85
|
+
'controls': 'audio, video',
|
|
86
|
+
'coords': 'area',
|
|
87
|
+
'crossorigin': 'audio, img, link, script, video',
|
|
88
|
+
'csp': 'iframe', # eksperymentalny, działa tylko w chrome i edge
|
|
89
|
+
'data': 'object',
|
|
90
|
+
'data-*': 'global',
|
|
91
|
+
'datetime': 'del, ins, time',
|
|
92
|
+
'decoding': 'img',
|
|
93
|
+
'default': 'track',
|
|
94
|
+
'defer': 'script',
|
|
95
|
+
'dir': 'global',
|
|
96
|
+
'dirname': 'input, textarea',
|
|
97
|
+
'disabled': 'button, fieldset, input, optgroup, option, select, textarea',
|
|
98
|
+
'download': 'a, area',
|
|
99
|
+
'draggable': 'global',
|
|
100
|
+
'enctype': 'form',
|
|
101
|
+
'enterkeyhint': 'textarea, contenteditable',
|
|
102
|
+
'elementtiming': 'img, image, inside svg, video, p, elements which have a background-image',
|
|
103
|
+
'for': 'label, output',
|
|
104
|
+
'form': 'button, fieldset, input, object, output, select, textarea',
|
|
105
|
+
'formaction': 'input, button',
|
|
106
|
+
'formenctype': 'button, input',
|
|
107
|
+
'formmethod': 'button, input',
|
|
108
|
+
'formnovalidate': 'button, input',
|
|
109
|
+
'formtarget': 'button, input',
|
|
110
|
+
'headers': 'td, th',
|
|
111
|
+
'height': 'canvas, embed, iframe, img, input, object, video', # liepiej użyć css height
|
|
112
|
+
'hidden': 'global',
|
|
113
|
+
'high': 'meter',
|
|
114
|
+
'href': 'a, area, base, link',
|
|
115
|
+
'hreflang': 'a, link',
|
|
116
|
+
'http-equiv': 'meta',
|
|
117
|
+
'id': 'global',
|
|
118
|
+
'integrity': 'link, script',
|
|
119
|
+
'inputmode': 'textarea, contenteditable',
|
|
120
|
+
'ismap': 'img',
|
|
121
|
+
'itemprop': 'global',
|
|
122
|
+
'kind': 'track',
|
|
123
|
+
'label': 'optgroup, option, track',
|
|
124
|
+
'lang': 'global',
|
|
125
|
+
'language': 'script',
|
|
126
|
+
'loading': 'img, iframe',
|
|
127
|
+
'list': 'input',
|
|
128
|
+
'loop': 'audio, video',
|
|
129
|
+
'low': 'meter',
|
|
130
|
+
'max': 'input, meter, progress',
|
|
131
|
+
'maxlength': 'input, textarea',
|
|
132
|
+
'minlength': 'input, textarea',
|
|
133
|
+
'media': 'a, area, link, source, style',
|
|
134
|
+
'method': 'form',
|
|
135
|
+
'min': 'input, meter',
|
|
136
|
+
'multiple': 'input, select',
|
|
137
|
+
'muted': 'audio, video',
|
|
138
|
+
'name': 'button, form, fieldset, iframe, input, object, output, select, textarea, map, meta',
|
|
139
|
+
'novalidate': 'form',
|
|
140
|
+
'open': 'details, dialog',
|
|
141
|
+
'optimum': 'meter',
|
|
142
|
+
'pattern': 'input',
|
|
143
|
+
'ping': 'a, area',
|
|
144
|
+
'placeholder': 'input, textarea',
|
|
145
|
+
'playsinlane': 'video',
|
|
146
|
+
'poster': 'video',
|
|
147
|
+
'preload': 'audio, video',
|
|
148
|
+
'readonly': 'input, textarea',
|
|
149
|
+
'referrerpolicy': 'a, area, iframe, img, link, script',
|
|
150
|
+
'rel': 'a, area, link',
|
|
151
|
+
'required': 'input, select, textarea',
|
|
152
|
+
'reversed': 'ol',
|
|
153
|
+
'role': 'global',
|
|
154
|
+
'rows': 'textarea',
|
|
155
|
+
'rowspan': 'td, th',
|
|
156
|
+
'sandbox': 'iframe',
|
|
157
|
+
'scope': 'th',
|
|
158
|
+
'selected': 'option',
|
|
159
|
+
'shape': 'a, area',
|
|
160
|
+
'size': 'input, select',
|
|
161
|
+
'sizes': 'link, img, source',
|
|
162
|
+
'slot': 'global',
|
|
163
|
+
'span': 'col, colgroup',
|
|
164
|
+
'spellcheck': 'global',
|
|
165
|
+
'src': 'audio, video, embed, iframe, img, input, script, source, track',
|
|
166
|
+
'srcdoc': 'iframe',
|
|
167
|
+
'srclang': 'track',
|
|
168
|
+
'srcset': 'image, source',
|
|
169
|
+
'start': 'ol',
|
|
170
|
+
'step': 'input',
|
|
171
|
+
'style': 'global',
|
|
172
|
+
'tabindex': 'global',
|
|
173
|
+
'target': 'a, area, base, form',
|
|
174
|
+
'title': 'global',
|
|
175
|
+
'translate': 'global',
|
|
176
|
+
'type': 'button, input, embed, object, ol, script, source, style, menu, link',
|
|
177
|
+
'usemap': 'img, input, object',
|
|
178
|
+
'value': 'button, data, input, li, meter, option, progress',
|
|
179
|
+
'width': 'canvas, embed, iframe, img, input, object, video', # lepiej użyć css width
|
|
180
|
+
'wrap': 'textarea'
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
# test = HTMLAttrBase()
|
|
185
|
+
|
|
186
|
+
# if test:
|
|
187
|
+
# print('True')
|
|
188
|
+
# else:
|
|
189
|
+
# print('False')
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Dict
|
|
3
|
+
|
|
4
|
+
from transcript.builders.html.args import HTMLArg
|
|
5
|
+
from transcript.builders.xml import XMLTagBase
|
|
6
|
+
from transcript.core.container import ContainerBase
|
|
7
|
+
from transcript.core.factory import ContainerConfig, ContainerFactory
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class HTMLTagBase(XMLTagBase):
|
|
11
|
+
|
|
12
|
+
self_closing: bool = False
|
|
13
|
+
cl: str = ' /'
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Doctype(ContainerBase):
|
|
17
|
+
|
|
18
|
+
name: str = 'doctype'
|
|
19
|
+
|
|
20
|
+
def build(self, depth: int = 0) -> str:
|
|
21
|
+
return f'{self._indent(depth)}<!DOCTYPE html>'
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Comment(ContainerBase):
|
|
25
|
+
|
|
26
|
+
name: str = 'comment'
|
|
27
|
+
|
|
28
|
+
def __init__(self, text: str = '', *contents):
|
|
29
|
+
super().__init__(*contents)
|
|
30
|
+
if text:
|
|
31
|
+
self.content.insert(0, text)
|
|
32
|
+
|
|
33
|
+
def build(self, depth: int = 0) -> str:
|
|
34
|
+
inner = ' '.join(str(item) if not isinstance(item, ContainerBase) else item.build() for item in self.content)
|
|
35
|
+
return f'{self._indent(depth)}<!-- {inner} -->'
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
TAGS_LIST = [
|
|
39
|
+
'a', 'abbr', 'address', 'area', 'article', 'aside', 'audio',
|
|
40
|
+
'b', 'base', 'bdi', 'bdo', 'blockquote', 'body', 'br', 'button',
|
|
41
|
+
'canvas', 'caption', 'cite', 'code', 'col', 'colgroup',
|
|
42
|
+
'data', 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog', 'div', 'dl', 'dt',
|
|
43
|
+
'em', 'embed', 'fieldset', 'figcaption', 'figure', 'footer', 'form',
|
|
44
|
+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html',
|
|
45
|
+
'i', 'iframe', 'img', 'input', 'ins',
|
|
46
|
+
'kbd', 'label', 'legend', 'li', 'link', 'main', 'map', 'mark', 'menu', 'meta', 'meter',
|
|
47
|
+
'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output',
|
|
48
|
+
'p', 'param', 'picture', 'pre', 'progress',
|
|
49
|
+
'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'search', 'section', 'select', 'slot',
|
|
50
|
+
'small', 'source', 'span', 'strong', 'style', 'sub', 'summary', 'sup',
|
|
51
|
+
'table', 'tbody', 'td', 'template', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title',
|
|
52
|
+
'tr', 'track',
|
|
53
|
+
'u', 'ul', 'var', 'video', 'wbr',
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
SELF_CLOSING = {
|
|
57
|
+
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link',
|
|
58
|
+
'meta', 'param', 'source', 'track', 'wbr',
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
dynamic_tags: Dict[str, ContainerConfig] = {
|
|
62
|
+
tag: {
|
|
63
|
+
'base': HTMLTagBase,
|
|
64
|
+
'self_closing': tag in SELF_CLOSING,
|
|
65
|
+
'doc': f'Signifikant HTML <{tag}>.',
|
|
66
|
+
}
|
|
67
|
+
for tag in TAGS_LIST
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
factory = ContainerFactory(
|
|
71
|
+
dynamic_tags,
|
|
72
|
+
pyi_path=Path(__file__).parent,
|
|
73
|
+
pyi_file_name='__init__',
|
|
74
|
+
extra_classes=[HTMLTagBase, Doctype, Comment, HTMLArg],
|
|
75
|
+
write_init=False,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
TAGS = factory.make()
|
|
79
|
+
globals().update(TAGS)
|
|
80
|
+
|
|
81
|
+
__all__ = ['HTMLTagBase', 'Doctype', 'Comment', 'HTMLArg', 'TAGS', *TAGS.keys()]
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from transcript.core.container import ContainerBase, Fragment
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class JinjaNode(ContainerBase):
|
|
9
|
+
"""Blok dyrektywy Jinja2."""
|
|
10
|
+
|
|
11
|
+
name: str = 'jinja'
|
|
12
|
+
statement: str = ''
|
|
13
|
+
closing: str | None = None
|
|
14
|
+
|
|
15
|
+
def __init__(self, statement: str = '', *contents: Any, closing: str | None = None):
|
|
16
|
+
super().__init__(*contents)
|
|
17
|
+
if statement:
|
|
18
|
+
self.statement = statement
|
|
19
|
+
if closing is not None:
|
|
20
|
+
self.closing = closing
|
|
21
|
+
|
|
22
|
+
def _open(self) -> str:
|
|
23
|
+
return f'{{% {self.statement} %}}'
|
|
24
|
+
|
|
25
|
+
def _close(self) -> str:
|
|
26
|
+
if not self.closing:
|
|
27
|
+
return ''
|
|
28
|
+
return f'{{% {self.closing} %}}'
|
|
29
|
+
|
|
30
|
+
def build(self, depth: int = 0) -> str:
|
|
31
|
+
indent = self._indent(depth)
|
|
32
|
+
open_tag = f'{indent}{self._open()}'
|
|
33
|
+
close_tag = self._close()
|
|
34
|
+
if not self.content:
|
|
35
|
+
if close_tag:
|
|
36
|
+
return f'{open_tag}{close_tag}'
|
|
37
|
+
return open_tag
|
|
38
|
+
inner = '\n'.join(self._render_child(element, depth + 1) for element in self.content)
|
|
39
|
+
if close_tag:
|
|
40
|
+
return f'{open_tag}\n{inner}\n{indent}{close_tag}'
|
|
41
|
+
return f'{open_tag}\n{inner}'
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class JinjaVar(ContainerBase):
|
|
45
|
+
"""Wyrażenie ``{{ expression }}``."""
|
|
46
|
+
|
|
47
|
+
name: str = 'jinja_var'
|
|
48
|
+
|
|
49
|
+
def __init__(self, expression: str, *filters: str):
|
|
50
|
+
super().__init__()
|
|
51
|
+
self.expression = expression
|
|
52
|
+
self.filters = filters
|
|
53
|
+
|
|
54
|
+
def build(self, depth: int = 0) -> str:
|
|
55
|
+
expr = self.expression
|
|
56
|
+
for jinja_filter in self.filters:
|
|
57
|
+
expr += f'|{jinja_filter}'
|
|
58
|
+
return f'{self._indent(depth)}{{{{ {expr} }}}}'
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class JinjaComment(ContainerBase):
|
|
62
|
+
"""Komentarz ``{# ... #}``."""
|
|
63
|
+
|
|
64
|
+
name: str = 'jinja_comment'
|
|
65
|
+
|
|
66
|
+
def __init__(self, text: str = ''):
|
|
67
|
+
super().__init__()
|
|
68
|
+
self.text = text
|
|
69
|
+
|
|
70
|
+
def build(self, depth: int = 0) -> str:
|
|
71
|
+
return f'{self._indent(depth)}{{# {self.text} #}}'
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class JinjaExtends(JinjaNode):
|
|
75
|
+
name: str = 'extends'
|
|
76
|
+
closing = None
|
|
77
|
+
|
|
78
|
+
def __init__(self, template: str):
|
|
79
|
+
super().__init__(f'extends "{template}"')
|
|
80
|
+
self.template = template
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class JinjaInclude(JinjaNode):
|
|
84
|
+
name: str = 'include'
|
|
85
|
+
closing = None
|
|
86
|
+
|
|
87
|
+
def __init__(self, template: str, context: str = ''):
|
|
88
|
+
statement = f'include "{template}"'
|
|
89
|
+
if context:
|
|
90
|
+
statement += f' {context}'
|
|
91
|
+
super().__init__(statement)
|
|
92
|
+
self.template = template
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class JinjaBlock(JinjaNode):
|
|
96
|
+
name: str = 'block'
|
|
97
|
+
|
|
98
|
+
def __init__(self, block_name: str, *contents: Any):
|
|
99
|
+
super().__init__(f'block {block_name}', *contents, closing='endblock')
|
|
100
|
+
self.block_name = block_name
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class JinjaFor(JinjaNode):
|
|
104
|
+
name: str = 'for'
|
|
105
|
+
|
|
106
|
+
def __init__(self, item: str, iterable: str, *contents: Any, reversed_loop: bool = False):
|
|
107
|
+
statement = f'for {item} in {iterable}'
|
|
108
|
+
if reversed_loop:
|
|
109
|
+
statement += ' reversed'
|
|
110
|
+
super().__init__(statement, *contents, closing='endfor')
|
|
111
|
+
self.item = item
|
|
112
|
+
self.iterable = iterable
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class JinjaIf(JinjaNode):
|
|
116
|
+
name: str = 'if'
|
|
117
|
+
|
|
118
|
+
def __init__(self, condition: str, *contents: Any):
|
|
119
|
+
super().__init__(f'if {condition}', *contents, closing='endif')
|
|
120
|
+
self.condition = condition
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class JinjaElif(JinjaNode):
|
|
124
|
+
name: str = 'elif'
|
|
125
|
+
closing = None
|
|
126
|
+
|
|
127
|
+
def __init__(self, condition: str, *contents: Any):
|
|
128
|
+
super().__init__(f'elif {condition}', *contents)
|
|
129
|
+
self.condition = condition
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class JinjaElse(JinjaNode):
|
|
133
|
+
name: str = 'else'
|
|
134
|
+
closing = None
|
|
135
|
+
|
|
136
|
+
def __init__(self, *contents: Any):
|
|
137
|
+
super().__init__('else', *contents)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
class JinjaMacro(JinjaNode):
|
|
141
|
+
name: str = 'macro'
|
|
142
|
+
|
|
143
|
+
def __init__(self, macro_name: str, signature: str = '', *contents: Any):
|
|
144
|
+
args = f'({signature})' if signature else '()'
|
|
145
|
+
super().__init__(f'macro {macro_name}{args}', *contents, closing='endmacro')
|
|
146
|
+
self.macro_name = macro_name
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
class JinjaCall(JinjaNode):
|
|
150
|
+
name: str = 'call'
|
|
151
|
+
|
|
152
|
+
def __init__(self, call_expr: str, *contents: Any):
|
|
153
|
+
super().__init__(f'call {call_expr}', *contents, closing='endcall')
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
class JinjaFilter(JinjaNode):
|
|
157
|
+
name: str = 'filter'
|
|
158
|
+
|
|
159
|
+
def __init__(self, filter_expr: str, *contents: Any):
|
|
160
|
+
super().__init__(f'filter {filter_expr}', *contents, closing='endfilter')
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
class JinjaWith(JinjaNode):
|
|
164
|
+
name: str = 'with'
|
|
165
|
+
|
|
166
|
+
def __init__(self, assignments: str = '', *contents: Any):
|
|
167
|
+
statement = f'with {assignments}'.rstrip()
|
|
168
|
+
super().__init__(statement, *contents, closing='endwith')
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class JinjaSet(JinjaNode):
|
|
172
|
+
name: str = 'set'
|
|
173
|
+
closing = None
|
|
174
|
+
|
|
175
|
+
def __init__(self, target: str, expression: str | None = None, *contents: Any):
|
|
176
|
+
if expression is None:
|
|
177
|
+
super().__init__(f'set {target}', *contents, closing='endset')
|
|
178
|
+
else:
|
|
179
|
+
super().__init__(f'set {target} = {expression}', *contents)
|
|
180
|
+
self.target = target
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
class JinjaFrom(JinjaNode):
|
|
184
|
+
name: str = 'from'
|
|
185
|
+
closing = None
|
|
186
|
+
|
|
187
|
+
def __init__(self, template: str, names: str):
|
|
188
|
+
super().__init__(f'from "{template}" import {names}')
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
class JinjaImport(JinjaNode):
|
|
192
|
+
name: str = 'import'
|
|
193
|
+
closing = None
|
|
194
|
+
|
|
195
|
+
def __init__(self, template: str, alias: str | None = None):
|
|
196
|
+
statement = f'import "{template}"'
|
|
197
|
+
if alias:
|
|
198
|
+
statement += f' as {alias}'
|
|
199
|
+
super().__init__(statement)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
class JinjaRaw(JinjaNode):
|
|
203
|
+
name: str = 'raw'
|
|
204
|
+
|
|
205
|
+
def __init__(self, *contents: Any):
|
|
206
|
+
super().__init__('raw', *contents, closing='endraw')
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
class JinjaAutoescape(JinjaNode):
|
|
210
|
+
name: str = 'autoescape'
|
|
211
|
+
|
|
212
|
+
def __init__(self, enabled: bool | str = True, *contents: Any):
|
|
213
|
+
value = enabled if isinstance(enabled, str) else ('true' if enabled else 'false')
|
|
214
|
+
super().__init__(f'autoescape {value}', *contents, closing='endautoescape')
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
class JinjaTemplate(Fragment):
|
|
218
|
+
"""Kompletny szablon Jinja2 składany z bloków."""
|
|
219
|
+
|
|
220
|
+
name: str = 'jinja_template'
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
__all__ = [
|
|
224
|
+
'JinjaNode', 'JinjaVar', 'JinjaComment', 'JinjaExtends', 'JinjaInclude',
|
|
225
|
+
'JinjaBlock', 'JinjaFor', 'JinjaIf', 'JinjaElif', 'JinjaElse', 'JinjaMacro',
|
|
226
|
+
'JinjaCall', 'JinjaFilter', 'JinjaWith', 'JinjaSet', 'JinjaFrom', 'JinjaImport',
|
|
227
|
+
'JinjaRaw', 'JinjaAutoescape', 'JinjaTemplate',
|
|
228
|
+
]
|