euv-docs 0.37.2

A documentation site generator powered by euv + euv-ui (Rust/WASM).
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
use super::*;

/// Tokenizes a code block into styled segments.
///
/// The tokenizer is deliberately simple and deterministic: it recognises
/// comments, strings, numbers and identifiers, and classifies the
/// identifiers by the language's own keyword table. It never fails —
/// an unrecognised language yields the code as one plain segment.
///
/// # Arguments
///
/// - `&str` - The fence language.
/// - `&str` - The raw code.
///
/// # Returns
///
/// - `Vec<CodeToken>` - The segments, in source order.
pub(crate) fn tokenize(lang: &str, code: &str) -> Vec<CodeToken> {
    match normalize_language(lang) {
        CodeLanguage::Plain => plain_tokens(code),
        CodeLanguage::Rust => tokenize_rust(code),
        CodeLanguage::Bash => tokenize_bash(code),
        CodeLanguage::Json => tokenize_json(code),
        CodeLanguage::TypeScript => tokenize_typescript(code),
        CodeLanguage::Html => tokenize_html(code),
        CodeLanguage::Css => tokenize_css(code),
    }
}

/// Renders a code block as HTML.
///
/// The segments become spans that carry one class each; a plain segment
/// stays bare text. The code itself is escaped, so a block that holds
/// markup shows the markup instead of running it.
///
/// # Arguments
///
/// - `&str` - The fence language.
/// - `&str` - The raw code.
///
/// # Returns
///
/// - `String` - The highlighted markup.
pub(crate) fn highlighted_html(lang: &str, code: &str) -> String {
    let mut html: String = String::with_capacity(code.len());
    for token in tokenize(lang, code) {
        match token_class(token.kind) {
            Some(class) => {
                html.push_str("<span class=\"");
                html.push_str(class);
                html.push_str("\">");
                html.push_str(&escape_html(&token.text));
                html.push_str("</span>");
            }
            None => html.push_str(&escape_html(&token.text)),
        }
    }
    html
}

/// Escapes the characters that would otherwise be read as markup.
///
/// # Arguments
///
/// - `&str` - The text to escape.
///
/// # Returns
///
/// - `String` - The escaped text.
fn escape_html(text: &str) -> String {
    let mut escaped: String = String::with_capacity(text.len());
    for character in text.chars() {
        match character {
            '&' => escaped.push_str("&amp;"),
            '<' => escaped.push_str("&lt;"),
            '>' => escaped.push_str("&gt;"),
            _ => escaped.push(character),
        }
    }
    escaped
}

/// Returns the CSS class of a token kind, or `None` for plain text.
///
/// Plain text carries no class: it inherits the code block's own
/// foreground colour, so an unstyled segment is the same colour as the
/// rest of the block and the markup stays free of empty spans.
///
/// # Arguments
///
/// - `CodeTokenKind` - The kind of the token.
///
/// # Returns
///
/// - `Option<&'static str>` - The class, or `None` for plain text.
pub(crate) fn token_class(kind: CodeTokenKind) -> Option<&'static str> {
    match kind {
        CodeTokenKind::Plain => None,
        CodeTokenKind::Keyword => Some(CLASS_TOKEN_KEYWORD),
        CodeTokenKind::String => Some(CLASS_TOKEN_STRING),
        CodeTokenKind::Number => Some(CLASS_TOKEN_NUMBER),
        CodeTokenKind::Comment => Some(CLASS_TOKEN_COMMENT),
        CodeTokenKind::Function => Some(CLASS_TOKEN_FUNCTION),
        CodeTokenKind::Type => Some(CLASS_TOKEN_TYPE),
        CodeTokenKind::Attribute => Some(CLASS_TOKEN_ATTRIBUTE),
        CodeTokenKind::Tag => Some(CLASS_TOKEN_TAG),
        CodeTokenKind::Variable => Some(CLASS_TOKEN_VARIABLE),
    }
}

/// Maps a fence language onto the language the tokenizer knows.
///
/// # Arguments
///
/// - `&str` - The fence language.
///
/// # Returns
///
/// - `CodeLanguage` - The matching language.
fn normalize_language(lang: &str) -> CodeLanguage {
    match lang.trim().to_ascii_lowercase().as_str() {
        "rust" | "rs" => CodeLanguage::Rust,
        "bash" | "sh" | "shell" | "zsh" | "console" | "terminal" | "shell-session" => {
            CodeLanguage::Bash
        }
        "json" | "jsonc" => CodeLanguage::Json,
        "ts" | "tsx" | "typescript" | "js" | "jsx" | "javascript" => CodeLanguage::TypeScript,
        "html" | "xml" | "vue" | "svg" => CodeLanguage::Html,
        "css" | "scss" | "less" => CodeLanguage::Css,
        _ => CodeLanguage::Plain,
    }
}

/// Returns the code as a single plain segment.
///
/// # Arguments
///
/// - `&str` - The raw code.
///
/// # Returns
///
/// - `Vec<CodeToken>` - One segment, empty when the code is empty.
fn plain_tokens(code: &str) -> Vec<CodeToken> {
    if code.is_empty() {
        return Vec::new();
    }
    vec![CodeToken {
        kind: CodeTokenKind::Plain,
        text: code.to_string(),
    }]
}

/// Appends a segment, merging it into the previous one when both have
/// the same kind.
///
/// Merging keeps the rendered markup small: a line of plain code is
/// one span, not one span per word.
///
/// # Arguments
///
/// - `&mut Vec<CodeToken>` - The segment list.
/// - `CodeTokenKind` - The kind of the segment.
/// - `&str` - The segment text.
fn push_token(tokens: &mut Vec<CodeToken>, kind: CodeTokenKind, text: &str) {
    if text.is_empty() {
        return;
    }
    if let Some(last) = tokens.last_mut()
        && last.kind == kind
    {
        last.text.push_str(text);
        return;
    }
    tokens.push(CodeToken {
        kind,
        text: text.to_string(),
    });
}

/// Moves the pending plain text into the segment list.
///
/// # Arguments
///
/// - `&mut Vec<CodeToken>` - The segment list.
/// - `&mut String` - The pending plain text, emptied by the call.
fn flush_plain(tokens: &mut Vec<CodeToken>, plain: &mut String) {
    if plain.is_empty() {
        return;
    }
    push_token(tokens, CodeTokenKind::Plain, plain);
    plain.clear();
}

/// Collects a range of characters into a string.
///
/// # Arguments
///
/// - `&[char]` - The source characters.
/// - `usize` - The first index.
/// - `usize` - The index past the last one.
///
/// # Returns
///
/// - `String` - The collected text.
fn collect(chars: &[char], start: usize, end: usize) -> String {
    chars[start.min(chars.len())..end.min(chars.len())]
        .iter()
        .collect()
}

/// Whether a character can start an identifier.
///
/// # Arguments
///
/// - `char` - The character.
///
/// # Returns
///
/// - `bool` - `true` when the character starts an identifier.
fn is_ident_start(candidate: char) -> bool {
    candidate.is_alphabetic() || candidate == '_'
}

/// Whether a character can continue an identifier.
///
/// # Arguments
///
/// - `char` - The character.
///
/// # Returns
///
/// - `bool` - `true` when the character continues an identifier.
fn is_ident_part(candidate: char) -> bool {
    candidate.is_alphanumeric() || candidate == '_'
}

/// Whether a word starts with an uppercase letter, which is the usual
/// spelling of a type.
///
/// # Arguments
///
/// - `&str` - The word.
///
/// # Returns
///
/// - `bool` - `true` when the word looks like a type name.
fn starts_uppercase(word: &str) -> bool {
    word.chars().next().is_some_and(|c: char| c.is_uppercase())
}

/// Whether the next meaningful character opens a call parenthesis.
///
/// # Arguments
///
/// - `&[char]` - The source characters.
/// - `usize` - The index to look from.
///
/// # Returns
///
/// - `bool` - `true` when the word is followed by `(`.
fn next_is_call(chars: &[char], from: usize) -> bool {
    let mut index: usize = from;
    while index < chars.len() && chars[index].is_whitespace() {
        index += 1;
    }
    chars.get(index) == Some(&'(')
}

/// Tokenizes Rust source.
///
/// # Arguments
///
/// - `&str` - The raw code.
///
/// # Returns
///
/// - `Vec<CodeToken>` - The segments, in source order.
fn tokenize_rust(code: &str) -> Vec<CodeToken> {
    let chars: Vec<char> = code.chars().collect();
    let mut tokens: Vec<CodeToken> = Vec::new();
    let mut plain: String = String::new();
    let mut index: usize = 0;
    let mut pending: Option<CodeTokenKind> = None;
    while index < chars.len() {
        let current: char = chars[index];
        let next: char = chars.get(index + 1).copied().unwrap_or('\0');
        if current == '/' && next == '/' {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            while index < chars.len() && chars[index] != '\n' {
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::Comment,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current == '/' && next == '*' {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            index += 2;
            while index < chars.len()
                && !(chars[index] == '*' && chars.get(index + 1) == Some(&'/'))
            {
                index += 1;
            }
            index = (index + 2).min(chars.len());
            push_token(
                &mut tokens,
                CodeTokenKind::Comment,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current == '"' {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            index += 1;
            while index < chars.len() {
                if chars[index] == '\\' {
                    index += 2;
                    continue;
                }
                if chars[index] == '"' {
                    index += 1;
                    break;
                }
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::String,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current == '\'' {
            let is_char_literal: bool =
                chars.get(index + 1) == Some(&'\\') || chars.get(index + 2) == Some(&'\'');
            if is_char_literal {
                flush_plain(&mut tokens, &mut plain);
                let start: usize = index;
                index += 1;
                while index < chars.len() {
                    if chars[index] == '\\' {
                        index += 2;
                        continue;
                    }
                    if chars[index] == '\'' {
                        index += 1;
                        break;
                    }
                    index += 1;
                }
                push_token(
                    &mut tokens,
                    CodeTokenKind::String,
                    &collect(&chars, start, index),
                );
                continue;
            }
            plain.push(current);
            index += 1;
            continue;
        }
        if current == '#' && next == '[' {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            let mut depth: usize = 0;
            while index < chars.len() {
                if chars[index] == '[' {
                    depth += 1;
                }
                if chars[index] == ']' {
                    depth -= 1;
                    if depth == 0 {
                        index += 1;
                        break;
                    }
                }
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::Attribute,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current.is_ascii_digit() {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            while index < chars.len()
                && (chars[index].is_ascii_alphanumeric()
                    || chars[index] == '.'
                    || chars[index] == '_')
            {
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::Number,
                &collect(&chars, start, index),
            );
            continue;
        }
        if is_ident_start(current) {
            let start: usize = index;
            while index < chars.len() && is_ident_part(chars[index]) {
                index += 1;
            }
            let word: String = collect(&chars, start, index);
            flush_plain(&mut tokens, &mut plain);
            let (kind, announced): (CodeTokenKind, Option<CodeTokenKind>) = match pending.take() {
                Some(kind) => (kind, None),
                None => classify_rust_word(&word, &chars, index),
            };
            pending = announced;
            push_token(&mut tokens, kind, &word);
            continue;
        }
        plain.push(current);
        index += 1;
    }
    flush_plain(&mut tokens, &mut plain);
    tokens
}

/// Classifies a Rust word, and reports what the *next* word will be
/// when the current one announces it (`fn` announces a function name,
/// `struct` announces a type name).
///
/// # Arguments
///
/// - `&str` - The word.
/// - `&[char]` - The source characters.
/// - `usize` - The index just past the word.
///
/// # Returns
///
/// - `(CodeTokenKind, Option<CodeTokenKind>)` - The kind of the word,
///   and the kind the next word gets when this one announces it.
fn classify_rust_word(
    word: &str,
    chars: &[char],
    after: usize,
) -> (CodeTokenKind, Option<CodeTokenKind>) {
    if RUST_KEYWORDS.contains(&word) {
        if word == RUST_FUNCTION_KEYWORD {
            return (CodeTokenKind::Keyword, Some(CodeTokenKind::Function));
        }
        if RUST_TYPE_KEYWORDS.contains(&word) {
            return (CodeTokenKind::Keyword, Some(CodeTokenKind::Type));
        }
        return (CodeTokenKind::Keyword, None);
    }
    if starts_uppercase(word) {
        return (CodeTokenKind::Type, None);
    }
    if next_is_call(chars, after) {
        return (CodeTokenKind::Function, None);
    }
    (CodeTokenKind::Plain, None)
}

/// Tokenizes a shell script.
///
/// # Arguments
///
/// - `&str` - The raw code.
///
/// # Returns
///
/// - `Vec<CodeToken>` - The segments, in source order.
fn tokenize_bash(code: &str) -> Vec<CodeToken> {
    let chars: Vec<char> = code.chars().collect();
    let mut tokens: Vec<CodeToken> = Vec::new();
    let mut plain: String = String::new();
    let mut index: usize = 0;
    let mut at_command_start: bool = true;
    while index < chars.len() {
        let current: char = chars[index];
        if current == '#' {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            while index < chars.len() && chars[index] != '\n' {
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::Comment,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current == '"' || current == '\'' {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            index += 1;
            while index < chars.len() {
                if chars[index] == '\\' {
                    index += 2;
                    continue;
                }
                if chars[index] == current {
                    index += 1;
                    break;
                }
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::String,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current == '$' {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            index += 1;
            if chars.get(index) == Some(&'{') {
                while index < chars.len() && chars[index] != '}' {
                    index += 1;
                }
                index = (index + 1).min(chars.len());
            } else {
                while index < chars.len() && is_ident_part(chars[index]) {
                    index += 1;
                }
            }
            push_token(
                &mut tokens,
                CodeTokenKind::Variable,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current.is_ascii_digit() {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            while index < chars.len() && (chars[index].is_ascii_digit() || chars[index] == '.') {
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::Number,
                &collect(&chars, start, index),
            );
            continue;
        }
        if is_ident_start(current) {
            let start: usize = index;
            while index < chars.len() && is_ident_part(chars[index]) {
                index += 1;
            }
            let word: String = collect(&chars, start, index);
            flush_plain(&mut tokens, &mut plain);
            let kind: CodeTokenKind = if BASH_KEYWORDS.contains(&word.as_str()) {
                CodeTokenKind::Keyword
            } else if at_command_start {
                CodeTokenKind::Function
            } else {
                CodeTokenKind::Plain
            };
            if kind != CodeTokenKind::Keyword {
                at_command_start = false;
            }
            push_token(&mut tokens, kind, &word);
            continue;
        }
        plain.push(current);
        index += 1;
        if current == '\n' || current == ';' || current == '|' || current == '&' {
            at_command_start = true;
        }
    }
    flush_plain(&mut tokens, &mut plain);
    tokens
}

/// Tokenizes JSON.
///
/// # Arguments
///
/// - `&str` - The raw code.
///
/// # Returns
///
/// - `Vec<CodeToken>` - The segments, in source order.
fn tokenize_json(code: &str) -> Vec<CodeToken> {
    let chars: Vec<char> = code.chars().collect();
    let mut tokens: Vec<CodeToken> = Vec::new();
    let mut plain: String = String::new();
    let mut index: usize = 0;
    while index < chars.len() {
        let current: char = chars[index];
        if current == '"' {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            index += 1;
            while index < chars.len() {
                if chars[index] == '\\' {
                    index += 2;
                    continue;
                }
                if chars[index] == '"' {
                    index += 1;
                    break;
                }
                index += 1;
            }
            let kind: CodeTokenKind = if json_key_follows(&chars, index) {
                CodeTokenKind::Attribute
            } else {
                CodeTokenKind::String
            };
            push_token(&mut tokens, kind, &collect(&chars, start, index));
            continue;
        }
        if current.is_ascii_digit()
            || (current == '-'
                && chars
                    .get(index + 1)
                    .is_some_and(|c: &char| c.is_ascii_digit()))
        {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            index += 1;
            while index < chars.len()
                && (chars[index].is_ascii_digit() || chars[index] == '.' || chars[index] == 'e')
            {
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::Number,
                &collect(&chars, start, index),
            );
            continue;
        }
        if is_ident_start(current) {
            let start: usize = index;
            while index < chars.len() && is_ident_part(chars[index]) {
                index += 1;
            }
            let word: String = collect(&chars, start, index);
            flush_plain(&mut tokens, &mut plain);
            let kind: CodeTokenKind = if JSON_KEYWORDS.contains(&word.as_str()) {
                CodeTokenKind::Keyword
            } else {
                CodeTokenKind::Plain
            };
            push_token(&mut tokens, kind, &word);
            continue;
        }
        plain.push(current);
        index += 1;
    }
    flush_plain(&mut tokens, &mut plain);
    tokens
}

/// Whether the string that ends at `after` is an object key.
///
/// # Arguments
///
/// - `&[char]` - The source characters.
/// - `usize` - The index just past the closing quote.
///
/// # Returns
///
/// - `bool` - `true` when a colon follows the string.
fn json_key_follows(chars: &[char], after: usize) -> bool {
    let mut index: usize = after;
    while index < chars.len() && chars[index].is_whitespace() {
        index += 1;
    }
    chars.get(index) == Some(&':')
}

/// Tokenizes TypeScript or JavaScript source.
///
/// # Arguments
///
/// - `&str` - The raw code.
///
/// # Returns
///
/// - `Vec<CodeToken>` - The segments, in source order.
fn tokenize_typescript(code: &str) -> Vec<CodeToken> {
    let chars: Vec<char> = code.chars().collect();
    let mut tokens: Vec<CodeToken> = Vec::new();
    let mut plain: String = String::new();
    let mut index: usize = 0;
    while index < chars.len() {
        let current: char = chars[index];
        let next: char = chars.get(index + 1).copied().unwrap_or('\0');
        if current == '/' && next == '/' {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            while index < chars.len() && chars[index] != '\n' {
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::Comment,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current == '/' && next == '*' {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            index += 2;
            while index < chars.len()
                && !(chars[index] == '*' && chars.get(index + 1) == Some(&'/'))
            {
                index += 1;
            }
            index = (index + 2).min(chars.len());
            push_token(
                &mut tokens,
                CodeTokenKind::Comment,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current == '"' || current == '\'' || current == '`' {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            index += 1;
            while index < chars.len() {
                if chars[index] == '\\' {
                    index += 2;
                    continue;
                }
                if chars[index] == current {
                    index += 1;
                    break;
                }
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::String,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current == '@'
            && chars
                .get(index + 1)
                .is_some_and(|c: &char| is_ident_start(*c))
        {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            index += 1;
            while index < chars.len() && is_ident_part(chars[index]) {
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::Attribute,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current.is_ascii_digit() {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            while index < chars.len()
                && (chars[index].is_ascii_alphanumeric()
                    || chars[index] == '.'
                    || chars[index] == '_')
            {
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::Number,
                &collect(&chars, start, index),
            );
            continue;
        }
        if is_ident_start(current) {
            let start: usize = index;
            while index < chars.len() && is_ident_part(chars[index]) {
                index += 1;
            }
            let word: String = collect(&chars, start, index);
            flush_plain(&mut tokens, &mut plain);
            let kind: CodeTokenKind = if TS_KEYWORDS.contains(&word.as_str()) {
                CodeTokenKind::Keyword
            } else if starts_uppercase(&word) {
                CodeTokenKind::Type
            } else if next_is_call(&chars, index) {
                CodeTokenKind::Function
            } else {
                CodeTokenKind::Plain
            };
            push_token(&mut tokens, kind, &word);
            continue;
        }
        plain.push(current);
        index += 1;
    }
    flush_plain(&mut tokens, &mut plain);
    tokens
}

/// Tokenizes HTML.
///
/// # Arguments
///
/// - `&str` - The raw code.
///
/// # Returns
///
/// - `Vec<CodeToken>` - The segments, in source order.
fn tokenize_html(code: &str) -> Vec<CodeToken> {
    let chars: Vec<char> = code.chars().collect();
    let mut tokens: Vec<CodeToken> = Vec::new();
    let mut plain: String = String::new();
    let mut index: usize = 0;
    while index < chars.len() {
        let current: char = chars[index];
        if current == '<'
            && chars.get(index + 1) == Some(&'!')
            && chars.get(index + 2) == Some(&'-')
        {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            while index < chars.len()
                && !(chars[index] == '-' && chars.get(index + 1) == Some(&'-'))
            {
                index += 1;
            }
            index = (index + 2).min(chars.len());
            push_token(
                &mut tokens,
                CodeTokenKind::Comment,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current == '<' {
            flush_plain(&mut tokens, &mut plain);
            index += 1;
            if chars.get(index) == Some(&'/') {
                index += 1;
            }
            let name_start: usize = index;
            while index < chars.len() && is_ident_part(chars[index]) {
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::Tag,
                &collect(&chars, name_start, index),
            );
            while index < chars.len() && chars[index] != '>' {
                let attribute: char = chars[index];
                if attribute == '"' || attribute == '\'' {
                    let quote: char = attribute;
                    let value_start: usize = index;
                    index += 1;
                    while index < chars.len() && chars[index] != quote {
                        index += 1;
                    }
                    index = (index + 1).min(chars.len());
                    push_token(
                        &mut tokens,
                        CodeTokenKind::String,
                        &collect(&chars, value_start, index),
                    );
                    continue;
                }
                if is_ident_start(attribute) {
                    let attribute_start: usize = index;
                    while index < chars.len() && is_ident_part(chars[index]) {
                        index += 1;
                    }
                    push_token(
                        &mut tokens,
                        CodeTokenKind::Attribute,
                        &collect(&chars, attribute_start, index),
                    );
                    continue;
                }
                index += 1;
            }
            index = (index + 1).min(chars.len());
            continue;
        }
        plain.push(current);
        index += 1;
    }
    flush_plain(&mut tokens, &mut plain);
    tokens
}

/// Tokenizes CSS.
///
/// # Arguments
///
/// - `&str` - The raw code.
///
/// # Returns
///
/// - `Vec<CodeToken>` - The segments, in source order.
fn tokenize_css(code: &str) -> Vec<CodeToken> {
    let chars: Vec<char> = code.chars().collect();
    let mut tokens: Vec<CodeToken> = Vec::new();
    let mut plain: String = String::new();
    let mut index: usize = 0;
    let mut in_block: bool = false;
    while index < chars.len() {
        let current: char = chars[index];
        let next: char = chars.get(index + 1).copied().unwrap_or('\0');
        if current == '/' && next == '*' {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            index += 2;
            while index < chars.len()
                && !(chars[index] == '*' && chars.get(index + 1) == Some(&'/'))
            {
                index += 1;
            }
            index = (index + 2).min(chars.len());
            push_token(
                &mut tokens,
                CodeTokenKind::Comment,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current == '"' || current == '\'' {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            index += 1;
            while index < chars.len() && chars[index] != current {
                index += 1;
            }
            index = (index + 1).min(chars.len());
            push_token(
                &mut tokens,
                CodeTokenKind::String,
                &collect(&chars, start, index),
            );
            continue;
        }
        if current == '{' {
            in_block = true;
            plain.push(current);
            index += 1;
            continue;
        }
        if current == '}' {
            in_block = false;
            plain.push(current);
            index += 1;
            continue;
        }
        if current.is_ascii_digit() {
            flush_plain(&mut tokens, &mut plain);
            let start: usize = index;
            while index < chars.len()
                && (chars[index].is_ascii_alphanumeric()
                    || chars[index] == '.'
                    || chars[index] == '%')
            {
                index += 1;
            }
            push_token(
                &mut tokens,
                CodeTokenKind::Number,
                &collect(&chars, start, index),
            );
            continue;
        }
        if is_ident_start(current) || current == '-' {
            let start: usize = index;
            index += 1;
            while index < chars.len() && (is_ident_part(chars[index]) || chars[index] == '-') {
                index += 1;
            }
            let word: String = collect(&chars, start, index);
            flush_plain(&mut tokens, &mut plain);
            let kind: CodeTokenKind = if in_block && css_is_property(&chars, index) {
                CodeTokenKind::Attribute
            } else if in_block {
                CodeTokenKind::Plain
            } else {
                CodeTokenKind::Tag
            };
            push_token(&mut tokens, kind, &word);
            continue;
        }
        plain.push(current);
        index += 1;
    }
    flush_plain(&mut tokens, &mut plain);
    tokens
}

/// Whether the word that ends at `after` is a property name, i.e. is
/// followed by a colon.
///
/// # Arguments
///
/// - `&[char]` - The source characters.
/// - `usize` - The index just past the word.
///
/// # Returns
///
/// - `bool` - `true` when a colon follows the word.
fn css_is_property(chars: &[char], after: usize) -> bool {
    let mut index: usize = after;
    while index < chars.len() && chars[index].is_whitespace() {
        index += 1;
    }
    chars.get(index) == Some(&':')
}