pulldown_html_ext/html/
syntect.rs

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
use crate::html::{config, HtmlError};
use lazy_static::lazy_static;
use pulldown_cmark_escape::StrWrite;
use serde::{Deserialize, Deserializer};
use syntect::highlighting::{Theme, ThemeSet};
use syntect::html::{ClassStyle, ClassedHTMLGenerator};
use syntect::parsing::SyntaxSet;
use syntect::util::LinesWithEndings;

use crate::html::{DefaultHtmlWriter, HtmlConfig, HtmlState, HtmlWriter};

lazy_static! {
    static ref SYNTAX_SET: SyntaxSet = SyntaxSet::load_defaults_newlines();
    static ref THEME_SET: ThemeSet = ThemeSet::load_defaults();
}

fn deserialize_class_style<'de, D>(deserializer: D) -> Result<ClassStyle, D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize)]
    #[serde(rename_all = "snake_case")]
    enum ClassStyleHelper {
        Spaced,
        SpacedPrefix,
    }

    let style = ClassStyleHelper::deserialize(deserializer)?;
    Ok(match style {
        ClassStyleHelper::Spaced => ClassStyle::Spaced,
        ClassStyleHelper::SpacedPrefix => ClassStyle::SpacedPrefixed { prefix: "" },
    })
}

/// Configuration options for syntax highlighting that can be cloned
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct SyntectConfigStyle {
    /// Name of the theme to use (e.g., "base16-ocean.dark")
    pub theme: String,
    /// Style of CSS classes to generate
    #[serde(
        deserialize_with = "deserialize_class_style",
        default = "default_class_style"
    )]
    pub class_style: ClassStyle,
    /// Whether to include CSS in the output
    #[serde(default = "default_inject_css")]
    pub inject_css: bool,
}

fn default_class_style() -> ClassStyle {
    ClassStyle::Spaced
}

fn default_inject_css() -> bool {
    true
}

/// Complete syntax highlighting configuration including non-clonable parts
#[derive(Debug, Default)]
pub struct SyntectConfig {
    /// Style configuration
    pub style: SyntectConfigStyle,
    /// Custom syntax set to use (optional) - primarily for testing
    #[doc(hidden)]
    pub syntax_set: Option<SyntaxSet>,
    /// Custom theme set to use (optional) - primarily for testing
    #[doc(hidden)]
    pub theme_set: Option<ThemeSet>,
}

impl Default for SyntectConfigStyle {
    fn default() -> Self {
        Self {
            theme: "base16-ocean.dark".to_string(),
            class_style: ClassStyle::Spaced,
            inject_css: true,
        }
    }
}

impl HtmlConfig {
    /// Create a new configuration with syntect syntax highlighting enabled
    pub fn with_syntect(syntect_config: SyntectConfig) -> Self {
        HtmlConfig {
            syntect: Some(syntect_config.style),
            ..Default::default()
        }
    }
}

/// Writer that adds syntax highlighting to code blocks
pub struct SyntectWriter<'a, W: StrWrite> {
    inner: DefaultHtmlWriter<'a, W>,
    style: SyntectConfigStyle,
    syntax_set: Option<&'a SyntaxSet>,
    theme_set: Option<&'a ThemeSet>,
    current_lang: Option<String>,
}

impl<'a, W: StrWrite> SyntectWriter<'a, W> {
    pub fn new(writer: W, config: &'a config::HtmlConfig) -> Self {
        let style = config.syntect.clone().unwrap_or_default();

        Self {
            inner: DefaultHtmlWriter::new(writer, config),
            style,
            syntax_set: None,
            theme_set: None,
            current_lang: None,
        }
    }

    pub fn with_custom_sets(
        writer: W,
        config: &'a config::HtmlConfig,
        syntax_set: Option<&'a SyntaxSet>,
        theme_set: Option<&'a ThemeSet>,
    ) -> Self {
        let style = config.syntect.clone().unwrap_or_default();

        Self {
            inner: DefaultHtmlWriter::new(writer, config),
            style,
            syntax_set,
            theme_set,
            current_lang: None,
        }
    }

    fn highlight_code(&self, code: &str, lang: Option<&str>) -> String {
        let syntax_set = self.syntax_set.unwrap_or(&SYNTAX_SET);

        let syntax = match lang {
            Some(lang) => syntax_set
                .find_syntax_by_token(lang)
                .or_else(|| syntax_set.find_syntax_by_extension(lang)),
            None => None,
        }
        .unwrap_or_else(|| syntax_set.find_syntax_plain_text());

        let mut html_generator =
            ClassedHTMLGenerator::new_with_class_style(syntax, syntax_set, self.style.class_style);

        for line in LinesWithEndings::from(code) {
            let _ = html_generator.parse_html_for_line_which_includes_newline(line);
        }

        html_generator.finalize()
    }

    fn get_theme(&self) -> Result<&Theme, String> {
        let theme_set = self.theme_set.unwrap_or(&THEME_SET);
        theme_set
            .themes
            .get(&self.style.theme)
            .ok_or_else(|| format!("Theme '{}' not found", self.style.theme))
    }

    pub fn get_theme_css(&self) -> Result<String, String> {
        let theme = self.get_theme()?;
        syntect::html::css_for_theme_with_class_style(theme, self.style.class_style)
            .map_err(|e| e.to_string())
    }
}

impl<'a, W: StrWrite> HtmlWriter<W> for SyntectWriter<'a, W> {
    fn get_writer(&mut self) -> &mut W {
        self.inner.get_writer()
    }

    fn get_config(&self) -> &HtmlConfig {
        self.inner.get_config()
    }

    fn get_state(&mut self) -> &mut HtmlState {
        self.inner.get_state()
    }

    fn start_code_block(&mut self, kind: pulldown_cmark::CodeBlockKind) -> Result<(), HtmlError> {
        self.current_lang = match kind {
            pulldown_cmark::CodeBlockKind::Fenced(ref info) => {
                if info.is_empty() {
                    None
                } else {
                    Some(info.to_string())
                }
            }
            _ => None,
        };

        self.write_str("<pre")?;
        self.write_attributes("pre")?;
        self.write_str("><code")?;

        if let Some(ref lang) = self.current_lang {
            self.write_str(&format!(" class=\"language-{}\"", lang))?;
        }

        self.write_attributes("code")?;
        self.write_str(">")?;

        self.get_state().currently_in_code_block = true;
        Ok(())
    }

    fn text(&mut self, text: &str) -> Result<(), HtmlError> {
        if self.get_state().currently_in_code_block {
            let highlighted = self.highlight_code(text, self.current_lang.as_deref());
            self.write_str(&highlighted)
        } else {
            self.inner.text(text)
        }
    }

    fn end_code_block(&mut self) -> Result<(), HtmlError> {
        self.write_str("</code></pre>")?;
        self.current_lang = None;
        self.get_state().currently_in_code_block = false;
        Ok(())
    }
}

/// Convenience function to render Markdown with syntax highlighting
pub fn push_html_with_highlighting(
    markdown: &str,
    config: &HtmlConfig,
) -> Result<String, HtmlError> {
    use pulldown_cmark::Parser;
    use pulldown_cmark_escape::FmtWriter;

    let mut output = String::new();
    let writer = SyntectWriter::new(FmtWriter(&mut output), config);
    let mut renderer = crate::html::create_html_renderer(writer);

    let parser = Parser::new(markdown);
    renderer.run(parser)?;

    // Add CSS if configured
    if let Some(ref style) = config.syntect {
        if style.inject_css {
            match renderer.writer.get_theme_css() {
                Ok(css) => return Ok(format!("<style>{}</style>\n{}", css, output)),
                Err(e) => eprintln!("Failed to generate syntax highlighting CSS: {}", e),
            }
        }
    }

    Ok(output)
}