-
Notifications
You must be signed in to change notification settings - Fork 45
/
common_styles.py
483 lines (392 loc) · 14.7 KB
/
common_styles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# Copyright 2024 The Penzai Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Common styles for parts of renderings."""
from __future__ import annotations
import dataclasses
import io
from typing import Any
from penzai.treescope import html_escaping
from penzai.treescope.foldable_representation import basic_parts
from penzai.treescope.foldable_representation import part_interface
CSSStyleRule = part_interface.CSSStyleRule
JavaScriptDefn = part_interface.JavaScriptDefn
RenderableTreePart = part_interface.RenderableTreePart
HtmlContextForSetup = part_interface.HtmlContextForSetup
class AbbreviationColor(basic_parts.BaseSpanGroup):
"""Renders its child in a color for non-roundtrippable abbreviations."""
def _span_css_class(self) -> str:
return "color_abbrev"
def _span_css_rule(self, context: HtmlContextForSetup) -> CSSStyleRule:
return CSSStyleRule(html_escaping.without_repeated_whitespace("""
.color_abbrev
{
color: #682a00;
}
"""))
class CommentColor(basic_parts.BaseSpanGroup):
"""Renders its child in a color for comments."""
def _span_css_class(self) -> str:
return "color_comment"
def _span_css_rule(self, context: HtmlContextForSetup) -> CSSStyleRule:
return CSSStyleRule(html_escaping.without_repeated_whitespace("""
.color_comment {
color: #aaaaaa;
}
.color_comment a:not(:hover){
color: #aaaaaa;
}
"""))
class ErrorColor(basic_parts.BaseSpanGroup):
"""Renders its child in red to indicate errors / problems during rendering."""
def _span_css_class(self) -> str:
return "color_error"
def _span_css_rule(self, context: HtmlContextForSetup) -> CSSStyleRule:
return CSSStyleRule(html_escaping.without_repeated_whitespace("""
.color_error {
color: red;
}
"""))
class DeferredPlaceholderStyle(basic_parts.BaseSpanGroup):
"""Renders its child in italics to indicate a deferred placeholder."""
def _span_css_class(self) -> str:
return "deferred_placeholder"
def _span_css_rule(self, context: HtmlContextForSetup) -> CSSStyleRule:
return CSSStyleRule(html_escaping.without_repeated_whitespace("""
.deferred_placeholder {
color: #a7a7a7;
font-style: italic;
}
"""))
class CommentColorWhenExpanded(basic_parts.BaseSpanGroup):
"""Renders its child in a color for comments, but only when expanded.
This can be used in combination with another color (usually AbbreviationColor)
to show a summary and then transform it into a comment when clicked, e.g. with
AbbreviationColor(CommentColorWhenExpanded(...))
"""
def _span_css_class(self) -> str:
return "color_comment_when_expanded"
def _span_css_rule(self, context: HtmlContextForSetup) -> CSSStyleRule:
return CSSStyleRule(html_escaping.without_repeated_whitespace(f"""
.color_comment_when_expanded:not({context.collapsed_selector} *) {{
color: #aaaaaa;
}}
"""))
@dataclasses.dataclass(frozen=True)
class CustomTextColor(basic_parts.DeferringToChild):
"""A group that wraps its child in a span with a custom text color.
Attributes:
child: Contents of the group.
color: CSS string defining the color.
"""
child: RenderableTreePart
color: str
def render_to_html(
self,
stream: io.TextIOBase,
*,
at_beginning_of_line: bool = False,
render_context: dict[Any, Any],
):
colorstr = html_escaping.escape_html_attribute(self.color)
stream.write(f'<span style="color:{colorstr};">')
self.child.render_to_html(
stream,
at_beginning_of_line=at_beginning_of_line,
render_context=render_context,
)
stream.write("</span>")
class DashedGrayOutlineBox(basic_parts.BaseBoxWithOutline):
"""A highlighted box that identifies a part as being selected."""
def _box_css_class(self) -> str:
return "dashed_gray_outline"
def _box_css_rule(
self, context: HtmlContextForSetup
) -> part_interface.CSSStyleRule:
return part_interface.CSSStyleRule(
html_escaping.without_repeated_whitespace("""
.box_with_outline.dashed_gray_outline
{
outline: 1px dashed #aaaaaa;
}
""")
)
@dataclasses.dataclass(frozen=True)
class ColoredBorderIndentedChildren(basic_parts.IndentedChildren):
"""A sequence of children that also draws a colored line on the left.
Assumes that the CSS variables --block-color and --block-border-scale are
set in some container object. These are set by WithBlockColor.
"""
def html_setup_parts(
self, context: HtmlContextForSetup
) -> set[CSSStyleRule | JavaScriptDefn]:
rule = html_escaping.without_repeated_whitespace(f"""
.stacked_children.colored_border
> .stacked_child:not({context.collapsed_selector} *)
{{
display: block;
margin-left: calc(2ch - 0.4ch);
}}
.stacked_children.colored_border:not({context.collapsed_selector} *)
{{
display: block;
border-left: solid 0.4ch var(--block-color);
padding-top: calc(0.5lh * (1 - var(--block-border-scale)));
margin-top: calc(-0.5lh * (1 - var(--block-border-scale)));
padding-bottom: 0.5lh;
margin-bottom: -0.5lh;
}}
.stacked_children.colored_border:not({context.collapsed_selector} *)
{{
border-top-left-radius: calc(1em * var(--block-border-scale));
}}
{context.collapsed_selector} .stacked_children.colored_border
{{
border-top: solid 1px var(--block-color);
border-bottom: solid 1px var(--block-color);
}}
""")
return {CSSStyleRule(rule)} | super().html_setup_parts(context)
def render_to_html(
self,
stream: io.TextIOBase,
*,
at_beginning_of_line: bool = False,
render_context: dict[Any, Any],
):
stream.write('<span class="stacked_children colored_border">')
for part in self.children:
# All children render at the beginning of their line.
stream.write('<span class="stacked_child colored_border">')
part.render_to_html(
stream,
at_beginning_of_line=True,
render_context=render_context,
)
stream.write("</span>")
stream.write("</span>")
def _common_block_rules(
context: HtmlContextForSetup,
) -> dict[str, CSSStyleRule]:
return {
"colored_line": CSSStyleRule(
html_escaping.without_repeated_whitespace(f"""
.colored_line
{{
color: black;
background-color: var(--block-color);
border-top: 1px solid var(--block-color);
border-bottom: 1px solid var(--block-color);
}}
{context.hyperlink_hover_selector} .colored_line {{
background-color: oklch(84% 0.145 109);
}}
{context.hyperlink_clicked_tick_selector} .colored_line {{
background-color: oklch(94.1% 0.09 136.7);
}}
{context.hyperlink_clicked_selector}:not(
{context.hyperlink_clicked_tick_selector}
) .colored_line {{
transition: background-color 1s ease-in-out,
font-weight 1s ease-in-out;
}}
""")
),
"patterned_line": CSSStyleRule(
html_escaping.without_repeated_whitespace(f"""
.patterned_line
{{
color: black;
border: 1px solid var(--block-color);
background-image: var(--block-background-image);
background-clip: padding-box;
}}
{context.hyperlink_hover_selector} .patterned_line {{
background-color: oklch(84% 0.145 109);
background-image: none;
}}
{context.hyperlink_clicked_tick_selector} .colored_line {{
background-color: oklch(94.1% 0.09 136.7);
background-image: none;
}}
{context.hyperlink_clicked_selector}:not(
{context.hyperlink_clicked_tick_selector}
) .colored_line {{
transition: background-color 1s ease-in-out,
background-image 1s ease-in-out,
font-weight 1s ease-in-out;
}}
""")
),
"topline": CSSStyleRule(html_escaping.without_repeated_whitespace(f"""
.topline:not({context.collapsed_selector} *)
{{
padding-bottom: 0.15em;
line-height: 1.5em;
}}
""")),
"bottomline": CSSStyleRule(html_escaping.without_repeated_whitespace(f"""
.bottomline:not({context.collapsed_selector} *)
{{
padding-top: 0.15em;
line-height: 1.5em;
}}
""")),
"hch_space_left": CSSStyleRule(
html_escaping.without_repeated_whitespace("""
.hch_space_left
{
padding-left: 0.5ch;
}
""")
),
"hch_space_right": CSSStyleRule(
html_escaping.without_repeated_whitespace("""
.hch_space_right
{
padding-right: 0.5ch;
}
""")
),
}
class ColoredTopLineSpanGroup(basic_parts.BaseSpanGroup):
"""Customized span group that colors the prefix of a block."""
def _span_css_class(self) -> str:
return "colored_line topline hch_space_left"
def _span_css_rule(self, context: HtmlContextForSetup) -> set[CSSStyleRule]:
rules = _common_block_rules(context)
return {rules["colored_line"], rules["topline"], rules["hch_space_left"]}
class ColoredSingleLineSpanGroup(basic_parts.BaseSpanGroup):
"""Customized span group that colors a single line."""
def _span_css_class(self) -> str:
return "colored_line hch_space_left hch_space_right"
def _span_css_rule(self, context: HtmlContextForSetup) -> set[CSSStyleRule]:
rules = _common_block_rules(context)
return {
rules["colored_line"],
rules["hch_space_left"],
rules["hch_space_right"],
}
class ColoredBottomLineSpanGroup(basic_parts.BaseSpanGroup):
"""Customized span group that colors the suffix of a block."""
def _span_css_class(self) -> str:
return "colored_line bottomline hch_space_right"
def _span_css_rule(self, context: HtmlContextForSetup) -> set[CSSStyleRule]:
rules = _common_block_rules(context)
return {
rules["colored_line"],
rules["bottomline"],
rules["hch_space_right"],
}
@dataclasses.dataclass
class WithBlockColor(basic_parts.DeferringToChild):
"""A group that configures block color CSS variables.
This provides the necessary colors for ColoredBorderStackedChildren,
ColoredTopLineSpanGroup, or ColoredBottomLineSpanGroup.
Attributes:
child: Contents of the group.
color: CSS color to render.
"""
child: RenderableTreePart
color: str
def render_to_html(
self,
stream: io.TextIOBase,
*,
at_beginning_of_line: bool = False,
render_context: dict[Any, Any],
):
style_string = f"--block-color: {self.color};"
if at_beginning_of_line:
style_string += "--block-border-scale: 0;"
else:
style_string += "--block-border-scale: 1;"
stream.write(f'<span style="{style_string}">')
self.child.render_to_html(
stream,
at_beginning_of_line=at_beginning_of_line,
render_context=render_context,
)
stream.write("</span>")
class PatternedTopLineSpanGroup(basic_parts.BaseSpanGroup):
"""Customized span group that adds a pattern to the prefix of a block."""
def _span_css_class(self) -> str:
return "patterned_line topline hch_space_left"
def _span_css_rule(self, context: HtmlContextForSetup) -> set[CSSStyleRule]:
rules = _common_block_rules(context)
return {rules["patterned_line"], rules["topline"], rules["hch_space_left"]}
class PatternedBottomLineSpanGroup(basic_parts.BaseSpanGroup):
"""Customized span group that adds a pattern to the suffix of a block."""
def _span_css_class(self) -> str:
return "patterned_line bottomline hch_space_left hch_space_right"
def _span_css_rule(self, context: HtmlContextForSetup) -> set[CSSStyleRule]:
rules = _common_block_rules(context)
return {
rules["patterned_line"],
rules["bottomline"],
rules["hch_space_left"],
rules["hch_space_right"],
}
class PatternedSingleLineSpanGroup(basic_parts.BaseSpanGroup):
"""Customized span group that adds a pattern to a single line."""
def _span_css_class(self) -> str:
return "patterned_line expand_right"
def _span_css_rule(self, context: HtmlContextForSetup) -> set[CSSStyleRule]:
rules = _common_block_rules(context)
return {rules["patterned_line"], rules["expand_right"]}
@dataclasses.dataclass
class WithBlockPattern(basic_parts.DeferringToChild):
"""A group that configures block color CSS variables.
This should be used around uses of PatternedTopLineSpanGroup,
PatternedBottomLineSpanGroup, or PatternedSingleLineSpanGroup.
Attributes:
child: Contents of the group.
color: CSS color for borders.
pattern: CSS value for a background image pattern (e.g. using
`repeating-linear-gradient`)
"""
child: RenderableTreePart
color: str
pattern: str
def render_to_html(
self,
stream: io.TextIOBase,
*,
at_beginning_of_line: bool = False,
render_context: dict[Any, Any],
):
style_string = (
f"--block-color: {self.color}; "
f"--block-background-image: {self.pattern};"
)
if at_beginning_of_line:
style_string += "--block-border-scale: 0;"
else:
style_string += "--block-border-scale: 1;"
stream.write(f'<span style="{style_string}">')
self.child.render_to_html(
stream,
at_beginning_of_line=at_beginning_of_line,
render_context=render_context,
)
stream.write("</span>")
class QualifiedTypeNameSpanGroup(basic_parts.BaseSpanGroup):
"""Customized span group that displays qualified names in a small font."""
def _span_css_class(self) -> str:
return "qualname_prefix"
def _span_css_rule(self, context: HtmlContextForSetup) -> CSSStyleRule:
return CSSStyleRule(html_escaping.without_repeated_whitespace("""
.qualname_prefix {
font-size: 0.8em;
}
"""))