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.
@@ -0,0 +1,392 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any, Iterable
4
+
5
+ from transcript.builders.scss import CSSClass, SCSS, SCSSRoot, SCSSVariable
6
+
7
+ # Skala spacing jak w Tailwind (rem)
8
+ SPACING: dict[str, str] = {
9
+ '0': '0',
10
+ 'px': '1px',
11
+ '0.5': '0.125rem',
12
+ '1': '0.25rem',
13
+ '1.5': '0.375rem',
14
+ '2': '0.5rem',
15
+ '2.5': '0.625rem',
16
+ '3': '0.75rem',
17
+ '3.5': '0.875rem',
18
+ '4': '1rem',
19
+ '5': '1.25rem',
20
+ '6': '1.5rem',
21
+ '7': '1.75rem',
22
+ '8': '2rem',
23
+ '9': '2.25rem',
24
+ '10': '2.5rem',
25
+ '12': '3rem',
26
+ '14': '3.5rem',
27
+ '16': '4rem',
28
+ '20': '5rem',
29
+ '24': '6rem',
30
+ '32': '8rem',
31
+ '40': '10rem',
32
+ '48': '12rem',
33
+ '56': '14rem',
34
+ '64': '16rem',
35
+ 'auto': 'auto',
36
+ 'full': '100%',
37
+ }
38
+
39
+ COLORS: dict[str, str] = {
40
+ 'transparent': 'transparent',
41
+ 'current': 'currentColor',
42
+ 'black': '#000000',
43
+ 'white': '#ffffff',
44
+ 'slate-50': '#f8fafc',
45
+ 'slate-100': '#f1f5f9',
46
+ 'slate-200': '#e2e8f0',
47
+ 'slate-300': '#cbd5e1',
48
+ 'slate-400': '#94a3b8',
49
+ 'slate-500': '#64748b',
50
+ 'slate-600': '#475569',
51
+ 'slate-700': '#334155',
52
+ 'slate-800': '#1e293b',
53
+ 'slate-900': '#0f172a',
54
+ 'gray-100': '#f3f4f6',
55
+ 'gray-300': '#d1d5db',
56
+ 'gray-500': '#6b7280',
57
+ 'gray-700': '#374151',
58
+ 'gray-900': '#111827',
59
+ 'red-500': '#ef4444',
60
+ 'red-600': '#dc2626',
61
+ 'orange-500': '#f97316',
62
+ 'amber-500': '#f59e0b',
63
+ 'yellow-400': '#facc15',
64
+ 'green-500': '#22c55e',
65
+ 'green-600': '#16a34a',
66
+ 'emerald-500': '#10b981',
67
+ 'teal-500': '#14b8a6',
68
+ 'cyan-500': '#06b6d4',
69
+ 'sky-500': '#0ea5e9',
70
+ 'blue-500': '#3b82f6',
71
+ 'blue-600': '#2563eb',
72
+ 'indigo-500': '#6366f1',
73
+ 'violet-500': '#8b5cf6',
74
+ 'purple-500': '#a855f7',
75
+ 'pink-500': '#ec4899',
76
+ 'rose-500': '#f43f5e',
77
+ }
78
+
79
+ FONT_SIZES: dict[str, tuple[str, str]] = {
80
+ 'xs': ('0.75rem', '1rem'),
81
+ 'sm': ('0.875rem', '1.25rem'),
82
+ 'base': ('1rem', '1.5rem'),
83
+ 'lg': ('1.125rem', '1.75rem'),
84
+ 'xl': ('1.25rem', '1.75rem'),
85
+ '2xl': ('1.5rem', '2rem'),
86
+ '3xl': ('1.875rem', '2.25rem'),
87
+ '4xl': ('2.25rem', '2.5rem'),
88
+ '5xl': ('3rem', '1'),
89
+ }
90
+
91
+ ROUNDED: dict[str, str] = {
92
+ 'none': '0',
93
+ 'sm': '0.125rem',
94
+ '': '0.25rem',
95
+ 'md': '0.375rem',
96
+ 'lg': '0.5rem',
97
+ 'xl': '0.75rem',
98
+ '2xl': '1rem',
99
+ '3xl': '1.5rem',
100
+ 'full': '9999px',
101
+ }
102
+
103
+
104
+ def utility(name: str, **props: Any) -> CSSClass:
105
+ """Pojedyncza klasa utility (bez BEM — płaski selektor Tailwind)."""
106
+ return CSSClass(name, bem=False, **props)
107
+
108
+
109
+ def tw(*parts: str) -> str:
110
+ """Składa nazwy klas Tailwind-like do atrybutu ``class``."""
111
+ return ' '.join(part for part in parts if part)
112
+
113
+
114
+ class TailwindVariables(SCSSRoot):
115
+ """Tokeny spacing / kolorów jako zmienne SCSS."""
116
+
117
+ name: str = 'tailwind_variables'
118
+
119
+ def __init__(self):
120
+ items: list[Any] = []
121
+ for key, value in SPACING.items():
122
+ safe = key.replace('.', '_')
123
+ items.append(SCSSVariable(f'space-{safe}', value))
124
+ for key, value in COLORS.items():
125
+ items.append(SCSSVariable(f'color-{key}', value))
126
+ super().__init__(*items)
127
+
128
+
129
+ class TailwindPreflight(SCSSRoot):
130
+ """Minimalny reset + bazowe style jak preflight Tailwind."""
131
+
132
+ name: str = 'tailwind_preflight'
133
+
134
+ def __init__(self):
135
+ super().__init__(
136
+ SCSS(
137
+ '*, *::before, *::after',
138
+ box_sizing='border-box',
139
+ border_width='0',
140
+ border_style='solid',
141
+ ),
142
+ SCSS('html', line_height='1.5'),
143
+ SCSS(
144
+ 'body',
145
+ margin='0',
146
+ font_family='system-ui, -apple-system, Segoe UI, Roboto, sans-serif',
147
+ line_height='inherit',
148
+ ),
149
+ SCSS('img, video', max_width='100%', height='auto', display='block'),
150
+ SCSS('a', color='inherit', text_decoration='inherit'),
151
+ SCSS('button, input, select, textarea', font='inherit', color='inherit'),
152
+ )
153
+
154
+
155
+ def _spacing_utils() -> list[CSSClass]:
156
+ rules: list[CSSClass] = []
157
+ sides = {
158
+ 'p': 'padding',
159
+ 'px': ('padding-left', 'padding-right'),
160
+ 'py': ('padding-top', 'padding-bottom'),
161
+ 'pt': 'padding-top',
162
+ 'pr': 'padding-right',
163
+ 'pb': 'padding-bottom',
164
+ 'pl': 'padding-left',
165
+ 'm': 'margin',
166
+ 'mx': ('margin-left', 'margin-right'),
167
+ 'my': ('margin-top', 'margin-bottom'),
168
+ 'mt': 'margin-top',
169
+ 'mr': 'margin-right',
170
+ 'mb': 'margin-bottom',
171
+ 'ml': 'margin-left',
172
+ 'gap': 'gap',
173
+ 'gap-x': 'column-gap',
174
+ 'gap-y': 'row-gap',
175
+ }
176
+ for prefix, prop in sides.items():
177
+ for key, value in SPACING.items():
178
+ name = f'{prefix}-{key}'
179
+ if isinstance(prop, tuple):
180
+ props = {p.replace('-', '_'): value for p in prop}
181
+ else:
182
+ props = {prop.replace('-', '_'): value}
183
+ rules.append(utility(name, **props))
184
+ return rules
185
+
186
+
187
+ def _layout_utils() -> list[CSSClass]:
188
+ return [
189
+ utility('block', display='block'),
190
+ utility('inline-block', display='inline-block'),
191
+ utility('inline', display='inline'),
192
+ utility('flex', display='flex'),
193
+ utility('inline-flex', display='inline-flex'),
194
+ utility('grid', display='grid'),
195
+ utility('hidden', display='none'),
196
+ utility('flex-row', flex_direction='row'),
197
+ utility('flex-col', flex_direction='column'),
198
+ utility('flex-wrap', flex_wrap='wrap'),
199
+ utility('flex-nowrap', flex_wrap='nowrap'),
200
+ utility('items-start', align_items='flex-start'),
201
+ utility('items-center', align_items='center'),
202
+ utility('items-end', align_items='flex-end'),
203
+ utility('items-stretch', align_items='stretch'),
204
+ utility('justify-start', justify_content='flex-start'),
205
+ utility('justify-center', justify_content='center'),
206
+ utility('justify-end', justify_content='flex-end'),
207
+ utility('justify-between', justify_content='space-between'),
208
+ utility('justify-around', justify_content='space-around'),
209
+ utility('grow', flex_grow='1'),
210
+ utility('shrink-0', flex_shrink='0'),
211
+ utility('grid-cols-1', grid_template_columns='repeat(1, minmax(0, 1fr))'),
212
+ utility('grid-cols-2', grid_template_columns='repeat(2, minmax(0, 1fr))'),
213
+ utility('grid-cols-3', grid_template_columns='repeat(3, minmax(0, 1fr))'),
214
+ utility('grid-cols-4', grid_template_columns='repeat(4, minmax(0, 1fr))'),
215
+ utility('col-span-2', grid_column='span 2 / span 2'),
216
+ utility('col-span-3', grid_column='span 3 / span 3'),
217
+ ]
218
+
219
+
220
+ def _size_utils() -> list[CSSClass]:
221
+ rules = [
222
+ utility('w-full', width='100%'),
223
+ utility('w-screen', width='100vw'),
224
+ utility('w-auto', width='auto'),
225
+ utility('w-fit', width='fit-content'),
226
+ utility('h-full', height='100%'),
227
+ utility('h-screen', height='100vh'),
228
+ utility('h-auto', height='auto'),
229
+ utility('min-h-screen', min_height='100vh'),
230
+ utility('min-w-0', min_width='0'),
231
+ utility('max-w-sm', max_width='24rem'),
232
+ utility('max-w-md', max_width='28rem'),
233
+ utility('max-w-lg', max_width='32rem'),
234
+ utility('max-w-xl', max_width='36rem'),
235
+ utility('max-w-2xl', max_width='42rem'),
236
+ utility('max-w-4xl', max_width='56rem'),
237
+ utility('max-w-6xl', max_width='72rem'),
238
+ utility('max-w-7xl', max_width='80rem'),
239
+ utility('max-w-full', max_width='100%'),
240
+ utility('container', width='100%', margin_left='auto', margin_right='auto', padding_left='1rem', padding_right='1rem'),
241
+ ]
242
+ for key, value in SPACING.items():
243
+ if key in {'auto', 'full'}:
244
+ continue
245
+ rules.append(utility(f'w-{key}', width=value))
246
+ rules.append(utility(f'h-{key}', height=value))
247
+ return rules
248
+
249
+
250
+ def _typography_utils() -> list[CSSClass]:
251
+ rules = [
252
+ utility('font-sans', font_family='system-ui, -apple-system, Segoe UI, Roboto, sans-serif'),
253
+ utility('font-mono', font_family='ui-monospace, SFMono-Regular, Menlo, monospace'),
254
+ utility('font-thin', font_weight='100'),
255
+ utility('font-light', font_weight='300'),
256
+ utility('font-normal', font_weight='400'),
257
+ utility('font-medium', font_weight='500'),
258
+ utility('font-semibold', font_weight='600'),
259
+ utility('font-bold', font_weight='700'),
260
+ utility('font-extrabold', font_weight='800'),
261
+ utility('italic', font_style='italic'),
262
+ utility('not-italic', font_style='normal'),
263
+ utility('text-left', text_align='left'),
264
+ utility('text-center', text_align='center'),
265
+ utility('text-right', text_align='right'),
266
+ utility('underline', text_decoration_line='underline'),
267
+ utility('no-underline', text_decoration_line='none'),
268
+ utility('uppercase', text_transform='uppercase'),
269
+ utility('lowercase', text_transform='lowercase'),
270
+ utility('capitalize', text_transform='capitalize'),
271
+ utility('truncate', overflow='hidden', text_overflow='ellipsis', white_space='nowrap'),
272
+ utility('whitespace-nowrap', white_space='nowrap'),
273
+ utility('leading-none', line_height='1'),
274
+ utility('leading-tight', line_height='1.25'),
275
+ utility('leading-normal', line_height='1.5'),
276
+ utility('leading-relaxed', line_height='1.625'),
277
+ ]
278
+ for key, (size, leading) in FONT_SIZES.items():
279
+ rules.append(utility(f'text-{key}', font_size=size, line_height=leading))
280
+ for key, value in COLORS.items():
281
+ rules.append(utility(f'text-{key}', color=value))
282
+ return rules
283
+
284
+
285
+ def _color_border_utils() -> list[CSSClass]:
286
+ rules = [
287
+ utility('border', border_width='1px', border_style='solid', border_color='#e5e7eb'),
288
+ utility('border-0', border_width='0'),
289
+ utility('border-2', border_width='2px', border_style='solid'),
290
+ utility('border-t', border_top_width='1px', border_top_style='solid'),
291
+ utility('border-b', border_bottom_width='1px', border_bottom_style='solid'),
292
+ utility('shadow-sm', box_shadow='0 1px 2px 0 rgb(0 0 0 / 0.05)'),
293
+ utility('shadow', box_shadow='0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1)'),
294
+ utility('shadow-md', box_shadow='0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1)'),
295
+ utility('shadow-lg', box_shadow='0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1)'),
296
+ utility('shadow-none', box_shadow='none'),
297
+ utility('opacity-0', opacity='0'),
298
+ utility('opacity-50', opacity='0.5'),
299
+ utility('opacity-75', opacity='0.75'),
300
+ utility('opacity-100', opacity='1'),
301
+ ]
302
+ for key, value in ROUNDED.items():
303
+ name = 'rounded' if key == '' else f'rounded-{key}'
304
+ rules.append(utility(name, border_radius=value))
305
+ for key, value in COLORS.items():
306
+ rules.append(utility(f'bg-{key}', background_color=value))
307
+ rules.append(utility(f'border-{key}', border_color=value))
308
+ return rules
309
+
310
+
311
+ def _position_utils() -> list[CSSClass]:
312
+ rules = [
313
+ utility('static', position='static'),
314
+ utility('fixed', position='fixed'),
315
+ utility('absolute', position='absolute'),
316
+ utility('relative', position='relative'),
317
+ utility('sticky', position='sticky'),
318
+ utility('inset-0', top='0', right='0', bottom='0', left='0'),
319
+ utility('top-0', top='0'),
320
+ utility('right-0', right='0'),
321
+ utility('bottom-0', bottom='0'),
322
+ utility('left-0', left='0'),
323
+ utility('z-0', z_index='0'),
324
+ utility('z-10', z_index='10'),
325
+ utility('z-20', z_index='20'),
326
+ utility('z-30', z_index='30'),
327
+ utility('z-40', z_index='40'),
328
+ utility('z-50', z_index='50'),
329
+ utility('overflow-hidden', overflow='hidden'),
330
+ utility('overflow-auto', overflow='auto'),
331
+ utility('overflow-x-auto', overflow_x='auto'),
332
+ utility('overflow-y-auto', overflow_y='auto'),
333
+ utility('object-cover', object_fit='cover'),
334
+ utility('object-contain', object_fit='contain'),
335
+ utility('pointer-events-none', pointer_events='none'),
336
+ utility('cursor-pointer', cursor='pointer'),
337
+ utility('select-none', user_select='none'),
338
+ utility('sr-only', position='absolute', width='1px', height='1px', padding='0', margin='-1px', overflow='hidden', clip='rect(0, 0, 0, 0)', white_space='nowrap', border_width='0'),
339
+ ]
340
+ return rules
341
+
342
+
343
+ class TailwindUtilities(SCSSRoot):
344
+ """Pełny zestaw klas utility w stylu TailwindCSS."""
345
+
346
+ name: str = 'tailwind_utilities'
347
+
348
+ def __init__(self, include: Iterable[str] | None = None):
349
+ groups = {
350
+ 'spacing': _spacing_utils,
351
+ 'layout': _layout_utils,
352
+ 'sizing': _size_utils,
353
+ 'typography': _typography_utils,
354
+ 'color': _color_border_utils,
355
+ 'position': _position_utils,
356
+ }
357
+ selected = set(include) if include is not None else set(groups)
358
+ rules: list[Any] = []
359
+ for key, factory in groups.items():
360
+ if key in selected:
361
+ rules.extend(factory())
362
+ super().__init__(*rules)
363
+
364
+
365
+ class Tailwind(SCSSRoot):
366
+ """Arkusz Tailwind-like: zmienne + preflight + utilities.
367
+
368
+ Użycie::
369
+
370
+ sheet = Tailwind()
371
+ Div(class_=tw('flex', 'items-center', 'gap-4', 'p-4', 'bg-white'))
372
+ """
373
+
374
+ name: str = 'tailwind'
375
+
376
+ def __init__(self, *, utilities: Iterable[str] | None = None, base: bool = True):
377
+ parts: list[Any] = [TailwindVariables()]
378
+ if base:
379
+ parts.append(TailwindPreflight())
380
+ parts.append(TailwindUtilities(include=utilities))
381
+ super().__init__(*parts)
382
+
383
+
384
+ # Alias historyczny / wygodny
385
+ TailwindBase = TailwindPreflight
386
+
387
+
388
+ __all__ = [
389
+ 'SPACING', 'COLORS', 'FONT_SIZES', 'ROUNDED',
390
+ 'utility', 'tw',
391
+ 'TailwindVariables', 'TailwindPreflight', 'TailwindBase', 'TailwindUtilities', 'Tailwind',
392
+ ]
transcript/project.py ADDED
@@ -0,0 +1,223 @@
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+ from typing import Any, Iterable, Literal
5
+
6
+ from transcript.builders.js import JS, JSModule
7
+ from transcript.builders.scss import SCSS, SCSSRoot
8
+ from transcript.core.config import Config, init
9
+ from transcript.core.container import ContainerBase
10
+ from transcript.io.file import File
11
+ from transcript.purge import (
12
+ extract_classes_from_containers,
13
+ extract_classes_from_paths,
14
+ purge_scss,
15
+ )
16
+
17
+ ArtifactKind = Literal['template', 'style', 'script', 'graphic', 'auto']
18
+
19
+
20
+ class Project:
21
+ """Projekt frontendowy: katalogi templates/static oraz artefakty do generacji.
22
+
23
+ Przykład::
24
+
25
+ project = Project('myapp')
26
+ project.add('index.html', page)
27
+ project.add('app.scss', Tailwind())
28
+ project.add('app.js', script)
29
+ written = project.generate()
30
+ """
31
+
32
+ def __init__(
33
+ self,
34
+ root: str | Path = '.',
35
+ *,
36
+ templates_dir: str | Path = 'templates',
37
+ static_dir: str | Path = 'static',
38
+ css_dir: str | Path | None = None,
39
+ js_dir: str | Path | None = None,
40
+ graphics_dir: str | Path | None = None,
41
+ scss_dir: str | Path | None = None,
42
+ purge: bool = True,
43
+ safelist: Iterable[str] = (),
44
+ register_config: bool = True,
45
+ ):
46
+ self.root = Path(root)
47
+ self.templates_dir = self._resolve(templates_dir)
48
+ self.static_dir = self._resolve(static_dir)
49
+ self.css_dir = self._resolve(css_dir) if css_dir is not None else self.static_dir / 'css'
50
+ self.js_dir = self._resolve(js_dir) if js_dir is not None else self.static_dir / 'js'
51
+ self.graphics_dir = (
52
+ self._resolve(graphics_dir) if graphics_dir is not None else self.static_dir / 'graphics'
53
+ )
54
+ self.scss_dir = self._resolve(scss_dir) if scss_dir is not None else self.static_dir / 'scss'
55
+ self.purge = purge
56
+ self.safelist = set(safelist)
57
+
58
+ self.templates: dict[str, ContainerBase] = {}
59
+ self.styles: dict[str, ContainerBase] = {}
60
+ self.scripts: dict[str, ContainerBase] = {}
61
+ self.graphics: dict[str, Path | bytes | str] = {}
62
+
63
+ if register_config:
64
+ init(
65
+ Config(
66
+ templates_dir=str(self.templates_dir),
67
+ scss_dir=str(self.scss_dir),
68
+ js_dir=str(self.js_dir),
69
+ static_dir=str(self.static_dir),
70
+ css_dir=str(self.css_dir),
71
+ graphics_dir=str(self.graphics_dir),
72
+ )
73
+ )
74
+
75
+ def _resolve(self, path: str | Path) -> Path:
76
+ candidate = Path(path)
77
+ if candidate.is_absolute():
78
+ return candidate
79
+ return (self.root / candidate).resolve()
80
+
81
+ def ensure_dirs(self) -> None:
82
+ for path in (
83
+ self.templates_dir,
84
+ self.static_dir,
85
+ self.css_dir,
86
+ self.js_dir,
87
+ self.graphics_dir,
88
+ self.scss_dir,
89
+ ):
90
+ path.mkdir(parents=True, exist_ok=True)
91
+
92
+ def _detect_kind(self, name: str, container: Any) -> ArtifactKind:
93
+ lower = name.lower()
94
+ if lower.endswith(('.html', '.htm', '.jinja', '.j2')):
95
+ return 'template'
96
+ if lower.endswith(('.scss', '.sass', '.css')):
97
+ return 'style'
98
+ if lower.endswith(('.js', '.mjs', '.cjs', '.ts')):
99
+ return 'script'
100
+ if lower.endswith(('.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp', '.ico')):
101
+ return 'graphic'
102
+ if isinstance(container, (SCSS, SCSSRoot)):
103
+ return 'style'
104
+ if isinstance(container, (JS, JSModule)):
105
+ return 'script'
106
+ if isinstance(container, ContainerBase):
107
+ return 'template'
108
+ return 'auto'
109
+
110
+ def add(
111
+ self,
112
+ name: str,
113
+ artifact: ContainerBase | Path | bytes | str,
114
+ *,
115
+ kind: ArtifactKind = 'auto',
116
+ ) -> Project:
117
+ """Rejestruje artefakt pod nazwą pliku docelowego."""
118
+ resolved_kind = kind if kind != 'auto' else self._detect_kind(name, artifact)
119
+ if resolved_kind == 'template':
120
+ if not isinstance(artifact, ContainerBase):
121
+ raise TypeError('Szablon musi być kontenerem')
122
+ self.templates[name] = artifact
123
+ elif resolved_kind == 'style':
124
+ if not isinstance(artifact, ContainerBase):
125
+ raise TypeError('Styl musi być kontenerem SCSS/CSS')
126
+ self.styles[name] = artifact
127
+ elif resolved_kind == 'script':
128
+ if not isinstance(artifact, ContainerBase):
129
+ raise TypeError('Skrypt musi być kontenerem JS')
130
+ self.scripts[name] = artifact
131
+ elif resolved_kind == 'graphic':
132
+ self.graphics[name] = artifact # type: ignore[assignment]
133
+ else:
134
+ raise ValueError(f'Nie udało się ustalić rodzaju artefaktu: {name!r}')
135
+ return self
136
+
137
+ def __lshift__(self, item: Any) -> Project:
138
+ """``project << ('index.html', page)`` lub ``project << page`` (auto-nazwa)."""
139
+ if isinstance(item, tuple) and len(item) == 2:
140
+ name, artifact = item
141
+ return self.add(str(name), artifact)
142
+ if isinstance(item, ContainerBase):
143
+ name = getattr(item, 'name', None) or item.__class__.__name__.lower()
144
+ if isinstance(item, (SCSS, SCSSRoot)):
145
+ return self.add(f'{name}.scss', item, kind='style')
146
+ if isinstance(item, (JS, JSModule)):
147
+ return self.add(f'{name}.js', item, kind='script')
148
+ return self.add(f'{name}.html', item, kind='template')
149
+ raise TypeError('Project << oczekuje (nazwa, artefakt) albo kontenera')
150
+
151
+ def collect_used_classes(self) -> set[str]:
152
+ used = extract_classes_from_containers(self.templates.values())
153
+ used |= extract_classes_from_containers(self.scripts.values())
154
+ used |= extract_classes_from_paths(
155
+ [self.templates_dir, self.js_dir],
156
+ )
157
+ # style same w sobie nie oznaczają użycia — tylko content
158
+ return used
159
+
160
+ def _style_output_path(self, name: str) -> Path:
161
+ lower = name.lower()
162
+ if lower.endswith('.scss') or lower.endswith('.sass'):
163
+ return self.scss_dir / name
164
+ return self.css_dir / name
165
+
166
+ def _write_graphic(self, name: str, payload: Path | bytes | str) -> Path:
167
+ target = self.graphics_dir / name
168
+ target.parent.mkdir(parents=True, exist_ok=True)
169
+ if isinstance(payload, Path):
170
+ target.write_bytes(payload.read_bytes())
171
+ elif isinstance(payload, bytes):
172
+ target.write_bytes(payload)
173
+ else:
174
+ source = Path(payload)
175
+ if source.is_file():
176
+ target.write_bytes(source.read_bytes())
177
+ else:
178
+ target.write_text(str(payload), encoding='utf-8')
179
+ return target
180
+
181
+ def generate(self, *, purge: bool | None = None) -> dict[str, Path]:
182
+ """Generuje wszystkie zarejestrowane pliki w katalogach projektu.
183
+
184
+ Przy ``purge=True`` (domyślnie) finalny SCSS/CSS utility jest
185
+ odfiltrowany z nieużywanych klas — jak w TailwindCSS.
186
+ """
187
+ self.ensure_dirs()
188
+ do_purge = self.purge if purge is None else purge
189
+ used = self.collect_used_classes() if do_purge else set()
190
+ written: dict[str, Path] = {}
191
+
192
+ for name, container in self.templates.items():
193
+ path = self.templates_dir / name
194
+ container >> File(path)
195
+ written[name] = path
196
+
197
+ for name, container in self.styles.items():
198
+ path = self._style_output_path(name)
199
+ source = container
200
+ if do_purge:
201
+ source = purge_scss(container, used, safelist=self.safelist)
202
+ source >> File(path)
203
+ written[name] = path
204
+
205
+ for name, container in self.scripts.items():
206
+ path = self.js_dir / name
207
+ container >> File(path)
208
+ written[name] = path
209
+
210
+ for name, payload in self.graphics.items():
211
+ written[name] = self._write_graphic(name, payload)
212
+
213
+ return written
214
+
215
+ def __repr__(self) -> str:
216
+ return (
217
+ f'{self.__class__.__name__}(root={str(self.root)!r}, '
218
+ f'templates={len(self.templates)}, styles={len(self.styles)}, '
219
+ f'scripts={len(self.scripts)}, graphics={len(self.graphics)})'
220
+ )
221
+
222
+
223
+ __all__ = ['Project']