deckkit 0.1.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.
- deckkit/__init__.py +12 -0
- deckkit/_bundled_fonts.py +54 -0
- deckkit/_diagnostics.py +48 -0
- deckkit/_font_assets/SOURCES.txt +9 -0
- deckkit/_font_assets/SourceCodePro-Bold.ttf +0 -0
- deckkit/_font_assets/SourceCodePro-BoldIt.ttf +0 -0
- deckkit/_font_assets/SourceCodePro-It.ttf +0 -0
- deckkit/_font_assets/SourceCodePro-LICENSE.txt +93 -0
- deckkit/_font_assets/SourceCodePro-Regular.ttf +0 -0
- deckkit/_font_assets/SourceSans3-Bold.ttf +0 -0
- deckkit/_font_assets/SourceSans3-BoldIt.ttf +0 -0
- deckkit/_font_assets/SourceSans3-It.ttf +0 -0
- deckkit/_font_assets/SourceSans3-LICENSE.txt +93 -0
- deckkit/_font_assets/SourceSans3-Regular.ttf +0 -0
- deckkit/_variable_evaluation.py +82 -0
- deckkit/animation.py +211 -0
- deckkit/annotations.py +161 -0
- deckkit/backends/__init__.py +1 -0
- deckkit/backends/_font_embedding.py +41 -0
- deckkit/backends/_pptx_fonts.py +230 -0
- deckkit/backends/_pptx_shadows.py +241 -0
- deckkit/backends/_pptx_tables.py +251 -0
- deckkit/backends/pptx.py +1225 -0
- deckkit/backends/pptx_animation.py +175 -0
- deckkit/backends/svg.py +527 -0
- deckkit/canvas/__init__.py +40 -0
- deckkit/canvas/_geometry.py +49 -0
- deckkit/canvas/clipping.py +336 -0
- deckkit/canvas/context.py +34 -0
- deckkit/canvas/elements.py +306 -0
- deckkit/canvas/path.py +250 -0
- deckkit/canvas/scene.py +74 -0
- deckkit/canvas/style.py +31 -0
- deckkit/charts/__init__.py +59 -0
- deckkit/charts/axes.py +249 -0
- deckkit/charts/chart.py +244 -0
- deckkit/charts/content.py +132 -0
- deckkit/charts/context.py +45 -0
- deckkit/charts/interval.py +166 -0
- deckkit/charts/legends.py +82 -0
- deckkit/charts/marks.py +410 -0
- deckkit/charts/scales.py +205 -0
- deckkit/citations.py +200 -0
- deckkit/compiler.py +410 -0
- deckkit/context.py +72 -0
- deckkit/deck.py +168 -0
- deckkit/drawables/__init__.py +42 -0
- deckkit/drawables/bounds.py +17 -0
- deckkit/drawables/connector.py +22 -0
- deckkit/drawables/drawable.py +38 -0
- deckkit/drawables/ellipse.py +21 -0
- deckkit/drawables/group.py +10 -0
- deckkit/drawables/image.py +14 -0
- deckkit/drawables/line.py +18 -0
- deckkit/drawables/path.py +78 -0
- deckkit/drawables/rectangle.py +24 -0
- deckkit/drawables/rich_text.py +176 -0
- deckkit/drawables/table.py +42 -0
- deckkit/drawables/text.py +29 -0
- deckkit/effects.py +84 -0
- deckkit/expanded_views.py +561 -0
- deckkit/fonts.py +329 -0
- deckkit/geometry.py +48 -0
- deckkit/identity.py +72 -0
- deckkit/layout.py +398 -0
- deckkit/layouts/__init__.py +20 -0
- deckkit/layouts/_content.py +27 -0
- deckkit/layouts/annotations.py +217 -0
- deckkit/layouts/containers.py +202 -0
- deckkit/layouts/grid.py +299 -0
- deckkit/layouts/modifiers.py +200 -0
- deckkit/layouts/primitives.py +213 -0
- deckkit/layouts/table.py +139 -0
- deckkit/measurement.py +420 -0
- deckkit/paint.py +73 -0
- deckkit/py.typed +0 -0
- deckkit/reconcile.py +119 -0
- deckkit/semantic.py +47 -0
- deckkit/theme.py +364 -0
- deckkit/themes/__init__.py +91 -0
- deckkit/themes/clarity.py +98 -0
- deckkit/tokens.py +226 -0
- deckkit/units.py +73 -0
- deckkit/variables.py +212 -0
- deckkit/view_expansion.py +185 -0
- deckkit/views/__init__.py +100 -0
- deckkit/views/_code_source.py +42 -0
- deckkit/views/_drawing.py +52 -0
- deckkit/views/canvas.py +27 -0
- deckkit/views/code.py +481 -0
- deckkit/views/code_evolution.py +284 -0
- deckkit/views/composite_view.py +19 -0
- deckkit/views/divider.py +38 -0
- deckkit/views/ellipse.py +38 -0
- deckkit/views/grid.py +156 -0
- deckkit/views/hstack.py +43 -0
- deckkit/views/image.py +43 -0
- deckkit/views/layout_view.py +30 -0
- deckkit/views/line.py +31 -0
- deckkit/views/modifier_views.py +281 -0
- deckkit/views/path.py +36 -0
- deckkit/views/primitive_view.py +16 -0
- deckkit/views/pygments_highlighter.py +67 -0
- deckkit/views/rectangle.py +61 -0
- deckkit/views/section_number.py +21 -0
- deckkit/views/semantic/__init__.py +51 -0
- deckkit/views/semantic/before_after.py +109 -0
- deckkit/views/semantic/bullet_graph.py +216 -0
- deckkit/views/semantic/card.py +129 -0
- deckkit/views/semantic/code_block.py +41 -0
- deckkit/views/semantic/diff.py +642 -0
- deckkit/views/semantic/estimate_interval.py +137 -0
- deckkit/views/semantic/figure.py +55 -0
- deckkit/views/semantic/item_list.py +171 -0
- deckkit/views/semantic/metric.py +74 -0
- deckkit/views/semantic/pill.py +69 -0
- deckkit/views/semantic/pipeline.py +94 -0
- deckkit/views/semantic/pipeline_stage.py +59 -0
- deckkit/views/semantic/semantic_view.py +76 -0
- deckkit/views/semantic/sparkline.py +164 -0
- deckkit/views/semantic/table.py +439 -0
- deckkit/views/semantic/terminal.py +361 -0
- deckkit/views/semantic/timeline.py +158 -0
- deckkit/views/slide.py +37 -0
- deckkit/views/slide_number.py +20 -0
- deckkit/views/slides/__init__.py +16 -0
- deckkit/views/slides/section_slide.py +89 -0
- deckkit/views/slides/slide_margins.py +32 -0
- deckkit/views/slides/slide_with_margins.py +22 -0
- deckkit/views/slides/title_and_content.py +49 -0
- deckkit/views/slides/title_and_content_slide.py +55 -0
- deckkit/views/spacer.py +45 -0
- deckkit/views/stack.py +36 -0
- deckkit/views/text.py +142 -0
- deckkit/views/view.py +229 -0
- deckkit/views/vstack.py +31 -0
- deckkit/views/zstack.py +27 -0
- deckkit-0.1.0.dist-info/METADATA +123 -0
- deckkit-0.1.0.dist-info/RECORD +144 -0
- deckkit-0.1.0.dist-info/WHEEL +5 -0
- deckkit-0.1.0.dist-info/licenses/LICENSE +21 -0
- deckkit-0.1.0.dist-info/licenses/src/deckkit/_font_assets/SourceCodePro-LICENSE.txt +93 -0
- deckkit-0.1.0.dist-info/licenses/src/deckkit/_font_assets/SourceSans3-LICENSE.txt +93 -0
- deckkit-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
"""Structural views produced by layout modifiers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from enum import Enum
|
|
7
|
+
from typing import ClassVar, Union
|
|
8
|
+
|
|
9
|
+
from ..geometry import RelativePoint
|
|
10
|
+
from ..layout import Layout
|
|
11
|
+
from ..units import Length, pt, require_length
|
|
12
|
+
from .layout_view import LayoutView
|
|
13
|
+
from .view import View
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class _Unbounded:
|
|
17
|
+
def __repr__(self) -> str:
|
|
18
|
+
return "unbounded"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
unbounded = _Unbounded()
|
|
22
|
+
FrameMaximum = Union[Length, _Unbounded]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class _ContentModifier(LayoutView):
|
|
26
|
+
@property
|
|
27
|
+
def _layout_flex(self) -> float:
|
|
28
|
+
child = self.children[0]
|
|
29
|
+
return super()._layout_flex or (0.0 if child is None else child._layout_flex)
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def _layout_flex_minimum(self) -> tuple[Length, Length] | None:
|
|
33
|
+
child = self.children[0]
|
|
34
|
+
return None if child is None else child._layout_flex_minimum
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def _layout_stretches_cross_axis(self) -> bool:
|
|
38
|
+
child = self.children[0]
|
|
39
|
+
return child is not None and child._layout_stretches_cross_axis
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass(frozen=True, init=False)
|
|
43
|
+
class Layered(_ContentModifier):
|
|
44
|
+
alignment: RelativePoint
|
|
45
|
+
in_front: bool
|
|
46
|
+
|
|
47
|
+
def __init__(
|
|
48
|
+
self,
|
|
49
|
+
content: View,
|
|
50
|
+
decoration: View,
|
|
51
|
+
*,
|
|
52
|
+
alignment: RelativePoint | None,
|
|
53
|
+
in_front: bool,
|
|
54
|
+
) -> None:
|
|
55
|
+
if not isinstance(decoration, View):
|
|
56
|
+
raise TypeError("A background or overlay must be a View")
|
|
57
|
+
if alignment is not None and not isinstance(alignment, RelativePoint):
|
|
58
|
+
raise TypeError("Layer alignment must be a RelativePoint")
|
|
59
|
+
object.__setattr__(
|
|
60
|
+
self, "alignment", RelativePoint.center if alignment is None else alignment
|
|
61
|
+
)
|
|
62
|
+
object.__setattr__(self, "in_front", in_front)
|
|
63
|
+
super().__init__(content, decoration)
|
|
64
|
+
|
|
65
|
+
def resolve_layout(self, lookup: object) -> Layout:
|
|
66
|
+
from ..layouts.modifiers import LayeredLayout
|
|
67
|
+
|
|
68
|
+
return LayeredLayout(self.alignment, self.in_front)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass(frozen=True, init=False)
|
|
72
|
+
class Placed(_ContentModifier):
|
|
73
|
+
x: Length
|
|
74
|
+
y: Length
|
|
75
|
+
anchor: RelativePoint | None
|
|
76
|
+
|
|
77
|
+
def __init__(
|
|
78
|
+
self, content: View, x: Length, y: Length, *, anchor: RelativePoint | None
|
|
79
|
+
) -> None:
|
|
80
|
+
require_length(x)
|
|
81
|
+
require_length(y)
|
|
82
|
+
if anchor is not None and not isinstance(anchor, RelativePoint):
|
|
83
|
+
raise TypeError("Position anchor must be a RelativePoint")
|
|
84
|
+
object.__setattr__(self, "x", x)
|
|
85
|
+
object.__setattr__(self, "y", y)
|
|
86
|
+
object.__setattr__(self, "anchor", anchor)
|
|
87
|
+
super().__init__(content)
|
|
88
|
+
|
|
89
|
+
def resolve_layout(self, lookup: object) -> Layout:
|
|
90
|
+
from ..layouts.modifiers import PlacementLayout
|
|
91
|
+
|
|
92
|
+
return PlacementLayout(self.x, self.y, self.anchor)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class Edge(Enum):
|
|
96
|
+
top = "top"
|
|
97
|
+
leading = "leading"
|
|
98
|
+
bottom = "bottom"
|
|
99
|
+
trailing = "trailing"
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@dataclass(frozen=True)
|
|
103
|
+
class EdgeSet:
|
|
104
|
+
values: frozenset[Edge]
|
|
105
|
+
all: ClassVar[EdgeSet]
|
|
106
|
+
horizontal: ClassVar[EdgeSet]
|
|
107
|
+
vertical: ClassVar[EdgeSet]
|
|
108
|
+
|
|
109
|
+
def __init__(self, *values: Edge) -> None:
|
|
110
|
+
if not all(isinstance(value, Edge) for value in values):
|
|
111
|
+
raise TypeError("EdgeSet values must be Edge values")
|
|
112
|
+
object.__setattr__(self, "values", frozenset(values))
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
EdgeSet.all = EdgeSet(*Edge)
|
|
116
|
+
EdgeSet.horizontal = EdgeSet(Edge.leading, Edge.trailing)
|
|
117
|
+
EdgeSet.vertical = EdgeSet(Edge.top, Edge.bottom)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
@dataclass(frozen=True)
|
|
121
|
+
class EdgeInsets:
|
|
122
|
+
"""Independent padding lengths for the four edges."""
|
|
123
|
+
|
|
124
|
+
top: Length = pt(0)
|
|
125
|
+
leading: Length = pt(0)
|
|
126
|
+
bottom: Length = pt(0)
|
|
127
|
+
trailing: Length = pt(0)
|
|
128
|
+
|
|
129
|
+
def __post_init__(self) -> None:
|
|
130
|
+
for value in (self.top, self.leading, self.bottom, self.trailing):
|
|
131
|
+
require_length(value)
|
|
132
|
+
if value < pt(0):
|
|
133
|
+
raise ValueError("Padding must be nonnegative")
|
|
134
|
+
|
|
135
|
+
@property
|
|
136
|
+
def horizontal(self) -> Length:
|
|
137
|
+
return self.leading + self.trailing
|
|
138
|
+
|
|
139
|
+
@property
|
|
140
|
+
def vertical(self) -> Length:
|
|
141
|
+
return self.top + self.bottom
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _padding_insets(amount: Length | EdgeInsets, edges: EdgeSet | None) -> EdgeInsets:
|
|
145
|
+
if isinstance(amount, EdgeInsets):
|
|
146
|
+
if edges is not None:
|
|
147
|
+
raise TypeError("Padding with EdgeInsets does not take an edges argument")
|
|
148
|
+
return amount
|
|
149
|
+
require_length(amount)
|
|
150
|
+
if amount < pt(0):
|
|
151
|
+
raise ValueError("Padding must be nonnegative")
|
|
152
|
+
selected = EdgeSet.all if edges is None else edges
|
|
153
|
+
if not isinstance(selected, EdgeSet):
|
|
154
|
+
raise TypeError("padding edges must be an EdgeSet")
|
|
155
|
+
|
|
156
|
+
def inset(edge: Edge) -> Length:
|
|
157
|
+
return amount if edge in selected.values else pt(0)
|
|
158
|
+
|
|
159
|
+
return EdgeInsets(
|
|
160
|
+
top=inset(Edge.top),
|
|
161
|
+
leading=inset(Edge.leading),
|
|
162
|
+
bottom=inset(Edge.bottom),
|
|
163
|
+
trailing=inset(Edge.trailing),
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@dataclass(frozen=True)
|
|
168
|
+
class FrameSpec:
|
|
169
|
+
width: Length | None = None
|
|
170
|
+
height: Length | None = None
|
|
171
|
+
min_width: Length | None = None
|
|
172
|
+
ideal_width: Length | None = None
|
|
173
|
+
max_width: FrameMaximum | None = None
|
|
174
|
+
min_height: Length | None = None
|
|
175
|
+
ideal_height: Length | None = None
|
|
176
|
+
max_height: FrameMaximum | None = None
|
|
177
|
+
alignment: RelativePoint = RelativePoint.center
|
|
178
|
+
|
|
179
|
+
def __post_init__(self) -> None:
|
|
180
|
+
if not isinstance(self.alignment, RelativePoint):
|
|
181
|
+
raise TypeError("Frame alignment must be a RelativePoint")
|
|
182
|
+
self._validate_axis(
|
|
183
|
+
"width", self.width, self.min_width, self.ideal_width, self.max_width
|
|
184
|
+
)
|
|
185
|
+
self._validate_axis(
|
|
186
|
+
"height", self.height, self.min_height, self.ideal_height, self.max_height
|
|
187
|
+
)
|
|
188
|
+
if all(
|
|
189
|
+
value is None
|
|
190
|
+
for value in (
|
|
191
|
+
self.width,
|
|
192
|
+
self.height,
|
|
193
|
+
self.min_width,
|
|
194
|
+
self.ideal_width,
|
|
195
|
+
self.max_width,
|
|
196
|
+
self.min_height,
|
|
197
|
+
self.ideal_height,
|
|
198
|
+
self.max_height,
|
|
199
|
+
)
|
|
200
|
+
):
|
|
201
|
+
raise ValueError("frame() requires at least one dimension")
|
|
202
|
+
|
|
203
|
+
@staticmethod
|
|
204
|
+
def _validate_axis(
|
|
205
|
+
name: str,
|
|
206
|
+
exact: Length | None,
|
|
207
|
+
minimum: Length | None,
|
|
208
|
+
ideal: Length | None,
|
|
209
|
+
maximum: FrameMaximum | None,
|
|
210
|
+
) -> None:
|
|
211
|
+
if exact is not None and any(v is not None for v in (minimum, ideal, maximum)):
|
|
212
|
+
raise ValueError(f"Exact {name} cannot be combined with flexible {name}")
|
|
213
|
+
for value in (exact, minimum, ideal):
|
|
214
|
+
if value is not None:
|
|
215
|
+
require_length(value)
|
|
216
|
+
if value < pt(0):
|
|
217
|
+
raise ValueError("Frame dimensions must be nonnegative")
|
|
218
|
+
if maximum is not None and not isinstance(maximum, _Unbounded):
|
|
219
|
+
require_length(maximum)
|
|
220
|
+
if maximum < pt(0):
|
|
221
|
+
raise ValueError("Frame dimensions must be nonnegative")
|
|
222
|
+
if minimum is not None and isinstance(maximum, Length) and minimum > maximum:
|
|
223
|
+
raise ValueError(f"Minimum {name} exceeds maximum {name}")
|
|
224
|
+
if ideal is not None and (
|
|
225
|
+
(minimum is not None and ideal < minimum)
|
|
226
|
+
or (isinstance(maximum, Length) and ideal > maximum)
|
|
227
|
+
):
|
|
228
|
+
raise ValueError(f"Ideal {name} is outside the frame range")
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
@dataclass(frozen=True, init=False)
|
|
232
|
+
class Frame(_ContentModifier):
|
|
233
|
+
spec: FrameSpec
|
|
234
|
+
|
|
235
|
+
def __init__(self, content: View, spec: FrameSpec) -> None:
|
|
236
|
+
object.__setattr__(self, "spec", spec)
|
|
237
|
+
super().__init__(content)
|
|
238
|
+
|
|
239
|
+
@property
|
|
240
|
+
def _layout_flex_minimum(self) -> tuple[Length, Length] | None:
|
|
241
|
+
from ..layouts.modifiers import _clamp
|
|
242
|
+
|
|
243
|
+
base = super()._layout_flex_minimum
|
|
244
|
+
if base is None:
|
|
245
|
+
return None
|
|
246
|
+
s = self.spec
|
|
247
|
+
return (
|
|
248
|
+
s.width or _clamp(base[0], s.min_width, s.max_width),
|
|
249
|
+
s.height or _clamp(base[1], s.min_height, s.max_height),
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
def resolve_layout(self, lookup: object) -> Layout:
|
|
253
|
+
from ..layouts import FrameLayout
|
|
254
|
+
|
|
255
|
+
return FrameLayout(self.spec)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
@dataclass(frozen=True, init=False)
|
|
259
|
+
class Padding(_ContentModifier):
|
|
260
|
+
insets: EdgeInsets
|
|
261
|
+
|
|
262
|
+
def __init__(
|
|
263
|
+
self, content: View, amount: Length | EdgeInsets, edges: EdgeSet | None = None
|
|
264
|
+
) -> None:
|
|
265
|
+
object.__setattr__(self, "insets", _padding_insets(amount, edges))
|
|
266
|
+
super().__init__(content)
|
|
267
|
+
|
|
268
|
+
@property
|
|
269
|
+
def _layout_flex_minimum(self) -> tuple[Length, Length] | None:
|
|
270
|
+
base = super()._layout_flex_minimum
|
|
271
|
+
if base is None:
|
|
272
|
+
return None
|
|
273
|
+
return (
|
|
274
|
+
base[0] + self.insets.horizontal,
|
|
275
|
+
base[1] + self.insets.vertical,
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
def resolve_layout(self, lookup: object) -> Layout:
|
|
279
|
+
from ..layouts import PaddingLayout
|
|
280
|
+
|
|
281
|
+
return PaddingLayout(self.insets)
|
deckkit/views/path.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
|
|
5
|
+
from ..canvas import CanvasPathCommand, Stroke
|
|
6
|
+
from ..canvas.path import Path as CanvasPath
|
|
7
|
+
from ..context import Context
|
|
8
|
+
from ..expanded_views import ExpandedDrawing, ExpandedMetadata
|
|
9
|
+
from ..theme import ColorStyle
|
|
10
|
+
from ._drawing import expand_drawing
|
|
11
|
+
from .primitive_view import PrimitiveView
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class Path(PrimitiveView):
|
|
16
|
+
"""A path whose supplied commands give it an intrinsic size in points."""
|
|
17
|
+
|
|
18
|
+
commands: tuple[CanvasPathCommand, ...]
|
|
19
|
+
fill: ColorStyle | None = None
|
|
20
|
+
stroke: Stroke | None = field(default_factory=Stroke)
|
|
21
|
+
fill_opacity: float = 1.0
|
|
22
|
+
_element: CanvasPath = field(init=False, repr=False, compare=False)
|
|
23
|
+
|
|
24
|
+
def __post_init__(self) -> None:
|
|
25
|
+
commands = tuple(self.commands)
|
|
26
|
+
if not all(isinstance(command, CanvasPathCommand) for command in commands):
|
|
27
|
+
raise TypeError("Path commands must be CanvasPathCommand values")
|
|
28
|
+
object.__setattr__(self, "commands", commands)
|
|
29
|
+
object.__setattr__(
|
|
30
|
+
self,
|
|
31
|
+
"_element",
|
|
32
|
+
CanvasPath(commands, self.fill, self.stroke, self.fill_opacity),
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
def expand(self, context: Context, metadata: ExpandedMetadata) -> ExpandedDrawing:
|
|
36
|
+
return expand_drawing(self._element, context, metadata)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
from ..context import Context
|
|
6
|
+
from ..expanded_views import ExpandedMetadata, ExpandedPrimitive
|
|
7
|
+
from .view import View
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PrimitiveView(View, ABC):
|
|
11
|
+
"""A rendering leaf that resolves to a typed expanded view."""
|
|
12
|
+
|
|
13
|
+
@abstractmethod
|
|
14
|
+
def expand(self, context: Context, metadata: ExpandedMetadata) -> ExpandedPrimitive:
|
|
15
|
+
"""Resolve this leaf into rendering properties and a layout policy."""
|
|
16
|
+
raise NotImplementedError
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""Optional Pygments adapter for code spans."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from re import finditer
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from .code import CodeLine, CodeSpan
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _scope(token: object) -> str | None:
|
|
12
|
+
value = str(token)
|
|
13
|
+
if value == "Token":
|
|
14
|
+
return None
|
|
15
|
+
prefix = "Token."
|
|
16
|
+
return value[len(prefix) :].lower() if value.startswith(prefix) else value.lower()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class PygmentsHighlighter:
|
|
20
|
+
"""Produce CodeLine values from any installed Pygments lexer."""
|
|
21
|
+
|
|
22
|
+
def highlight(self, source: str, language: str) -> tuple[CodeLine, ...]:
|
|
23
|
+
try:
|
|
24
|
+
from pygments import lex # type: ignore[import-untyped]
|
|
25
|
+
from pygments.lexers import ( # type: ignore[import-untyped]
|
|
26
|
+
get_lexer_by_name,
|
|
27
|
+
)
|
|
28
|
+
from pygments.util import ClassNotFound # type: ignore[import-untyped]
|
|
29
|
+
except ImportError as error:
|
|
30
|
+
raise ImportError(
|
|
31
|
+
"Automatic code highlighting requires Pygments; "
|
|
32
|
+
"install deckkit[highlight]"
|
|
33
|
+
) from error
|
|
34
|
+
|
|
35
|
+
try:
|
|
36
|
+
lexer = get_lexer_by_name(
|
|
37
|
+
language,
|
|
38
|
+
stripnl=False,
|
|
39
|
+
stripall=False,
|
|
40
|
+
ensurenl=False,
|
|
41
|
+
tabsize=0,
|
|
42
|
+
)
|
|
43
|
+
except ClassNotFound as error:
|
|
44
|
+
raise ValueError(f"Unknown code language {language!r}") from error
|
|
45
|
+
|
|
46
|
+
lines: list[list[CodeSpan]] = [[]]
|
|
47
|
+
|
|
48
|
+
def append(text: str, token: Any) -> None:
|
|
49
|
+
if not text:
|
|
50
|
+
return
|
|
51
|
+
scope = _scope(token)
|
|
52
|
+
current = lines[-1]
|
|
53
|
+
if current and current[-1].token == scope:
|
|
54
|
+
previous = current[-1]
|
|
55
|
+
current[-1] = CodeSpan(previous.text + text, scope)
|
|
56
|
+
else:
|
|
57
|
+
current.append(CodeSpan(text, scope))
|
|
58
|
+
|
|
59
|
+
for token, value in lex(source, lexer):
|
|
60
|
+
start = 0
|
|
61
|
+
for newline in finditer(r"\r\n?|\n", value):
|
|
62
|
+
append(value[start : newline.start()], token)
|
|
63
|
+
lines.append([])
|
|
64
|
+
start = newline.end()
|
|
65
|
+
append(value[start:], token)
|
|
66
|
+
|
|
67
|
+
return tuple(CodeLine(*line) for line in lines)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
from ..canvas import Stroke
|
|
6
|
+
from ..canvas._geometry import validate_opacity
|
|
7
|
+
from ..context import Context
|
|
8
|
+
from ..expanded_views import ExpandedMetadata, ExpandedRectangle
|
|
9
|
+
from ..layouts import ShapeLayout
|
|
10
|
+
from ..paint import LinearGradient
|
|
11
|
+
from ..theme import Color, ColorStyle, ForegroundColor
|
|
12
|
+
from ..units import Length, pt, require_length
|
|
13
|
+
from .primitive_view import PrimitiveView
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class Rectangle(PrimitiveView):
|
|
18
|
+
"""A filled rectangle with an optional stroke. Use zero fill opacity for an outline."""
|
|
19
|
+
|
|
20
|
+
corner_radius: Length = pt(0)
|
|
21
|
+
stroke: Stroke | None = None
|
|
22
|
+
fill_opacity: float = 1.0
|
|
23
|
+
fill: ColorStyle | LinearGradient | None = None
|
|
24
|
+
|
|
25
|
+
def __post_init__(self) -> None:
|
|
26
|
+
require_length(self.corner_radius)
|
|
27
|
+
if self.corner_radius.points < 0:
|
|
28
|
+
raise ValueError("Rectangle corner radius must be non-negative")
|
|
29
|
+
validate_opacity(self.fill_opacity)
|
|
30
|
+
|
|
31
|
+
def expand(self, context: Context, metadata: ExpandedMetadata) -> ExpandedRectangle:
|
|
32
|
+
stroke = self.stroke
|
|
33
|
+
stroke_color = None
|
|
34
|
+
if stroke is not None:
|
|
35
|
+
style = stroke.style
|
|
36
|
+
stroke_color = (
|
|
37
|
+
style if isinstance(style, Color) else context.environment[style]
|
|
38
|
+
).hex
|
|
39
|
+
return ExpandedRectangle(
|
|
40
|
+
metadata,
|
|
41
|
+
ShapeLayout(),
|
|
42
|
+
self._fill_color(context).hex,
|
|
43
|
+
self.corner_radius,
|
|
44
|
+
stroke_color,
|
|
45
|
+
stroke.width if stroke is not None else pt(0),
|
|
46
|
+
stroke.dash if stroke is not None else (),
|
|
47
|
+
self.fill_opacity,
|
|
48
|
+
stroke.opacity if stroke is not None else 1.0,
|
|
49
|
+
stroke.line_cap if stroke is not None else "butt",
|
|
50
|
+
stroke.line_join if stroke is not None else "miter",
|
|
51
|
+
self.fill.resolve(context.environment)
|
|
52
|
+
if isinstance(self.fill, LinearGradient)
|
|
53
|
+
else None,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
def _fill_color(self, context: Context) -> Color:
|
|
57
|
+
if isinstance(self.fill, Color):
|
|
58
|
+
return self.fill
|
|
59
|
+
if self.fill is None or isinstance(self.fill, LinearGradient):
|
|
60
|
+
return context.environment[ForegroundColor]
|
|
61
|
+
return context.environment[self.fill]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
from ..context import Context
|
|
4
|
+
from ..expanded_views import ExpandedMetadata, ExpandedPrimitive
|
|
5
|
+
from .primitive_view import PrimitiveView
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True)
|
|
9
|
+
class SectionNumber(PrimitiveView):
|
|
10
|
+
"""Display the active section number, with an optional format specifier."""
|
|
11
|
+
|
|
12
|
+
format: str = ""
|
|
13
|
+
|
|
14
|
+
def expand(self, context: Context, metadata: ExpandedMetadata) -> ExpandedPrimitive:
|
|
15
|
+
from ..variables import _deferred_text, _format, section_count
|
|
16
|
+
|
|
17
|
+
return _deferred_text(
|
|
18
|
+
context,
|
|
19
|
+
metadata,
|
|
20
|
+
lambda values: _format(values.value(section_count), self.format),
|
|
21
|
+
)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Semantic presentation components with overridable theme recipes."""
|
|
2
|
+
|
|
3
|
+
from .before_after import BeforeAfter
|
|
4
|
+
from .bullet_graph import BulletBand, BulletGraph
|
|
5
|
+
from .card import Card
|
|
6
|
+
from .code_block import CodeBlock
|
|
7
|
+
from .diff import Diff, DiffHunk, DiffLine
|
|
8
|
+
from .estimate_interval import EstimateInterval
|
|
9
|
+
from .figure import Figure
|
|
10
|
+
from .item_list import ItemList, ListItem
|
|
11
|
+
from .metric import Metric
|
|
12
|
+
from .pill import Pill
|
|
13
|
+
from .pipeline import Pipeline
|
|
14
|
+
from .pipeline_stage import PipelineStage
|
|
15
|
+
from .semantic_view import SemanticView, StyledSemanticView
|
|
16
|
+
from .sparkline import Sparkline
|
|
17
|
+
from .table import CellStyle, ColumnWidth, RowStyle, Table, TableColumn
|
|
18
|
+
from .terminal import Terminal, TerminalCommand, TerminalOutput
|
|
19
|
+
from .timeline import Timeline, TimelineEvent
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
"BeforeAfter",
|
|
23
|
+
"BulletBand",
|
|
24
|
+
"BulletGraph",
|
|
25
|
+
"Card",
|
|
26
|
+
"CellStyle",
|
|
27
|
+
"CodeBlock",
|
|
28
|
+
"ColumnWidth",
|
|
29
|
+
"Diff",
|
|
30
|
+
"DiffHunk",
|
|
31
|
+
"DiffLine",
|
|
32
|
+
"EstimateInterval",
|
|
33
|
+
"Figure",
|
|
34
|
+
"ItemList",
|
|
35
|
+
"ListItem",
|
|
36
|
+
"Metric",
|
|
37
|
+
"Pill",
|
|
38
|
+
"Pipeline",
|
|
39
|
+
"PipelineStage",
|
|
40
|
+
"RowStyle",
|
|
41
|
+
"SemanticView",
|
|
42
|
+
"Sparkline",
|
|
43
|
+
"StyledSemanticView",
|
|
44
|
+
"Table",
|
|
45
|
+
"TableColumn",
|
|
46
|
+
"Terminal",
|
|
47
|
+
"TerminalCommand",
|
|
48
|
+
"TerminalOutput",
|
|
49
|
+
"Timeline",
|
|
50
|
+
"TimelineEvent",
|
|
51
|
+
]
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""A semantic view for presenting a before-and-after comparison."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
from ...context import Context
|
|
8
|
+
from ...theme import (
|
|
9
|
+
AccentColor,
|
|
10
|
+
CaptionTextStyle,
|
|
11
|
+
Color,
|
|
12
|
+
OnAccentColor,
|
|
13
|
+
PrimaryColor,
|
|
14
|
+
SpacingUnit,
|
|
15
|
+
SurfaceColor,
|
|
16
|
+
)
|
|
17
|
+
from ...tokens import DesignToken, TokenResolver
|
|
18
|
+
from ...units import Length
|
|
19
|
+
from ..hstack import HStack
|
|
20
|
+
from ..text import Text
|
|
21
|
+
from ..view import View
|
|
22
|
+
from ..vstack import VStack
|
|
23
|
+
from .card import Card
|
|
24
|
+
from .pill import Pill
|
|
25
|
+
from .semantic_view import StyledSemanticView
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class BeforeAfter(StyledSemanticView["BeforeAfter.Style"]):
|
|
30
|
+
"""Present two states side by side.
|
|
31
|
+
|
|
32
|
+
Both content slots accept arbitrary :class:`~deckkit.views.View` values,
|
|
33
|
+
so callers can use text, stacks, shapes, or other semantic components. The
|
|
34
|
+
default recipe labels the states ``BEFORE`` and ``AFTER`` and highlights the
|
|
35
|
+
latter with the theme's accent style.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
before: Content describing the initial state.
|
|
39
|
+
after: Content describing the resulting state.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
@dataclass(frozen=True)
|
|
43
|
+
class Style:
|
|
44
|
+
label_width: Length
|
|
45
|
+
label_height: Length
|
|
46
|
+
column_spacing: Length
|
|
47
|
+
before_background: Color
|
|
48
|
+
before_foreground: Color
|
|
49
|
+
after_background: Color
|
|
50
|
+
after_foreground: Color
|
|
51
|
+
|
|
52
|
+
@staticmethod
|
|
53
|
+
def _default_style(values: TokenResolver) -> Style:
|
|
54
|
+
return BeforeAfter.Style(
|
|
55
|
+
values[SpacingUnit] * 22,
|
|
56
|
+
values[SpacingUnit] * 9,
|
|
57
|
+
values[SpacingUnit] * 6,
|
|
58
|
+
values[SurfaceColor],
|
|
59
|
+
values[PrimaryColor],
|
|
60
|
+
values[AccentColor],
|
|
61
|
+
values[OnAccentColor],
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
style_token = DesignToken[Style](
|
|
65
|
+
"component.before-after",
|
|
66
|
+
lambda values: BeforeAfter._default_style(values),
|
|
67
|
+
value_type=Style,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
before: View
|
|
71
|
+
after: View
|
|
72
|
+
|
|
73
|
+
def __post_init__(self) -> None:
|
|
74
|
+
for name in ("before", "after"):
|
|
75
|
+
if not isinstance(getattr(self, name), View):
|
|
76
|
+
raise TypeError(
|
|
77
|
+
f"BeforeAfter.{name} must be a View; wrap text in Text(...)"
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
def body(self, context: Context) -> View:
|
|
81
|
+
"""Arrange the comparison using the default horizontal recipe."""
|
|
82
|
+
style = context.environment[self.style_token]
|
|
83
|
+
before = Card(
|
|
84
|
+
VStack(
|
|
85
|
+
Pill(
|
|
86
|
+
Text("BEFORE").font(CaptionTextStyle),
|
|
87
|
+
)
|
|
88
|
+
.style(
|
|
89
|
+
background=style.before_background,
|
|
90
|
+
foreground=style.before_foreground,
|
|
91
|
+
)
|
|
92
|
+
.frame(width=style.label_width, height=style.label_height),
|
|
93
|
+
self.before,
|
|
94
|
+
)
|
|
95
|
+
).flex()
|
|
96
|
+
after = Card(
|
|
97
|
+
VStack(
|
|
98
|
+
Pill(
|
|
99
|
+
Text("AFTER").font(CaptionTextStyle),
|
|
100
|
+
)
|
|
101
|
+
.style(
|
|
102
|
+
background=style.after_background,
|
|
103
|
+
foreground=style.after_foreground,
|
|
104
|
+
)
|
|
105
|
+
.frame(width=style.label_width, height=style.label_height),
|
|
106
|
+
self.after.foreground_style(AccentColor),
|
|
107
|
+
)
|
|
108
|
+
).flex()
|
|
109
|
+
return HStack(before, after, spacing=style.column_spacing)
|