syntaxmate 0.1.1

Rust-native TextMate syntax highlighting with bundled grammars and themes
Documentation
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
//! Safe, dependency-free HTML and ANSI rendering for highlighted documents.

#[cfg(any(feature = "ansi", feature = "html"))]
use std::fmt::Write as _;

use crate::HighlightStatus;
#[cfg(feature = "ansi")]
use crate::theme::RgbColor;
#[cfg(all(
    feature = "bundled-grammars",
    feature = "bundled-themes",
    any(feature = "ansi", feature = "html")
))]
use crate::{EngineHighlightedLine, HighlightedText, Theme};
#[cfg(any(feature = "ansi", feature = "html"))]
use crate::{Error, HighlightedDocument, HighlightedLine, Result, Style, theme::SyntaxModifiers};

/// A rendered string together with the tokenizer completion status.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RenderedOutput {
    content: String,
    status: HighlightStatus,
}

impl RenderedOutput {
    /// Returns the rendered text.
    pub fn as_str(&self) -> &str {
        &self.content
    }

    /// Consumes the result and returns the rendered text.
    pub fn into_string(self) -> String {
        self.content
    }

    /// Reports whether tokenization completed without exhausting a safety budget.
    pub fn status(&self) -> HighlightStatus {
        self.status
    }
}

/// Options for [`render_html`].
#[cfg(feature = "html")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HtmlOptions {
    /// Wrap output in `<pre><code>...</code></pre>`.
    pub include_wrapper: bool,
    /// Class placed on the `<pre>` wrapper. Ignored without a wrapper.
    pub class: Option<String>,
    /// Add a `data-scopes` attribute containing the exact TextMate scope stack.
    pub include_scopes: bool,
}

#[cfg(feature = "html")]
impl Default for HtmlOptions {
    fn default() -> Self {
        Self {
            include_wrapper: true,
            class: Some("syntaxmate".to_owned()),
            include_scopes: false,
        }
    }
}

/// Renders highlighted source as escaped HTML.
///
/// Source text, wrapper classes, and optional scope attributes are escaped.
/// The document must have been produced from `source`; a mismatch returns an
/// error rather than slicing unchecked byte ranges.
#[cfg(feature = "html")]
pub fn render_html(
    source: &str,
    document: &HighlightedDocument,
    options: &HtmlOptions,
) -> Result<RenderedOutput> {
    let lines = validated_lines(source, document)?;
    let mut output = String::with_capacity(source.len().saturating_mul(2));
    if options.include_wrapper {
        output.push_str("<pre");
        if let Some(class) = options.class.as_deref() {
            output.push_str(" class=\"");
            escape_html_attribute(class, &mut output);
            output.push('"');
        }
        output.push_str("><code>");
    }

    for (line_index, (text, line)) in lines.into_iter().enumerate() {
        render_html_line(text, line, options, &mut output);
        if line_index + 1 < document.lines().len() {
            output.push('\n');
        }
    }

    if options.include_wrapper {
        output.push_str("</code></pre>");
    }
    Ok(RenderedOutput {
        content: output,
        status: document.status(),
    })
}

/// Renders the engine's compact scope-stack tokens directly, avoiding owned
/// public token and styled-document intermediates.
#[cfg(all(
    feature = "html",
    feature = "bundled-grammars",
    feature = "bundled-themes"
))]
pub(crate) fn render_html_compact(
    source: &str,
    tokens: &HighlightedText,
    status: HighlightStatus,
    theme: &Theme,
    options: &HtmlOptions,
) -> Result<RenderedOutput> {
    let mut output = String::with_capacity(source.len().saturating_mul(2));
    if options.include_wrapper {
        output.push_str("<pre");
        if let Some(class) = options.class.as_deref() {
            output.push_str(" class=\"");
            escape_html_attribute(class, &mut output);
            output.push('"');
        }
        output.push_str("><code>");
    }

    let mut source_lines = crate::engine::line::LineChunks::new(source);
    for (line_index, line) in tokens.lines.iter().enumerate() {
        let chunk = source_lines.next().ok_or_else(compact_line_count_error)?;
        render_html_compact_line(chunk.text, line, theme, options, &mut output);
        if line_index + 1 < tokens.lines.len() {
            output.push('\n');
        }
    }
    if source_lines.next().is_some() {
        return Err(compact_line_count_error());
    }

    if options.include_wrapper {
        output.push_str("</code></pre>");
    }
    Ok(RenderedOutput {
        content: output,
        status,
    })
}

#[cfg(all(
    feature = "html",
    feature = "bundled-grammars",
    feature = "bundled-themes"
))]
fn render_html_compact_line(
    text: &str,
    line: &EngineHighlightedLine,
    theme: &Theme,
    options: &HtmlOptions,
    output: &mut String,
) {
    let mut cursor = 0;
    for span in &line.segments {
        debug_assert!(
            cursor <= span.byte_start
                && span.byte_start <= span.byte_end
                && span.byte_end <= text.len()
                && text.is_char_boundary(span.byte_start)
                && text.is_char_boundary(span.byte_end)
        );
        escape_html_text(&text[cursor..span.byte_start], output);
        output.push_str("<span");
        write_html_style(theme.resolve(&line.scope_table, span.scope_stack), output);
        if options.include_scopes {
            output.push_str(" data-scopes=\"");
            for (index, scope) in line.scope_table.stack_names(span.scope_stack).enumerate() {
                if index != 0 {
                    output.push(' ');
                }
                escape_html_attribute(scope, output);
            }
            output.push('"');
        }
        output.push('>');
        escape_html_text(&text[span.byte_start..span.byte_end], output);
        output.push_str("</span>");
        cursor = span.byte_end;
    }
    escape_html_text(&text[cursor..], output);
}

#[cfg(feature = "html")]
fn render_html_line(
    text: &str,
    line: &HighlightedLine,
    options: &HtmlOptions,
    output: &mut String,
) {
    let mut cursor = 0;
    for span in line.spans() {
        let range = span.range();
        escape_html_text(&text[cursor..range.start], output);
        output.push_str("<span");
        write_html_style(span.style(), output);
        if options.include_scopes {
            output.push_str(" data-scopes=\"");
            for (index, scope) in line.scope_names(span.scope_stack()).enumerate() {
                if index != 0 {
                    output.push(' ');
                }
                escape_html_attribute(scope, output);
            }
            output.push('"');
        }
        output.push('>');
        escape_html_text(&text[range.clone()], output);
        output.push_str("</span>");
        cursor = range.end;
    }
    escape_html_text(&text[cursor..], output);
}

#[cfg(feature = "html")]
fn write_html_style(style: Style, output: &mut String) {
    if style == Style::default() {
        return;
    }
    output.push_str(" style=\"");
    if let Some(color) = style.foreground {
        let _ = write!(
            output,
            "color:#{:02x}{:02x}{:02x};",
            color.red, color.green, color.blue
        );
    }
    if let Some(color) = style.background {
        let _ = write!(
            output,
            "background-color:#{:02x}{:02x}{:02x};",
            color.red, color.green, color.blue
        );
    }
    if style.modifiers.contains(SyntaxModifiers::BOLD) {
        output.push_str("font-weight:bold;");
    }
    if style.modifiers.contains(SyntaxModifiers::ITALIC) {
        output.push_str("font-style:italic;");
    }
    if style.modifiers.contains(SyntaxModifiers::UNDERLINED)
        || style.modifiers.contains(SyntaxModifiers::CROSSED_OUT)
    {
        output.push_str("text-decoration:");
        if style.modifiers.contains(SyntaxModifiers::UNDERLINED) {
            output.push_str("underline");
        }
        if style.modifiers.contains(SyntaxModifiers::CROSSED_OUT) {
            if style.modifiers.contains(SyntaxModifiers::UNDERLINED) {
                output.push(' ');
            }
            output.push_str("line-through");
        }
        output.push(';');
    }
    output.push('"');
}

#[cfg(feature = "html")]
fn escape_html_text(text: &str, output: &mut String) {
    for character in text.chars() {
        match character {
            '&' => output.push_str("&amp;"),
            '<' => output.push_str("&lt;"),
            '>' => output.push_str("&gt;"),
            '"' => output.push_str("&quot;"),
            '\'' => output.push_str("&#39;"),
            character => output.push(character),
        }
    }
}

#[cfg(feature = "html")]
fn escape_html_attribute(text: &str, output: &mut String) {
    for character in text.chars() {
        match character {
            '&' => output.push_str("&amp;"),
            '<' => output.push_str("&lt;"),
            '>' => output.push_str("&gt;"),
            '"' => output.push_str("&quot;"),
            '\'' => output.push_str("&#39;"),
            '\0' => output.push('\u{fffd}'),
            character if character.is_control() => {
                let _ = write!(output, "&#x{:x};", u32::from(character));
            }
            character => output.push(character),
        }
    }
}

/// Options for [`render_ansi`].
#[cfg(feature = "ansi")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AnsiOptions {
    /// Emit 24-bit foreground, background, and modifier SGR sequences.
    pub colors: bool,
    /// Replace source C0/C1 control characters with visible Unicode control pictures.
    /// Newlines inserted between logical lines and horizontal tabs are preserved.
    pub sanitize_control_characters: bool,
}

#[cfg(feature = "ansi")]
impl Default for AnsiOptions {
    fn default() -> Self {
        Self {
            colors: true,
            sanitize_control_characters: true,
        }
    }
}

/// Renders highlighted source using 24-bit ANSI SGR sequences.
///
/// Control-character sanitization is enabled by default so untrusted source
/// cannot inject terminal escape sequences. Disable it only for trusted input.
#[cfg(feature = "ansi")]
pub fn render_ansi(
    source: &str,
    document: &HighlightedDocument,
    options: &AnsiOptions,
) -> Result<RenderedOutput> {
    let lines = validated_lines(source, document)?;
    let mut output = String::with_capacity(source.len().saturating_mul(2));
    for (line_index, (text, line)) in lines.into_iter().enumerate() {
        render_ansi_line(text, line, options, &mut output);
        if line_index + 1 < document.lines().len() {
            output.push('\n');
        }
    }
    Ok(RenderedOutput {
        content: output,
        status: document.status(),
    })
}

/// ANSI counterpart to the compact HTML renderer.
#[cfg(all(
    feature = "ansi",
    feature = "bundled-grammars",
    feature = "bundled-themes"
))]
pub(crate) fn render_ansi_compact(
    source: &str,
    tokens: &HighlightedText,
    status: HighlightStatus,
    theme: &Theme,
    options: &AnsiOptions,
) -> Result<RenderedOutput> {
    let mut output = String::with_capacity(source.len().saturating_mul(2));
    let mut source_lines = crate::engine::line::LineChunks::new(source);
    for (line_index, line) in tokens.lines.iter().enumerate() {
        let chunk = source_lines.next().ok_or_else(compact_line_count_error)?;
        render_ansi_compact_line(chunk.text, line, theme, options, &mut output);
        if line_index + 1 < tokens.lines.len() {
            output.push('\n');
        }
    }
    if source_lines.next().is_some() {
        return Err(compact_line_count_error());
    }
    Ok(RenderedOutput {
        content: output,
        status,
    })
}

#[cfg(all(
    feature = "ansi",
    feature = "bundled-grammars",
    feature = "bundled-themes"
))]
fn render_ansi_compact_line(
    text: &str,
    line: &EngineHighlightedLine,
    theme: &Theme,
    options: &AnsiOptions,
    output: &mut String,
) {
    let mut cursor = 0;
    let mut active_style = Style::default();
    for span in &line.segments {
        debug_assert!(
            cursor <= span.byte_start
                && span.byte_start <= span.byte_end
                && span.byte_end <= text.len()
                && text.is_char_boundary(span.byte_start)
                && text.is_char_boundary(span.byte_end)
        );
        write_ansi_source(&text[cursor..span.byte_start], options, output);
        let style = if options.colors {
            theme.resolve(&line.scope_table, span.scope_stack)
        } else {
            Style::default()
        };
        if style != active_style {
            if active_style != Style::default() {
                output.push_str("\x1b[0m");
            }
            write_ansi_style(style, output);
            active_style = style;
        }
        write_ansi_source(&text[span.byte_start..span.byte_end], options, output);
        cursor = span.byte_end;
    }
    if active_style != Style::default() {
        output.push_str("\x1b[0m");
    }
    write_ansi_source(&text[cursor..], options, output);
}

#[cfg(feature = "ansi")]
fn render_ansi_line(
    text: &str,
    line: &HighlightedLine,
    options: &AnsiOptions,
    output: &mut String,
) {
    let mut cursor = 0;
    let mut active_style = Style::default();
    for span in line.spans() {
        let range = span.range();
        write_ansi_source(&text[cursor..range.start], options, output);
        let style = if options.colors {
            span.style()
        } else {
            Style::default()
        };
        if style != active_style {
            if active_style != Style::default() {
                output.push_str("\x1b[0m");
            }
            write_ansi_style(style, output);
            active_style = style;
        }
        write_ansi_source(&text[range.clone()], options, output);
        cursor = range.end;
    }
    if active_style != Style::default() {
        output.push_str("\x1b[0m");
    }
    write_ansi_source(&text[cursor..], options, output);
}

#[cfg(feature = "ansi")]
fn write_ansi_style(style: Style, output: &mut String) {
    let has_codes =
        !style.modifiers.is_empty() || style.foreground.is_some() || style.background.is_some();
    if !has_codes {
        return;
    }
    output.push_str("\x1b[");
    let mut separator = "";
    for (enabled, code) in [
        (style.modifiers.contains(SyntaxModifiers::BOLD), "1"),
        (style.modifiers.contains(SyntaxModifiers::ITALIC), "3"),
        (style.modifiers.contains(SyntaxModifiers::UNDERLINED), "4"),
        (style.modifiers.contains(SyntaxModifiers::CROSSED_OUT), "9"),
    ] {
        if enabled {
            output.push_str(separator);
            output.push_str(code);
            separator = ";";
        }
    }
    if let Some(color) = style.foreground {
        output.push_str(separator);
        write_ansi_color("38", color, output);
        separator = ";";
    }
    if let Some(color) = style.background {
        output.push_str(separator);
        write_ansi_color("48", color, output);
    }
    output.push('m');
}

#[cfg(feature = "ansi")]
fn write_ansi_color(prefix: &str, color: RgbColor, output: &mut String) {
    let _ = write!(
        output,
        "{prefix};2;{};{};{}",
        color.red, color.green, color.blue
    );
}

#[cfg(feature = "ansi")]
fn write_ansi_source(text: &str, options: &AnsiOptions, output: &mut String) {
    if !options.sanitize_control_characters {
        output.push_str(text);
        return;
    }
    for character in text.chars() {
        if character == '\t' || !character.is_control() {
            output.push(character);
            continue;
        }
        match character {
            '\0'..='\x1f' => {
                let picture = char::from_u32(0x2400 + u32::from(character)).unwrap_or('\u{fffd}');
                output.push(picture);
            }
            '\x7f' => output.push('\u{2421}'),
            _ => {
                let _ = write!(output, "\\u{{{:x}}}", u32::from(character));
            }
        }
    }
}

#[cfg(all(
    feature = "bundled-grammars",
    feature = "bundled-themes",
    any(feature = "ansi", feature = "html")
))]
fn compact_line_count_error() -> Error {
    Error::Render("source and compact token document have different logical line counts".to_owned())
}

#[cfg(any(feature = "ansi", feature = "html"))]
fn validated_lines<'a>(
    source: &'a str,
    document: &'a HighlightedDocument,
) -> Result<Vec<(&'a str, &'a HighlightedLine)>> {
    let source_lines = crate::engine::line::LineChunks::new(source)
        .map(|line| line.text)
        .collect::<Vec<_>>();
    if source_lines.len() != document.lines().len() {
        return Err(Error::Render(format!(
            "source has {} logical lines but the highlighted document has {}",
            source_lines.len(),
            document.lines().len()
        )));
    }

    source_lines
        .into_iter()
        .zip(document.lines())
        .enumerate()
        .map(|(line_index, (text, line))| {
            let mut cursor = 0;
            for span in line.spans() {
                let range = span.range();
                if range.start < cursor
                    || range.start > range.end
                    || range.end > text.len()
                    || !text.is_char_boundary(range.start)
                    || !text.is_char_boundary(range.end)
                {
                    return Err(Error::Render(format!(
                        "invalid highlighted byte range {range:?} on line {line_index}"
                    )));
                }
                cursor = range.end;
            }
            Ok((text, line))
        })
        .collect()
}

#[cfg(all(
    test,
    feature = "ansi",
    feature = "html",
    feature = "bundled-grammars",
    feature = "bundled-themes"
))]
mod tests {
    use super::*;
    use crate::Highlighter;

    #[test]
    fn html_escapes_source_and_can_expose_exact_scopes() {
        let source = "fn main() { println!(\"<script>&\"); }\n";
        let mut highlighter = Highlighter::bundled().unwrap();
        let document = highlighter
            .highlight("rust", source, "github-dark")
            .unwrap();
        let output = render_html(
            source,
            &document,
            &HtmlOptions {
                class: Some("syntaxmate\" data-injected=\"no".to_owned()),
                include_scopes: true,
                ..HtmlOptions::default()
            },
        )
        .unwrap();
        assert!(
            output
                .as_str()
                .starts_with("<pre class=\"syntaxmate&quot; data-injected=&quot;no\"><code>")
        );
        assert!(!output.as_str().contains(" data-injected=\"no\""));
        assert!(output.as_str().contains("&lt;script&gt;&amp;"));
        assert!(!output.as_str().contains("<script>"));
        assert!(output.as_str().contains("data-scopes=\""));
        assert!(output.as_str().ends_with("</code></pre>"));
        assert!(output.status().is_complete());
    }

    #[test]
    fn ansi_style_writer_preserves_sgr_code_order_without_temporary_strings() {
        let mut output = String::new();
        write_ansi_style(
            Style {
                foreground: Some(RgbColor {
                    red: 1,
                    green: 2,
                    blue: 3,
                }),
                background: Some(RgbColor {
                    red: 4,
                    green: 5,
                    blue: 6,
                }),
                modifiers: SyntaxModifiers::BOLD,
            },
            &mut output,
        );
        assert_eq!(output, "\x1b[1;38;2;1;2;3;48;2;4;5;6m");

        for (modifier, expected) in [
            (SyntaxModifiers::ITALIC, "\x1b[3m"),
            (SyntaxModifiers::UNDERLINED, "\x1b[4m"),
            (SyntaxModifiers::CROSSED_OUT, "\x1b[9m"),
        ] {
            output.clear();
            write_ansi_style(
                Style {
                    modifiers: modifier,
                    ..Style::default()
                },
                &mut output,
            );
            assert_eq!(output, expected);
        }

        output.clear();
        write_ansi_style(Style::default(), &mut output);
        assert!(output.is_empty());
    }

    #[test]
    fn ansi_sanitizes_source_escape_sequences() {
        let source = "let value = \"\x1b[31m\";";
        let mut highlighter = Highlighter::bundled().unwrap();
        let document = highlighter
            .highlight("rust", source, "github-dark")
            .unwrap();
        let output = render_ansi(source, &document, &AnsiOptions::default()).unwrap();
        assert!(output.as_str().contains('␛'));
        assert!(!output.as_str().contains("\x1b[31m"));
        assert!(output.as_str().contains("\x1b["));
    }

    #[test]
    fn direct_compact_rendering_is_byte_exact_with_owned_rendering() {
        let source = "fn main() {\n\tprintln!(\"λ<&>\");\n}\n";

        let mut direct = Highlighter::bundled().unwrap();
        let direct_html = direct
            .highlight_html("rust", source, "github-dark")
            .unwrap();
        let direct_ansi = direct
            .highlight_ansi("rust", source, "github-dark")
            .unwrap();

        let mut owned = Highlighter::bundled().unwrap();
        let document = owned.highlight("rust", source, "github-dark").unwrap();
        let owned_html = render_html(source, &document, &HtmlOptions::default()).unwrap();
        let owned_ansi = render_ansi(source, &document, &AnsiOptions::default()).unwrap();

        assert_eq!(direct_html, owned_html);
        assert_eq!(direct_ansi, owned_ansi);

        let html_options = HtmlOptions {
            include_wrapper: false,
            class: None,
            include_scopes: true,
        };
        let ansi_options = AnsiOptions {
            colors: false,
            sanitize_control_characters: false,
        };
        let direct_html = direct
            .highlight_html_with_options("rust", source, "github-dark", &html_options)
            .unwrap();
        let direct_ansi = direct
            .highlight_ansi_with_options("rust", source, "github-dark", &ansi_options)
            .unwrap();
        assert_eq!(
            direct_html,
            render_html(source, &document, &html_options).unwrap()
        );
        assert_eq!(
            direct_ansi,
            render_ansi(source, &document, &ansi_options).unwrap()
        );
    }

    #[test]
    fn renderers_reject_a_document_from_different_source() {
        let mut highlighter = Highlighter::bundled().unwrap();
        let document = highlighter
            .highlight("rust", "let x = 1;", "github-dark")
            .unwrap();
        let error = render_html("different\nshape", &document, &HtmlOptions::default())
            .expect_err("line mismatch must fail");
        assert!(matches!(error, Error::Render(_)));
    }
}