syntaxmate 0.1.0

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
//! 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(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(),
    })
}

#[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(),
    })
}

#[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 mut codes = Vec::with_capacity(6);
    if style.modifiers.contains(SyntaxModifiers::BOLD) {
        codes.push("1".to_owned());
    }
    if style.modifiers.contains(SyntaxModifiers::ITALIC) {
        codes.push("3".to_owned());
    }
    if style.modifiers.contains(SyntaxModifiers::UNDERLINED) {
        codes.push("4".to_owned());
    }
    if style.modifiers.contains(SyntaxModifiers::CROSSED_OUT) {
        codes.push("9".to_owned());
    }
    if let Some(color) = style.foreground {
        codes.push(ansi_color("38", color));
    }
    if let Some(color) = style.background {
        codes.push(ansi_color("48", color));
    }
    if !codes.is_empty() {
        output.push_str("\x1b[");
        output.push_str(&codes.join(";"));
        output.push('m');
    }
}

#[cfg(feature = "ansi")]
fn ansi_color(prefix: &str, color: RgbColor) -> String {
    format!("{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(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_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 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(_)));
    }
}