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,280 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from transcript.core.container import ContainerBase, Fragment
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class JSLine(ContainerBase):
|
|
9
|
+
"""Pojedyncza instrukcja JavaScript (bez bloku)."""
|
|
10
|
+
|
|
11
|
+
name: str = 'js_line'
|
|
12
|
+
|
|
13
|
+
def __init__(self, statement: str = '', *, semicolon: bool = True):
|
|
14
|
+
super().__init__()
|
|
15
|
+
text = statement.rstrip()
|
|
16
|
+
if semicolon and text and not text.endswith((';', '{', ',')):
|
|
17
|
+
text = f'{text};'
|
|
18
|
+
self.statement = text
|
|
19
|
+
|
|
20
|
+
def build(self, depth: int = 0) -> str:
|
|
21
|
+
if not self.statement:
|
|
22
|
+
return ''
|
|
23
|
+
return f'{self._indent(depth)}{self.statement}'
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class JSComment(ContainerBase):
|
|
27
|
+
name: str = 'js_comment'
|
|
28
|
+
|
|
29
|
+
def __init__(self, text: str = '', *, block: bool = False):
|
|
30
|
+
super().__init__()
|
|
31
|
+
self.text = text
|
|
32
|
+
self.block = block
|
|
33
|
+
|
|
34
|
+
def build(self, depth: int = 0) -> str:
|
|
35
|
+
indent = self._indent(depth)
|
|
36
|
+
if self.block:
|
|
37
|
+
return f'{indent}/* {self.text} */'
|
|
38
|
+
return f'{indent}// {self.text}'
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class JSBlock(ContainerBase):
|
|
42
|
+
"""Blok ``{ ... }`` — kontener z otwarciem, ciałem i zamknięciem."""
|
|
43
|
+
|
|
44
|
+
name: str = 'js_block'
|
|
45
|
+
header: str = ''
|
|
46
|
+
|
|
47
|
+
def __init__(self, header: str = '', *contents: Any):
|
|
48
|
+
super().__init__(*contents)
|
|
49
|
+
if header:
|
|
50
|
+
self.header = header.rstrip()
|
|
51
|
+
|
|
52
|
+
def _open(self) -> str:
|
|
53
|
+
if self.header:
|
|
54
|
+
return f'{self.header} {{'
|
|
55
|
+
return '{'
|
|
56
|
+
|
|
57
|
+
def _close(self) -> str:
|
|
58
|
+
return '}'
|
|
59
|
+
|
|
60
|
+
def build(self, depth: int = 0) -> str:
|
|
61
|
+
indent = self._indent(depth)
|
|
62
|
+
open_line = f'{indent}{self._open()}'
|
|
63
|
+
close_line = f'{indent}{self._close()}'
|
|
64
|
+
if not self.content:
|
|
65
|
+
return f'{open_line}{self._close()}'
|
|
66
|
+
inner = '\n'.join(self._render_child(item, depth + 1) for item in self.content)
|
|
67
|
+
return f'{open_line}\n{inner}\n{close_line}'
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class JSModule(Fragment):
|
|
71
|
+
"""Plik / moduł JavaScript."""
|
|
72
|
+
|
|
73
|
+
name: str = 'js_module'
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class JSConst(JSLine):
|
|
77
|
+
name: str = 'js_const'
|
|
78
|
+
|
|
79
|
+
def __init__(self, identifier: str, value: Any):
|
|
80
|
+
rendered = value if isinstance(value, str) else repr(value)
|
|
81
|
+
super().__init__(f'const {identifier} = {rendered}')
|
|
82
|
+
self.identifier = identifier
|
|
83
|
+
self.value = value
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class JSLet(JSLine):
|
|
87
|
+
name: str = 'js_let'
|
|
88
|
+
|
|
89
|
+
def __init__(self, identifier: str, value: Any = None):
|
|
90
|
+
if value is None:
|
|
91
|
+
super().__init__(f'let {identifier}')
|
|
92
|
+
else:
|
|
93
|
+
rendered = value if isinstance(value, str) else repr(value)
|
|
94
|
+
super().__init__(f'let {identifier} = {rendered}')
|
|
95
|
+
self.identifier = identifier
|
|
96
|
+
self.value = value
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class JSVar(JSLine):
|
|
100
|
+
name: str = 'js_var'
|
|
101
|
+
|
|
102
|
+
def __init__(self, identifier: str, value: Any = None):
|
|
103
|
+
if value is None:
|
|
104
|
+
super().__init__(f'var {identifier}')
|
|
105
|
+
else:
|
|
106
|
+
rendered = value if isinstance(value, str) else repr(value)
|
|
107
|
+
super().__init__(f'var {identifier} = {rendered}')
|
|
108
|
+
self.identifier = identifier
|
|
109
|
+
self.value = value
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class JSReturn(JSLine):
|
|
113
|
+
name: str = 'js_return'
|
|
114
|
+
|
|
115
|
+
def __init__(self, expression: str | None = None):
|
|
116
|
+
if expression is None:
|
|
117
|
+
super().__init__('return')
|
|
118
|
+
else:
|
|
119
|
+
super().__init__(f'return {expression}')
|
|
120
|
+
self.expression = expression
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class JSCall(JSLine):
|
|
124
|
+
name: str = 'js_call'
|
|
125
|
+
|
|
126
|
+
def __init__(self, callee: str, *args: Any, semicolon: bool = True):
|
|
127
|
+
rendered = ', '.join(arg if isinstance(arg, str) else repr(arg) for arg in args)
|
|
128
|
+
super().__init__(f'{callee}({rendered})', semicolon=semicolon)
|
|
129
|
+
self.callee = callee
|
|
130
|
+
self.args = args
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class JSFunction(JSBlock):
|
|
134
|
+
"""Funkcja: ``function name(args) { ... }`` albo ``const name = (args) => { ... }``."""
|
|
135
|
+
|
|
136
|
+
name: str = 'js_function'
|
|
137
|
+
|
|
138
|
+
def __init__(
|
|
139
|
+
self,
|
|
140
|
+
fn_name: str = '',
|
|
141
|
+
args: str = '',
|
|
142
|
+
*contents: Any,
|
|
143
|
+
arrow: bool = False,
|
|
144
|
+
async_: bool = False,
|
|
145
|
+
):
|
|
146
|
+
self.fn_name = fn_name
|
|
147
|
+
self.args = args
|
|
148
|
+
self.arrow = arrow
|
|
149
|
+
self.async_ = async_
|
|
150
|
+
if arrow:
|
|
151
|
+
prefix = 'async ' if async_ else ''
|
|
152
|
+
binding = f'const {fn_name} = ' if fn_name else ''
|
|
153
|
+
header = f'{binding}{prefix}({args}) =>'
|
|
154
|
+
else:
|
|
155
|
+
prefix = 'async ' if async_ else ''
|
|
156
|
+
label = f' {fn_name}' if fn_name else ''
|
|
157
|
+
header = f'{prefix}function{label}({args})'
|
|
158
|
+
super().__init__(header, *contents)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
class JSIf(JSBlock):
|
|
162
|
+
name: str = 'js_if'
|
|
163
|
+
|
|
164
|
+
def __init__(self, condition: str, *contents: Any):
|
|
165
|
+
super().__init__(f'if ({condition})', *contents)
|
|
166
|
+
self.condition = condition
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
class JSElse(JSBlock):
|
|
170
|
+
name: str = 'js_else'
|
|
171
|
+
|
|
172
|
+
def __init__(self, *contents: Any):
|
|
173
|
+
super().__init__('else', *contents)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
class JSElif(JSBlock):
|
|
177
|
+
name: str = 'js_elif'
|
|
178
|
+
|
|
179
|
+
def __init__(self, condition: str, *contents: Any):
|
|
180
|
+
super().__init__(f'else if ({condition})', *contents)
|
|
181
|
+
self.condition = condition
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
class JSFor(JSBlock):
|
|
185
|
+
name: str = 'js_for'
|
|
186
|
+
|
|
187
|
+
def __init__(self, header: str, *contents: Any):
|
|
188
|
+
text = header if header.startswith('for') else f'for ({header})'
|
|
189
|
+
super().__init__(text, *contents)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
class JSWhile(JSBlock):
|
|
193
|
+
name: str = 'js_while'
|
|
194
|
+
|
|
195
|
+
def __init__(self, condition: str, *contents: Any):
|
|
196
|
+
super().__init__(f'while ({condition})', *contents)
|
|
197
|
+
self.condition = condition
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
class JSClass(JSBlock):
|
|
201
|
+
name: str = 'js_class'
|
|
202
|
+
|
|
203
|
+
def __init__(self, class_name: str, *contents: Any, extends: str | None = None):
|
|
204
|
+
header = f'class {class_name}'
|
|
205
|
+
if extends:
|
|
206
|
+
header += f' extends {extends}'
|
|
207
|
+
super().__init__(header, *contents)
|
|
208
|
+
self.class_name = class_name
|
|
209
|
+
self.extends = extends
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
class JSMethod(JSBlock):
|
|
213
|
+
name: str = 'js_method'
|
|
214
|
+
|
|
215
|
+
def __init__(self, method_name: str, args: str = '', *contents: Any, async_: bool = False):
|
|
216
|
+
prefix = 'async ' if async_ else ''
|
|
217
|
+
super().__init__(f'{prefix}{method_name}({args})', *contents)
|
|
218
|
+
self.method_name = method_name
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
class JSExport(JSLine):
|
|
222
|
+
name: str = 'js_export'
|
|
223
|
+
|
|
224
|
+
def __init__(self, statement: str):
|
|
225
|
+
text = statement if statement.lstrip().startswith('export') else f'export {statement}'
|
|
226
|
+
super().__init__(text)
|
|
227
|
+
self.statement = statement
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
class JSImport(JSLine):
|
|
231
|
+
name: str = 'js_import'
|
|
232
|
+
|
|
233
|
+
def __init__(self, clause: str, source: str | None = None):
|
|
234
|
+
if source is None:
|
|
235
|
+
super().__init__(clause if clause.lstrip().startswith('import') else f'import {clause}')
|
|
236
|
+
else:
|
|
237
|
+
super().__init__(f'import {clause} from {source!r}')
|
|
238
|
+
self.clause = clause
|
|
239
|
+
self.source = source
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
class JSObject(JSBlock):
|
|
243
|
+
"""Literal obiektu — dzieci to linie ``klucz: wartość,``."""
|
|
244
|
+
|
|
245
|
+
name: str = 'js_object'
|
|
246
|
+
|
|
247
|
+
def __init__(self, *contents: Any, binding: str | None = None):
|
|
248
|
+
header = f'const {binding} =' if binding else ''
|
|
249
|
+
super().__init__(header, *contents)
|
|
250
|
+
self.binding = binding
|
|
251
|
+
|
|
252
|
+
def _open(self) -> str:
|
|
253
|
+
if self.header:
|
|
254
|
+
return f'{self.header} {{'
|
|
255
|
+
return '{'
|
|
256
|
+
|
|
257
|
+
def _close(self) -> str:
|
|
258
|
+
return '};' if self.binding else '}'
|
|
259
|
+
|
|
260
|
+
def _inject(self, data: Any) -> None:
|
|
261
|
+
if isinstance(data, dict):
|
|
262
|
+
for key, value in data.items():
|
|
263
|
+
rendered = value if isinstance(value, str) else repr(value)
|
|
264
|
+
self.content.append(JSLine(f'{key}: {rendered},', semicolon=False))
|
|
265
|
+
return
|
|
266
|
+
super()._inject(data)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
class JS(JSModule):
|
|
270
|
+
"""Alias wygodnego kontenera modułu JavaScript."""
|
|
271
|
+
|
|
272
|
+
name: str = 'js'
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
__all__ = [
|
|
276
|
+
'JS', 'JSModule', 'JSBlock', 'JSLine', 'JSComment',
|
|
277
|
+
'JSConst', 'JSLet', 'JSVar', 'JSReturn', 'JSCall',
|
|
278
|
+
'JSFunction', 'JSIf', 'JSElse', 'JSElif', 'JSFor', 'JSWhile',
|
|
279
|
+
'JSClass', 'JSMethod', 'JSExport', 'JSImport', 'JSObject',
|
|
280
|
+
]
|
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import Any, Literal
|
|
5
|
+
|
|
6
|
+
from transcript.core.container import ContainerBase, Fragment
|
|
7
|
+
|
|
8
|
+
BEMKind = Literal['block', 'element', 'modifier', 'raw']
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _css_name(name: str) -> str:
|
|
12
|
+
return name.replace('_', '-')
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _strip_selector_name(selector: str) -> str:
|
|
16
|
+
text = selector.strip()
|
|
17
|
+
text = re.sub(r'^[&]?[.#]?', '', text)
|
|
18
|
+
return text.lstrip('_-')
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def bem_selector(
|
|
22
|
+
name: str,
|
|
23
|
+
kind: BEMKind = 'element',
|
|
24
|
+
*,
|
|
25
|
+
block: str | None = None,
|
|
26
|
+
) -> str:
|
|
27
|
+
"""Buduje selektor BEM: ``.block``, ``&__element``, ``&--modifier``."""
|
|
28
|
+
raw = name.strip()
|
|
29
|
+
if kind == 'raw' or raw.startswith(('@', ':', '>', '+', '~', '*', '[')) or raw.startswith('&'):
|
|
30
|
+
return raw
|
|
31
|
+
clean = _strip_selector_name(raw)
|
|
32
|
+
if kind == 'block':
|
|
33
|
+
base = block or clean
|
|
34
|
+
return base if base.startswith('.') else f'.{base}'
|
|
35
|
+
if kind == 'modifier' or raw.startswith('--') or raw.startswith('.--') or raw.startswith('&--'):
|
|
36
|
+
return f'&--{clean}'
|
|
37
|
+
if kind == 'element' or raw.startswith('__') or raw.startswith('.__') or raw.startswith('&__'):
|
|
38
|
+
return f'&__{clean}'
|
|
39
|
+
return f'&__{clean}'
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _infer_bem_kind(selector: str) -> BEMKind:
|
|
43
|
+
raw = selector.strip()
|
|
44
|
+
if raw.startswith(('@', ':', '>', '+', '~', '*', '[')):
|
|
45
|
+
return 'raw'
|
|
46
|
+
if raw.startswith('&:') or raw.startswith('&::') or raw.startswith('&['):
|
|
47
|
+
return 'raw'
|
|
48
|
+
if raw.startswith('--') or raw.startswith('.--') or raw.startswith('&--'):
|
|
49
|
+
return 'modifier'
|
|
50
|
+
if raw.startswith('__') or raw.startswith('.__') or raw.startswith('&__'):
|
|
51
|
+
return 'element'
|
|
52
|
+
if raw.startswith('&'):
|
|
53
|
+
return 'raw'
|
|
54
|
+
return 'element'
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class SCSSProp:
|
|
58
|
+
"""Deklaracja ``właściwość: wartość;``."""
|
|
59
|
+
|
|
60
|
+
def __init__(self, name: str, value: Any):
|
|
61
|
+
self.name = _css_name(name)
|
|
62
|
+
self.value = value
|
|
63
|
+
|
|
64
|
+
def __bool__(self) -> bool:
|
|
65
|
+
return self.value is not None and self.value != ''
|
|
66
|
+
|
|
67
|
+
def build(self, depth: int = 0) -> str:
|
|
68
|
+
indent = ' ' * depth
|
|
69
|
+
return f'{indent}{self.name}: {self.value};'
|
|
70
|
+
|
|
71
|
+
def __repr__(self) -> str:
|
|
72
|
+
return f'SCSSProp({self.name}: {self.value})'
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class SCSSComment(ContainerBase):
|
|
76
|
+
name: str = 'scss_comment'
|
|
77
|
+
|
|
78
|
+
def __init__(self, text: str):
|
|
79
|
+
super().__init__()
|
|
80
|
+
self.text = text
|
|
81
|
+
|
|
82
|
+
def build(self, depth: int = 0) -> str:
|
|
83
|
+
return f'{self._indent(depth)}/* {self.text} */'
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class SCSSVariable(ContainerBase):
|
|
87
|
+
name: str = 'scss_variable'
|
|
88
|
+
|
|
89
|
+
def __init__(self, name: str, value: Any, default: bool = False):
|
|
90
|
+
super().__init__()
|
|
91
|
+
self.var_name = name if name.startswith('$') else f'${name}'
|
|
92
|
+
self.var_value = value
|
|
93
|
+
self.default = default
|
|
94
|
+
|
|
95
|
+
def build(self, depth: int = 0) -> str:
|
|
96
|
+
suffix = ' !default' if self.default else ''
|
|
97
|
+
return f'{self._indent(depth)}{self.var_name}: {self.var_value}{suffix};'
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class SCSSLine(ContainerBase):
|
|
101
|
+
"""Pojedyncza dyrektywa SCSS bez klamr, np. ``@include``, ``@extend``."""
|
|
102
|
+
|
|
103
|
+
name: str = 'scss_line'
|
|
104
|
+
|
|
105
|
+
def __init__(self, statement: str):
|
|
106
|
+
super().__init__()
|
|
107
|
+
self.statement = statement.rstrip(';')
|
|
108
|
+
|
|
109
|
+
def build(self, depth: int = 0) -> str:
|
|
110
|
+
return f'{self._indent(depth)}{self.statement};'
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class SCSS(ContainerBase):
|
|
114
|
+
"""Reguła SCSS. Zagnieżdżone reguły i deklaracje wstrzykuje się operatorem ``<<``.
|
|
115
|
+
|
|
116
|
+
Gdy ``bem=True``, zagnieżdżone reguły klas są automatycznie przepisywane
|
|
117
|
+
do notacji BEM (``&__element``, ``&--modifier``).
|
|
118
|
+
"""
|
|
119
|
+
|
|
120
|
+
name: str = 'scss'
|
|
121
|
+
bem: bool = False
|
|
122
|
+
|
|
123
|
+
def __init__(self, selector: str = '&', *contents: Any, bem: bool | None = None, **props: Any):
|
|
124
|
+
self.selector = selector
|
|
125
|
+
self.props: list[SCSSProp] = []
|
|
126
|
+
if bem is not None:
|
|
127
|
+
self.bem = bem
|
|
128
|
+
if not getattr(self, 'block_name', None):
|
|
129
|
+
self.block_name = _strip_selector_name(selector) if self.bem else ''
|
|
130
|
+
super().__init__(*contents)
|
|
131
|
+
for key, value in props.items():
|
|
132
|
+
self.props.append(SCSSProp(key, value))
|
|
133
|
+
|
|
134
|
+
def _apply_bem(self, child: SCSS) -> None:
|
|
135
|
+
if not self.bem:
|
|
136
|
+
return
|
|
137
|
+
if getattr(child, '_bem_locked', False):
|
|
138
|
+
return
|
|
139
|
+
kind = getattr(child, 'bem_kind', None)
|
|
140
|
+
# Zagnieżdżony „block” (np. CSSClass) traktujemy jak element/modyfikator wg selektora.
|
|
141
|
+
if kind in (None, 'block'):
|
|
142
|
+
kind = _infer_bem_kind(child.selector)
|
|
143
|
+
if kind == 'block':
|
|
144
|
+
kind = 'element'
|
|
145
|
+
if kind == 'raw':
|
|
146
|
+
return
|
|
147
|
+
child.selector = bem_selector(child.selector, kind, block=self.block_name)
|
|
148
|
+
if isinstance(child, CSSClass):
|
|
149
|
+
child.class_name = _strip_selector_name(child.selector)
|
|
150
|
+
child.bem_kind = kind
|
|
151
|
+
if not getattr(child, 'block_name', None):
|
|
152
|
+
child.block_name = self.block_name
|
|
153
|
+
child.bem = True
|
|
154
|
+
|
|
155
|
+
def _inject(self, data: Any) -> None:
|
|
156
|
+
inject_into = getattr(data, 'inject_into', None)
|
|
157
|
+
if callable(inject_into) and not isinstance(data, SCSS):
|
|
158
|
+
inject_into(self)
|
|
159
|
+
return
|
|
160
|
+
if isinstance(data, SCSSProp):
|
|
161
|
+
self.props.append(data)
|
|
162
|
+
return
|
|
163
|
+
if isinstance(data, dict):
|
|
164
|
+
for key, value in data.items():
|
|
165
|
+
self.props.append(SCSSProp(key, value))
|
|
166
|
+
return
|
|
167
|
+
if (
|
|
168
|
+
isinstance(data, tuple)
|
|
169
|
+
and len(data) == 2
|
|
170
|
+
and isinstance(data[0], str)
|
|
171
|
+
and not isinstance(data[0], ContainerBase)
|
|
172
|
+
):
|
|
173
|
+
self.props.append(SCSSProp(data[0], data[1]))
|
|
174
|
+
return
|
|
175
|
+
if isinstance(data, SCSS):
|
|
176
|
+
self._apply_bem(data)
|
|
177
|
+
self.content.append(data)
|
|
178
|
+
return
|
|
179
|
+
super()._inject(data)
|
|
180
|
+
|
|
181
|
+
def element(self, name: str, *contents: Any, **props: Any) -> BEMElement:
|
|
182
|
+
node = BEMElement(name, *contents, block=self.block_name or _strip_selector_name(self.selector), **props)
|
|
183
|
+
self << node
|
|
184
|
+
return node
|
|
185
|
+
|
|
186
|
+
def modifier(self, name: str, *contents: Any, **props: Any) -> BEMModifier:
|
|
187
|
+
node = BEMModifier(name, *contents, block=self.block_name or _strip_selector_name(self.selector), **props)
|
|
188
|
+
self << node
|
|
189
|
+
return node
|
|
190
|
+
|
|
191
|
+
def build(self, depth: int = 0) -> str:
|
|
192
|
+
indent = self._indent(depth)
|
|
193
|
+
lines = [f'{indent}{self.selector} {{']
|
|
194
|
+
for prop in self.props:
|
|
195
|
+
if prop:
|
|
196
|
+
lines.append(prop.build(depth + 1))
|
|
197
|
+
for child in self.content:
|
|
198
|
+
lines.append(self._render_child(child, depth + 1))
|
|
199
|
+
lines.append(f'{indent}}}')
|
|
200
|
+
return '\n'.join(lines)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class SCSSRoot(Fragment):
|
|
204
|
+
"""Plik SCSS — zbiór zmiennych, mixinów i reguł."""
|
|
205
|
+
|
|
206
|
+
name: str = 'scss_root'
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
class SCSSMixin(SCSS):
|
|
210
|
+
name: str = 'scss_mixin'
|
|
211
|
+
|
|
212
|
+
def __init__(self, mixin_name: str, args: str = '', *contents: Any, **props: Any):
|
|
213
|
+
signature = f'@mixin {mixin_name}'
|
|
214
|
+
if args:
|
|
215
|
+
signature += f'({args})'
|
|
216
|
+
super().__init__(signature, *contents, **props)
|
|
217
|
+
self.mixin_name = mixin_name
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
class SCSSInclude(SCSSLine):
|
|
221
|
+
name: str = 'scss_include'
|
|
222
|
+
|
|
223
|
+
def __init__(self, mixin_name: str, args: str = ''):
|
|
224
|
+
call = f'{mixin_name}({args})' if args else mixin_name
|
|
225
|
+
super().__init__(f'@include {call}')
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
class SCSSExtend(SCSSLine):
|
|
229
|
+
name: str = 'scss_extend'
|
|
230
|
+
|
|
231
|
+
def __init__(self, selector: str):
|
|
232
|
+
super().__init__(f'@extend {selector}')
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
class SCSSMedia(SCSS):
|
|
236
|
+
name: str = 'scss_media'
|
|
237
|
+
|
|
238
|
+
def __init__(self, query: str, *contents: Any, **props: Any):
|
|
239
|
+
super().__init__(f'@media {query}', *contents, **props)
|
|
240
|
+
self.query = query
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
class SCSSSupports(SCSS):
|
|
244
|
+
name: str = 'scss_supports'
|
|
245
|
+
|
|
246
|
+
def __init__(self, condition: str, *contents: Any, **props: Any):
|
|
247
|
+
super().__init__(f'@supports {condition}', *contents, **props)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
class SCSSIf(SCSS):
|
|
251
|
+
name: str = 'scss_if'
|
|
252
|
+
|
|
253
|
+
def __init__(self, condition: str, *contents: Any, **props: Any):
|
|
254
|
+
super().__init__(f'@if {condition}', *contents, **props)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
class SCSSUse(SCSSLine):
|
|
258
|
+
name: str = 'scss_use'
|
|
259
|
+
|
|
260
|
+
def __init__(self, module: str, namespace: str | None = None):
|
|
261
|
+
statement = f'@use "{module}"'
|
|
262
|
+
if namespace:
|
|
263
|
+
statement += f' as {namespace}'
|
|
264
|
+
super().__init__(statement)
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
class SCSSForward(SCSSLine):
|
|
268
|
+
name: str = 'scss_forward'
|
|
269
|
+
|
|
270
|
+
def __init__(self, module: str):
|
|
271
|
+
super().__init__(f'@forward "{module}"')
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
class SCSSImport(SCSSLine):
|
|
275
|
+
name: str = 'scss_import'
|
|
276
|
+
|
|
277
|
+
def __init__(self, path: str):
|
|
278
|
+
super().__init__(f'@import "{path}"')
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
class CSSClass(SCSS):
|
|
282
|
+
"""Reguła klasy CSS/SCSS z domyślnym BEM.
|
|
283
|
+
|
|
284
|
+
Samodzielnie generuje blok ``.nazwa { ... }``.
|
|
285
|
+
Wstrzyknięty do znacznika XML/HTML (`tag << CSSClass('hero')`)
|
|
286
|
+
dodaje nazwę klasy do atrybutu ``class``.
|
|
287
|
+
Zagnieżdżone klasy stają się ``&__element`` / ``&--modifier``.
|
|
288
|
+
"""
|
|
289
|
+
|
|
290
|
+
name: str = 'css_class'
|
|
291
|
+
bem: bool = True
|
|
292
|
+
|
|
293
|
+
def __init__(self, class_name: str, *contents: Any, bem: bool | None = None, **props: Any):
|
|
294
|
+
self.class_name = class_name.lstrip('.')
|
|
295
|
+
self.bem_kind: BEMKind = 'block'
|
|
296
|
+
super().__init__(f'.{self.class_name}', *contents, bem=True if bem is None else bem, **props)
|
|
297
|
+
self.block_name = self.class_name
|
|
298
|
+
|
|
299
|
+
@property
|
|
300
|
+
def html_class(self) -> str:
|
|
301
|
+
return self.class_name
|
|
302
|
+
|
|
303
|
+
def inject_into(self, container: ContainerBase) -> None:
|
|
304
|
+
from transcript.builders.xml import XMLArg, XMLTagBase
|
|
305
|
+
|
|
306
|
+
if isinstance(container, XMLTagBase):
|
|
307
|
+
container._add_arg(XMLArg('class', self.html_class))
|
|
308
|
+
return
|
|
309
|
+
if isinstance(container, SCSS):
|
|
310
|
+
container._apply_bem(self)
|
|
311
|
+
container.content.append(self)
|
|
312
|
+
return
|
|
313
|
+
container.content.append(self)
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
class BEM(CSSClass):
|
|
317
|
+
"""Blok BEM — alias ``CSSClass`` z wymuszonym ``bem=True``."""
|
|
318
|
+
|
|
319
|
+
name: str = 'bem'
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
class BEMElement(SCSS):
|
|
323
|
+
"""Element BEM: ``&__name`` (oraz klasa HTML ``block__name``)."""
|
|
324
|
+
|
|
325
|
+
name: str = 'bem_element'
|
|
326
|
+
bem: bool = True
|
|
327
|
+
bem_kind: BEMKind = 'element'
|
|
328
|
+
_bem_locked: bool = True
|
|
329
|
+
|
|
330
|
+
def __init__(self, element: str, *contents: Any, block: str = '', **props: Any):
|
|
331
|
+
self.element_name = _strip_selector_name(element)
|
|
332
|
+
self.block_name = _strip_selector_name(block) if block else ''
|
|
333
|
+
super().__init__(f'&__{self.element_name}', *contents, bem=True, **props)
|
|
334
|
+
|
|
335
|
+
@property
|
|
336
|
+
def html_class(self) -> str:
|
|
337
|
+
if self.block_name:
|
|
338
|
+
return f'{self.block_name}__{self.element_name}'
|
|
339
|
+
return self.element_name
|
|
340
|
+
|
|
341
|
+
def inject_into(self, container: ContainerBase) -> None:
|
|
342
|
+
from transcript.builders.xml import XMLArg, XMLTagBase
|
|
343
|
+
|
|
344
|
+
if isinstance(container, XMLTagBase):
|
|
345
|
+
container._add_arg(XMLArg('class', self.html_class))
|
|
346
|
+
return
|
|
347
|
+
if isinstance(container, SCSS):
|
|
348
|
+
if not self.block_name and getattr(container, 'block_name', None):
|
|
349
|
+
self.block_name = container.block_name
|
|
350
|
+
container.content.append(self)
|
|
351
|
+
return
|
|
352
|
+
container.content.append(self)
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
class BEMModifier(SCSS):
|
|
356
|
+
"""Modyfikator BEM: ``&--name`` (oraz klasa HTML ``block--name``)."""
|
|
357
|
+
|
|
358
|
+
name: str = 'bem_modifier'
|
|
359
|
+
bem: bool = True
|
|
360
|
+
bem_kind: BEMKind = 'modifier'
|
|
361
|
+
_bem_locked: bool = True
|
|
362
|
+
|
|
363
|
+
def __init__(self, modifier: str, *contents: Any, block: str = '', **props: Any):
|
|
364
|
+
self.modifier_name = _strip_selector_name(modifier)
|
|
365
|
+
self.block_name = _strip_selector_name(block) if block else ''
|
|
366
|
+
super().__init__(f'&--{self.modifier_name}', *contents, bem=True, **props)
|
|
367
|
+
|
|
368
|
+
@property
|
|
369
|
+
def html_class(self) -> str:
|
|
370
|
+
if self.block_name:
|
|
371
|
+
return f'{self.block_name}--{self.modifier_name}'
|
|
372
|
+
return self.modifier_name
|
|
373
|
+
|
|
374
|
+
def inject_into(self, container: ContainerBase) -> None:
|
|
375
|
+
from transcript.builders.xml import XMLArg, XMLTagBase
|
|
376
|
+
|
|
377
|
+
if isinstance(container, XMLTagBase):
|
|
378
|
+
container._add_arg(XMLArg('class', self.html_class))
|
|
379
|
+
return
|
|
380
|
+
if isinstance(container, SCSS):
|
|
381
|
+
if not self.block_name and getattr(container, 'block_name', None):
|
|
382
|
+
self.block_name = container.block_name
|
|
383
|
+
container.content.append(self)
|
|
384
|
+
return
|
|
385
|
+
container.content.append(self)
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
__all__ = [
|
|
389
|
+
'SCSS', 'SCSSProp', 'SCSSRoot', 'SCSSVariable', 'SCSSComment',
|
|
390
|
+
'SCSSMixin', 'SCSSInclude', 'SCSSExtend', 'SCSSMedia', 'SCSSSupports',
|
|
391
|
+
'SCSSIf', 'SCSSUse', 'SCSSForward', 'SCSSImport', 'SCSSLine', 'CSSClass',
|
|
392
|
+
'BEM', 'BEMElement', 'BEMModifier', 'bem_selector',
|
|
393
|
+
]
|