comfit-core 0.1.0

Comfit highlighting engine, theme model, and renderers
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
//! Render escaped HTML with classes, inline styles, or CSS variables.

use std::{collections::HashMap, fmt::Write};

use crate::{Error, Highlighted, Modifier, Style, Theme};

use super::RenderLine;

/// HTML elements, classes, bracket colors, and token styling options.
#[derive(Debug, Clone)]
pub struct RenderHtmlConfig {
    /// Outer tag name. Defaults to `pre`.
    pub pre_tag: String,
    /// Line tag name. Defaults to `span`.
    pub line_tag: String,
    /// Optional class attribute on the outer element.
    pub pre_class: Option<String>,
    /// Prefix for token, rainbow, and line classes, escaped as attribute text.
    pub class_prefix: String,
    /// Number of colors to cycle through for bracket classes. Zero preserves depth.
    pub rainbow_colors: usize,
    /// Style tokens with classes, inline theme values, or CSS variables. Defaults to classes.
    pub style: RenderHtmlStyle,
}

impl Default for RenderHtmlConfig {
    fn default() -> Self {
        Self {
            pre_tag: "pre".into(),
            line_tag: "span".into(),
            pre_class: None,
            class_prefix: String::new(),
            rainbow_colors: 0,
            style: RenderHtmlStyle::default(),
        }
    }
}

/// How token styles are represented in HTML.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum RenderHtmlStyle {
    /// Emit capture classes for a caller-provided stylesheet.
    #[default]
    Class,
    /// Emit theme styles when a theme is supplied. Otherwise emit classes.
    Inline,
    /// Emit CSS variable references for styles defined in a stylesheet.
    Variables,
}

impl Highlighted<'_> {
    /// Render escaped HTML without a theme.
    ///
    /// Token and line classes require a caller-provided stylesheet. An inline-style
    /// configuration falls back to classes because no theme is supplied.
    /// [`RenderHtmlStyle::Variables`] emits variable references instead of theme colors.
    pub fn render_html_classes(&self, config: &RenderHtmlConfig) -> String {
        render(self.lines.iter(), None, &[], config, false)
    }

    /// Render escaped HTML with a theme and line emphasis.
    ///
    /// [`RenderHtmlStyle`] selects how token styles are emitted. A nonempty
    /// theme rainbow palette determines rainbow wrapping in class or inline mode.
    /// Variable mode ignores the supplied theme and uses `rainbow_colors` instead.
    pub fn render_html(&self, theme: &Theme, config: &RenderHtmlConfig) -> String {
        render(self.lines.iter(), Some(theme), &[], config, false)
    }

    /// Render inline styles for named themes with a selected default.
    ///
    /// The default theme uses literal CSS declarations. Other themes are embedded
    /// as `--comfit-<name>` properties on each element. Use CSS to activate them.
    /// The output contains no stylesheet or script. Each theme cycles through its own palette.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Html`] unless the style is [`RenderHtmlStyle::Inline`],
    /// the default theme exists, and names are nonempty ASCII letters, digits,
    /// underscores, or hyphens with no duplicate or colliding variable names.
    pub fn render_html_themes(
        &self,
        themes: &[(&str, &Theme)],
        default_theme: &str,
        config: &RenderHtmlConfig,
    ) -> Result<String, Error> {
        render_themes(self.lines.iter(), themes, default_theme, config)
    }
}

pub(crate) fn render_themes<'a, L: RenderLine + 'a>(
    lines: impl Iterator<Item = &'a L>,
    themes: &[(&str, &Theme)],
    default_theme: &str,
    config: &RenderHtmlConfig,
) -> Result<String, Error> {
    if config.style != RenderHtmlStyle::Inline {
        return Err(Error::Html("named themes require inline style".into()));
    }

    let mut names = std::collections::HashSet::new();

    for (name, _) in themes {
        if name.is_empty()
            || !name
                .bytes()
                .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
        {
            return Err(Error::Html(format!("invalid theme name: {name:?}")));
        }

        for (_, suffix, _) in PROPERTIES {
            if !names.insert(format!("{name}{suffix}")) {
                return Err(Error::Html(format!(
                    "duplicate or colliding theme name: {name:?}"
                )));
            }
        }
    }

    let theme = themes
        .iter()
        .find(|(name, _)| *name == default_theme)
        .ok_or_else(|| Error::Html(format!("unknown default theme: {default_theme:?}")))?
        .1;
    let alternatives: Vec<_> = themes
        .iter()
        .copied()
        .filter(|(name, _)| *name != default_theme)
        .collect();
    Ok(render(lines, Some(theme), &alternatives, config, true))
}

pub(crate) fn render<'a, L: RenderLine + 'a>(
    lines: impl Iterator<Item = &'a L>,
    theme: Option<&Theme>,
    alternatives: &[(&str, &Theme)],
    config: &RenderHtmlConfig,
    complete: bool,
) -> String {
    let theme = theme.filter(|_| config.style != RenderHtmlStyle::Variables);
    let mut html = String::from("<");
    html.push_str(&config.pre_tag);
    let mut css = String::new();
    let mut scratch = String::new();

    if let Some(class) = &config.pre_class {
        attribute(&mut html, "class", class);
    }

    if config.style == RenderHtmlStyle::Variables {
        variable_style(&mut css, None, None, None);
        attribute(&mut html, "style", &css);
    } else if let Some(theme) = theme {
        inline_style(&mut css, &theme.default, None, complete, &mut scratch);

        for (name, alternative) in alternatives {
            inline_style(
                &mut css,
                &alternative.default,
                Some(name),
                true,
                &mut scratch,
            );
        }
        attribute(&mut html, "style", &css);
    }

    html.push_str("><code>");
    let mut prefix = String::new();
    let mut styles = HashMap::new();

    for (index, line) in lines.enumerate() {
        if index > 0 {
            html.push('\n');
        }

        html.push('<');
        html.push_str(&config.line_tag);
        html.push_str(" class=\"");
        escape(&mut html, &config.class_prefix);
        html.push_str("line");

        if line.highlighted() {
            html.push(' ');
            escape(&mut html, &config.class_prefix);
            html.push_str("line-highlight");
        }
        html.push('"');

        if config.style == RenderHtmlStyle::Variables && line.highlighted() {
            css.clear();
            variable_style(&mut css, None, Some("line-highlight"), None);
            attribute(&mut html, "style", &css);
        } else if let Some(theme) = theme
            && (line.highlighted() || complete)
        {
            css.clear();
            let empty = Style::default();
            let style = if line.highlighted() {
                &theme.line_highlight
            } else {
                &empty
            };
            inline_style(&mut css, style, None, complete, &mut scratch);

            for (name, alternative) in alternatives {
                let style = if line.highlighted() {
                    &alternative.line_highlight
                } else {
                    &empty
                };
                inline_style(&mut css, style, Some(name), true, &mut scratch);
            }
            attribute(&mut html, "style", &css);
        }

        html.push('>');

        for token in line.tokens() {
            html.push_str("<span");

            if config.style == RenderHtmlStyle::Inline
                && let Some(theme) = theme
            {
                let rainbow = if alternatives.is_empty() {
                    token.rainbow.and_then(|depth| {
                        (!theme.rainbow.is_empty()).then(|| depth % theme.rainbow.len())
                    })
                } else {
                    token.rainbow
                };
                let css = styles.entry((token.highlight, rainbow)).or_insert_with(|| {
                    let mut css = String::new();
                    let style = token_style(theme, token.highlight, token.rainbow);
                    inline_style(&mut css, &style, None, complete, &mut scratch);

                    for (name, alternative) in alternatives {
                        let style = token_style(alternative, token.highlight, token.rainbow);
                        inline_style(&mut css, &style, Some(name), true, &mut scratch);
                    }
                    css
                });
                attribute(&mut html, "style", css);
            } else {
                if config.style == RenderHtmlStyle::Variables {
                    css.clear();
                    let rainbow = token.rainbow.map(|depth| {
                        if config.rainbow_colors == 0 {
                            depth
                        } else {
                            depth % config.rainbow_colors
                        }
                    });
                    variable_style(&mut css, token.highlight, None, rainbow);
                    attribute(&mut html, "style", &css);
                }

                if token.highlight.is_some() || token.rainbow.is_some() {
                    html.push_str(" class=\"");
                }

                if let Some(capture) = token.highlight {
                    prefix.clear();

                    for (index, part) in capture.split('.').enumerate() {
                        if index > 0 {
                            html.push(' ');
                        }
                        if !prefix.is_empty() {
                            prefix.push('-');
                        }
                        prefix.push_str(part);
                        escape(&mut html, &config.class_prefix);
                        escape(&mut html, &prefix);
                    }
                }

                if let Some(depth) = token.rainbow {
                    let count = theme
                        .filter(|theme| !theme.rainbow.is_empty())
                        .map_or(config.rainbow_colors, |theme| theme.rainbow.len());
                    let color = if count == 0 { depth } else { depth % count };
                    if token.highlight.is_some() {
                        html.push(' ');
                    }
                    escape(&mut html, &config.class_prefix);
                    html.push_str("rainbow ");
                    escape(&mut html, &config.class_prefix);
                    write!(html, "rainbow-{color}").unwrap();
                }

                if token.highlight.is_some() || token.rainbow.is_some() {
                    html.push('"');
                }
            }

            html.push('>');
            escape(&mut html, token.text);
            html.push_str("</span>");
        }

        html.push_str("</");
        html.push_str(&config.line_tag);
        html.push('>');
    }

    html.push_str("</code></");
    html.push_str(&config.pre_tag);
    html.push('>');

    html
}

// CSS property, alternate-theme variable suffix, and the explicit reset value.
const PROPERTIES: [(&str, &str, &str); 9] = [
    ("color", "", "inherit"),
    ("background-color", "-bg", "transparent"),
    ("font-weight", "-font-weight", "normal"),
    ("font-style", "-font-style", "normal"),
    ("opacity", "-opacity", "1"),
    ("visibility", "-visibility", "visible"),
    ("text-decoration-line", "-text-decoration", "none"),
    ("text-decoration-style", "-text-decoration-style", "solid"),
    (
        "text-decoration-color",
        "-text-decoration-color",
        "currentColor",
    ),
];

fn token_style(theme: &Theme, capture: Option<&str>, rainbow: Option<usize>) -> Style {
    let mut style = capture.map_or_else(|| theme.default.clone(), |capture| theme.style(capture));

    // The default background belongs on <pre>; reversed colors still need it to swap.
    if !style.modifiers.contains(&Modifier::Reversed) {
        style.bg = capture.and_then(|capture| {
            let capture = capture.trim_start_matches('.');
            std::iter::once(capture.len())
                .chain(capture.rmatch_indices('.').map(|(end, _)| end))
                .find_map(|end| theme.styles.get(&capture[..end]).and_then(|style| style.bg))
        });
    }

    if let Some(depth) = rainbow
        && !theme.rainbow.is_empty()
    {
        style.fg = Some(theme.rainbow[depth % theme.rainbow.len()]);
    }

    style
}

fn inline_style(
    css: &mut String,
    style: &Style,
    name: Option<&str>,
    complete: bool,
    scratch: &mut String,
) {
    if !complete {
        style.write_css(css);
        return;
    }

    scratch.clear();
    style.write_css(scratch);
    let mut values = PROPERTIES.map(|(_, _, fallback)| fallback);

    for declaration in scratch.split(';') {
        if let Some((property, value)) = declaration.split_once(':')
            && let Some(index) = PROPERTIES.iter().position(|(key, _, _)| *key == property)
        {
            values[index] = value;
        }
    }

    for ((property, suffix, _), value) in PROPERTIES.iter().zip(values) {
        if let Some(name) = name {
            write!(css, "--comfit-{name}{suffix}:{value};").unwrap();
        } else {
            write!(css, "{property}:{value};").unwrap();
        }
    }
}

fn variable_style(
    css: &mut String,
    capture: Option<&str>,
    scope: Option<&str>,
    rainbow: Option<usize>,
) {
    let capture = capture
        .map(|capture| capture.trim_start_matches('.'))
        .filter(|capture| !capture.is_empty());

    for (property, suffix, fallback) in PROPERTIES {
        let suffix = if suffix.is_empty() {
            "fg"
        } else {
            &suffix[1..]
        };
        write!(css, "{property}:").unwrap();
        let rainbow = rainbow.filter(|_| property == "color");

        if let Some(depth) = rainbow {
            write!(css, "var(--comfit-rainbow-{depth},").unwrap();
        }

        if let Some(scope) = scope {
            write!(css, "var(--comfit-{scope}-{suffix},{fallback})").unwrap();
        } else {
            let mut parentheses = 0;

            if let Some(capture) = capture {
                for end in std::iter::once(capture.len())
                    .chain(capture.rmatch_indices('.').map(|(end, _)| end))
                {
                    css.push_str("var(--comfit-");

                    for character in capture[..end].chars() {
                        if character == '.' {
                            css.push('-');
                        } else if character.is_ascii_alphanumeric()
                            || matches!(character, '-' | '_')
                        {
                            css.push(character);
                        } else {
                            write!(css, "\\{:x} ", character as u32).unwrap();
                        }
                    }

                    write!(css, "-{suffix},").unwrap();
                    parentheses += 1;
                }
            }

            write!(css, "var(--comfit-{suffix},{fallback})").unwrap();

            for _ in 0..parentheses {
                css.push(')');
            }
        }

        if rainbow.is_some() {
            css.push(')');
        }
        css.push(';');
    }
}

fn attribute(html: &mut String, name: &str, value: &str) {
    html.push(' ');
    html.push_str(name);
    html.push_str("=\"");
    escape(html, value);
    html.push('"');
}

fn escape(html: &mut String, text: &str) {
    for character in text.chars() {
        match character {
            '&' => html.push_str("&amp;"),
            '<' => html.push_str("&lt;"),
            '>' => html.push_str("&gt;"),
            '"' => html.push_str("&quot;"),
            '\'' => html.push_str("&#39;"),
            // HTML parsing would normalize a literal CR independently of the next line's LF.
            '\r' => html.push_str("&#13;"),
            _ => html.push(character),
        }
    }
}