-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcode_block_processor.dart
More file actions
675 lines (585 loc) · 20.6 KB
/
code_block_processor.dart
File metadata and controls
675 lines (585 loc) · 20.6 KB
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
// Copyright 2025 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert' show LineSplitter;
import 'package:collection/collection.dart';
import 'package:jaspr/dom.dart' as dom;
import 'package:jaspr/jaspr.dart' as jaspr;
import 'package:jaspr_content/jaspr_content.dart';
import 'package:meta/meta.dart';
import 'package:opal/opal.dart' as opal;
import '../components/common/wrapped_code_block.dart';
import '../components/dartpad/dartpad_injector.dart';
import '../highlight/theme/dark.dart';
import '../highlight/theme/light.dart';
import '../highlight/token_renderer.dart' as highlighter;
final class CodeBlockProcessor implements PageExtension {
static final opal.LanguageRegistry _languageRegistry =
opal.LanguageRegistry.withDefaults();
const CodeBlockProcessor();
@override
Future<List<Node>> apply(Page page, List<Node> nodes) async {
return _processNodes(nodes);
}
List<Node> _processNodes(List<Node> nodes) {
return [for (final node in nodes) _processNode(node)];
}
Node _processNode(Node node) {
if (node is! ElementNode) return node;
if (node.tag.toLowerCase() == 'pre') {
final children = node.children;
if (children != null && children.isNotEmpty) {
final firstChild = children.first;
if (firstChild is ElementNode && firstChild.tag == 'code') {
final lines = _extractContent(firstChild);
final language = _extractLanguage(
firstChild.attributes['class'] ?? '',
);
final rawMetadata = firstChild.attributes.remove('data-meta');
final metadata = rawMetadata != null
? _parseAttributes(rawMetadata)
: const <String, String?>{};
final title = metadata['title'];
if (language == 'dartpad') {
return ComponentNode(
DartPadWrapper(
content: lines.join('\n'),
title: title ?? 'Runnable Flutter example',
theme: metadata['theme'],
height: metadata['height'],
runAutomatically: metadata['run'] == 'true',
),
);
}
final tag = metadata['tag'];
final rawHighlightLines = metadata['highlightLines'];
final skipHighlighting = metadata.containsKey('noHighlight');
var showLineNumbers = false;
int? initialLineNumber;
if (metadata.containsKey('showLineNumbers')) {
showLineNumbers = true;
final rawShowLineNumbers = metadata['showLineNumbers'];
if (rawShowLineNumbers != null) {
initialLineNumber = int.tryParse(rawShowLineNumbers);
} else {
initialLineNumber = 1;
}
}
final isDiff = metadata.containsKey('diff');
if (isDiff && showLineNumbers) {
throw ArgumentError(
'showLineNumbers and diff are not supported on the same '
'code block yet.',
);
}
final isCollapsed = metadata.containsKey('collapsed');
if (isCollapsed && title == null) {
throw ArgumentError('Collapsed code blocks must have a title.');
}
final isFolding = metadata.containsKey('foldable');
final diffResult = isDiff
? _processDiffLines(lines)
: (lines: lines, addedLines: <int>{}, removedLines: <int>{});
final foldingResult = isFolding
? _processFoldingLines(diffResult.lines)
: (lines: diffResult.lines, foldingRanges: <FoldingRange>[]);
final codeLines = _removeHighlights(
foldingResult.lines,
skipHighlighting,
);
final processedContent = highlightCode(
codeLines,
language: language,
skipSyntaxHighlighting: skipHighlighting,
);
return ComponentNode(
WrappedCodeBlock(
content: processedContent,
language: language,
languagesToHide: const {
'plaintext',
'text',
'console',
'ps',
'diff',
},
title: title,
highlightLines: _parseNumbersAndRanges(rawHighlightLines),
addedLines: diffResult.addedLines,
removedLines: diffResult.removedLines,
foldingRanges: foldingResult.foldingRanges,
tag: tag != null ? CodeBlockTag.parse(tag) : null,
initialLineNumber: initialLineNumber ?? 1,
showLineNumbers: showLineNumbers,
collapsed: isCollapsed,
),
);
}
}
}
final nodeChildren = node.children;
return ElementNode(
node.tag,
node.attributes,
nodeChildren != null ? _processNodes(nodeChildren) : null,
);
}
static List<List<jaspr.Component>> highlightCode(
List<CodeLine> codeLines, {
required String language,
bool skipSyntaxHighlighting = false,
}) {
final content = codeLines.map((line) => line.content).toList();
final languageHighlighter = switch (_languageRegistry[language]) {
final highlighter? when !skipSyntaxHighlighting => highlighter,
_ => opal.BuiltInLanguages.text,
};
final highlightedSpans = languageHighlighter.tokenize(content);
var renderedSpans = highlighter.ThemedSpanRenderer(
themeByName: {
'light': highlighter.SyntaxHighlightingTheme(dashLightTheme),
'dark': highlighter.SyntaxHighlightingTheme(dashDarkTheme),
},
).render(highlightedSpans);
if (language == 'console') {
renderedSpans = _applyConsoleStyles(renderedSpans);
}
return [
for (var i = 0; i < renderedSpans.length; i++)
_processLine(renderedSpans[i], codeLines[i].highlights),
];
}
static const _consolePromptTokenTag = '__console_prompt_token';
static List<List<highlighter.ThemedSpan>> _applyConsoleStyles(
List<List<highlighter.ThemedSpan>> lines,
) {
return [
for (final line in lines)
if (line case [
final span,
...final rest,
] when span.content.startsWith('\$ '))
[
highlighter.ThemedSpan(
content: '\$ ',
styleByTheme: {
'light': dashLightTheme[opal.Tags.comment]!,
'dark': dashDarkTheme[opal.Tags.comment]!,
},
tag: _consolePromptTokenTag,
),
if (span.content.length > 2)
highlighter.ThemedSpan(
content: span.content.substring(2),
styleByTheme: span.styleByTheme,
tag: span.tag,
),
...rest,
]
else
line,
];
}
static List<jaspr.Component> _processLine(
List<highlighter.ThemedSpan> spans,
List<({int startColumn, int length})> highlights,
) {
if (highlights.isEmpty) {
return spans.map(_createSpan).toList(growable: false);
}
final processedSpans = <jaspr.Component>[];
var currentColumn = 0;
for (final span in spans) {
final spanEnd = currentColumn + span.content.length;
final intersecting = _findIntersectingHighlights(
highlights,
currentColumn,
spanEnd,
);
if (intersecting.isEmpty) {
processedSpans.add(_createSpan(span));
} else {
processedSpans.addAll(
_splitSpanByHighlights(span, intersecting, currentColumn),
);
}
currentColumn = spanEnd;
}
return processedSpans;
}
static List<({int startColumn, int length})> _findIntersectingHighlights(
List<({int startColumn, int length})> highlights,
int spanStart,
int spanEnd,
) => highlights
.where((h) {
final highlightEnd = h.startColumn + h.length;
return !(spanStart >= highlightEnd || spanEnd <= h.startColumn);
})
.sorted((a, b) => a.startColumn.compareTo(b.startColumn));
static List<jaspr.Component> _splitSpanByHighlights(
highlighter.ThemedSpan span,
List<({int startColumn, int length})> highlights,
int spanStart,
) {
final result = <jaspr.Component>[];
final spanLength = span.content.length;
var processedStart = 0;
for (final highlight in highlights) {
final highlightEnd = highlight.startColumn + highlight.length;
final startInSpan = (highlight.startColumn - spanStart).clamp(
0,
spanLength,
);
final endInSpan = (highlightEnd - spanStart).clamp(0, spanLength);
// Add non-highlighted portion before the highlight.
if (processedStart < startInSpan) {
result.add(
_createSpan(
span,
content: span.content.substring(processedStart, startInSpan),
),
);
}
// Add highlighted portion.
if (startInSpan < endInSpan) {
result.add(
jaspr.Component.element(
tag: 'mark',
attributes: const {'class': 'highlight'},
children: [
_createSpan(
span,
content: span.content.substring(startInSpan, endInSpan),
),
],
),
);
processedStart = endInSpan;
}
}
// Add remaining non-highlighted portion.
if (processedStart < spanLength) {
result.add(
_createSpan(
span,
content: span.content.substring(processedStart),
),
);
}
return result;
}
static jaspr.Component _createSpan(
highlighter.ThemedSpan span, {
String? content,
}) {
return dom.span(
[.text(content ?? span.content)],
attributes: {
'style': ?span.toInlineStyle(defaultTheme: 'light'),
if (span.tag == _consolePromptTokenTag) 'aria-hidden': 'true',
},
);
}
String _extractLanguage(String className) {
final match = RegExp(r'language-(\w+)').firstMatch(className);
return match?.group(1)?.toLowerCase() ?? 'plaintext';
}
List<String> _extractContent(Node node) {
// Remove trailing empty spaces and new lines.
final text = node.innerText.trimRight();
return const LineSplitter()
.convert(text)
.map((l) => l.trimRight())
.toList(growable: false);
}
List<CodeLine> _removeHighlights(
List<String> lines, [
bool skipHighlighting = false,
]) {
if (skipHighlighting) {
return lines
.map((line) => CodeLine(content: line, highlights: const []))
.toList(growable: false);
}
final lineHighlights = <int, List<({int startColumn, int length})>>{};
({int startLine, int startColumn})? currentHighlight;
final processedLines = <String>[];
for (var lineIndex = 0; lineIndex < lines.length; lineIndex++) {
final line = lines[lineIndex];
if (line.trim().isEmpty) {
// Empty lines should still be a part of the output.
processedLines.add('');
continue;
}
final processedLine = StringBuffer();
var i = 0;
while (i < line.length) {
// Check for the opening marker [!.
if (i + 1 < line.length && line[i] == '[' && line[i + 1] == '!') {
if (currentHighlight != null) {
throw ArgumentError(
'Opening highlight marker "[!" found at '
'line ${lineIndex + 1}, column ${i + 1} while '
'previous highlight at line ${currentHighlight.startLine + 1}, '
'column ${currentHighlight.startColumn + 1} is not closed',
);
}
currentHighlight = (
startLine: lineIndex,
startColumn: processedLine.length,
);
// Skip past the [! marker in the line.
i += 2;
continue;
}
// If there's an open highlight span, check for the closing marker !].
if (currentHighlight != null &&
i + 1 < line.length &&
line[i] == '!' &&
line[i + 1] == ']') {
if (currentHighlight.startLine == lineIndex) {
// If the highlight span is opened and closed in the same line.
lineHighlights.putIfAbsent(lineIndex, () => []).add((
startColumn: currentHighlight.startColumn,
length: processedLine.length - currentHighlight.startColumn,
));
} else {
// If the highlight span is opened then closed in separate lines.
// Add the highlight range for the line the span is opened in.
lineHighlights
.putIfAbsent(currentHighlight.startLine, () => [])
.add((
startColumn: currentHighlight.startColumn,
length:
processedLines[currentHighlight.startLine].length -
currentHighlight.startColumn,
));
// Add the highlight range for the lines completely included.
for (var j = currentHighlight.startLine + 1; j < lineIndex; j++) {
lineHighlights.putIfAbsent(j, () => []).add((
startColumn: 0,
length: processedLines[j].length,
));
}
// Add the highlight range for the line the span is closed in.
lineHighlights.putIfAbsent(lineIndex, () => []).add((
startColumn: 0,
length: processedLine.length,
));
}
currentHighlight = null;
// Skip past the !] marker in the line.
i += 2;
continue;
}
processedLine.write(line[i]);
i++;
}
processedLines.add(processedLine.toString());
}
// Check if a highlight span was never closed.
if (currentHighlight != null) {
throw ArgumentError(
'Unclosed highlight marker starting at '
'line ${currentHighlight.startLine + 1}, '
'column ${currentHighlight.startColumn + 1}',
);
}
return [
for (var i = 0; i < processedLines.length; i++)
CodeLine(
content: processedLines[i],
highlights: lineHighlights[i] ?? [],
),
];
}
/// Processes lines for diff mode, extracting added/removed line markers.
///
/// Lines starting with '+' are marked as added lines.
/// Lines starting with '-' are marked as removed lines.
/// The first two characters (marker and space) are removed from each line.
({
List<String> lines,
Set<int> addedLines,
Set<int> removedLines,
})
_processDiffLines(List<String> lines) {
final addedLines = <int>{};
final removedLines = <int>{};
final processedLines = <String>[];
for (var lineIndex = 0; lineIndex < lines.length; lineIndex += 1) {
final line = lines[lineIndex];
switch (line.isNotEmpty ? line[0] : '') {
case '+':
addedLines.add(lineIndex + 1);
case '-':
removedLines.add(lineIndex + 1);
}
// Remove the first 2 characters (marker and space).
processedLines.add(line.length >= 2 ? line.substring(2) : '');
}
return (
lines: processedLines,
addedLines: addedLines,
removedLines: removedLines,
);
}
/// Processes lines for folding mode, extracting folding range line markers.
///
/// Lines equal to '[*' are marked as the start of an open folding range.
/// Lines equal to '[* -' are marked as the start of a closed folding range.
/// Lines equal to '*]' are marked as the end of a folding range.
/// The folding markers are removed from the lines.
({
List<String> lines,
List<FoldingRange> foldingRanges,
})
_processFoldingLines(List<String> lines) {
final foldingRanges = <FoldingRange>[];
final processedLines = <String>[];
final foldingStack = <({int start, bool open})>[];
for (var lineIndex = 0; lineIndex < lines.length; lineIndex += 1) {
final line = lines[lineIndex];
// To account for removal of the folding marker lines.
final lineIndexCorrection =
(foldingRanges.length * 2) + foldingStack.length;
if (line.trim() == '[*') {
foldingStack.add((
start: lineIndex + 1 - lineIndexCorrection,
open: true,
));
// Skip adding this line to processed lines.
continue;
} else if (line.trim() == '[* -') {
foldingStack.add((
start: lineIndex + 1 - lineIndexCorrection,
open: false,
));
// Skip adding this line to processed lines.
continue;
} else if (line.trim() == '*]') {
if (foldingStack.isNotEmpty) {
final (:start, :open) = foldingStack.removeLast();
foldingRanges.add((
start: start,
end: lineIndex - lineIndexCorrection,
level: foldingStack.length,
open: open,
));
}
// Skip adding this line to processed lines.
continue;
}
processedLines.add(line);
}
return (
lines: processedLines,
foldingRanges: foldingRanges,
);
}
}
@immutable
final class CodeLine {
final String content;
final List<({int startColumn, int length})> highlights;
const CodeLine({required this.content, required this.highlights});
}
/// Parses a comma-separated list of numbers and ranges into a set of numbers.
///
/// Returns all unique numbers specified in the input.
Set<int> _parseNumbersAndRanges(String? input) {
if (input == null) return const {};
final elements = input.trim().split(',');
if (elements.isEmpty) return const {};
final combinedNumbers = <int>{};
for (final element in elements) {
final rangeParts = element.split('-');
// If it includes a dash, it is (hopefully) a range between two numbers.
if (rangeParts.length > 1) {
// Split by the dash, and turn each string into a number.
// Assume the user only included one dash.
final start = int.tryParse(rangeParts[0].trim());
final end = int.tryParse(rangeParts[1].trim());
if (start != null && end != null) {
for (var i = start; i <= end; i++) {
combinedNumbers.add(i);
}
}
} else {
// It's (hopefully) just a single number.
final number = int.tryParse(element.trim());
if (number != null) {
combinedNumbers.add(number);
}
}
}
return combinedNumbers;
}
/// Matches a key-value attribute pair, similar to HTML elements.
///
/// Group 1: The attribute key.
/// Group 2: The value if quoted and present.
/// Group 3: The value if unquoted and present.
final RegExp _attributeRegex = RegExp(r'(\w+)(?:=(?:"([^"]*)"|(\S+)))?');
Map<String, String?> _parseAttributes(String input) {
final matches = _attributeRegex.allMatches(input);
return {
for (final match in matches)
match.group(1)!: match.group(2) ?? match.group(3),
};
}
extension ThemedSpanToHtml on highlighter.ThemedSpan {
String? toInlineStyle({String? defaultTheme}) {
final buffer = StringBuffer();
for (final MapEntry(key: themeName, value: style) in styleByTheme.entries) {
final isDefault = themeName == defaultTheme;
if (style.foregroundColor case final fgColor?) {
if (isDefault) {
buffer.write('color: rgba(');
buffer.write((fgColor.red * 255).round());
buffer.write(', ');
buffer.write((fgColor.green * 255).round());
buffer.write(', ');
buffer.write((fgColor.blue * 255).round());
buffer.write(', ');
buffer.write(fgColor.alpha);
buffer.write('); ');
} else {
buffer.write('--opal-$themeName-color: rgba(');
buffer.write((fgColor.red * 255).round());
buffer.write(', ');
buffer.write((fgColor.green * 255).round());
buffer.write(', ');
buffer.write((fgColor.blue * 255).round());
buffer.write(', ');
buffer.write(fgColor.alpha);
buffer.write('); ');
}
}
if (style.fontStyle case final fontStyle?) {
final fontStyleValue = switch (fontStyle) {
highlighter.FontStyle.italic => 'italic',
highlighter.FontStyle.normal => 'normal',
};
if (isDefault) {
buffer.write('font-style: $fontStyleValue; ');
} else {
buffer.write('--opal-$themeName-font-style: $fontStyleValue; ');
}
}
if (style.fontWeight case final fontWeight?) {
if (isDefault) {
buffer.write('font-weight: ${fontWeight.weight}; ');
} else {
buffer.write('--opal-$themeName-font-weight: ${fontWeight.weight}; ');
}
}
}
final resultingStyle = buffer.toString().trimRight();
if (resultingStyle.isEmpty) {
return null;
}
return resultingStyle;
}
}