diff --git a/src/plugins/syntect.rs b/src/plugins/syntect.rs index d4f6d46c..b9a0abe6 100644 --- a/src/plugins/syntect.rs +++ b/src/plugins/syntect.rs @@ -107,3 +107,52 @@ impl SyntaxHighlighterAdapter for SyntectAdapter<'_> { build_opening_tag("code", attributes) } } + +#[derive(Debug, Default)] +/// A builder for [`SyntectAdapter`]. +/// +/// Allows customization of `Theme`, [`ThemeSet`], and [`SyntaxSet`]. +pub struct SyntectAdapterBuilder<'a> { + theme: Option<&'a str>, + syntax_set: Option, + theme_set: Option, +} + +impl<'a> SyntectAdapterBuilder<'a> { + /// Creates a new empty [`SyntectAdapterBuilder`] + pub fn new() -> Self { + Default::default() + } + + /// Sets the theme + pub fn theme(mut self, s: &'a str) -> Self { + self.theme.replace(s); + self + } + + /// Sets the syntax set + pub fn syntax_set(mut self, s: SyntaxSet) -> Self { + self.syntax_set.replace(s); + self + } + + /// Sets the theme set + pub fn theme_set(mut self, s: ThemeSet) -> Self { + self.theme_set.replace(s); + self + } + + /// Builds the [`SyntectAdapter`]. Default values: + /// - `theme`: `InspiredGitHub` + /// - `syntax_set`: [`SyntaxSet::load_defaults_newlines()`] + /// - `theme_set`: [`ThemeSet::load_defaults()`] + pub fn build(self) -> SyntectAdapter<'a> { + SyntectAdapter { + theme: self.theme.unwrap_or("InspiredGitHub"), + syntax_set: self + .syntax_set + .unwrap_or_else(SyntaxSet::load_defaults_newlines), + theme_set: self.theme_set.unwrap_or_else(ThemeSet::load_defaults), + } + } +}