From a0f12e8ecdfb1b25041da20262ded10d0d135919 Mon Sep 17 00:00:00 2001 From: KitsuneCafe <10284516+kitsunecafe@users.noreply.github.com> Date: Sun, 4 Feb 2024 14:55:18 -0500 Subject: [PATCH] add default --- src/lib.rs | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 343882f..83b078c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,11 +3,7 @@ use std::io::Write; use once_cell::sync::Lazy; use regex::{Captures, Regex, RegexBuilder}; use roxy_core::roxy::Parse; -use syntect::{ - highlighting::ThemeSet, - html::highlighted_html_for_string, - parsing::SyntaxSet -}; +use syntect::{highlighting::ThemeSet, html::highlighted_html_for_string, parsing::SyntaxSet}; const CODE_BLOCK_RE: Lazy = Lazy::new(|| { RegexBuilder::new(r"^```(.+?)\n(.+?)\n```") @@ -24,17 +20,32 @@ pub struct SyntectParser<'a> { } impl<'a> SyntectParser<'a> { - pub fn new(theme: &'a str) -> Self { + pub fn new(syntax_set: SyntaxSet, theme_set: ThemeSet, theme: &'a str) -> Self { Self { - syntax_set: SyntaxSet::load_defaults_newlines(), - theme_set: ThemeSet::load_defaults(), + syntax_set, + theme_set, theme, } } } +impl<'a> Default for SyntectParser<'a> { + fn default() -> Self { + Self { + syntax_set: SyntaxSet::load_defaults_newlines(), + theme_set: ThemeSet::load_defaults(), + theme: "base16-ocean.dark", + } + } +} + impl<'a> Parse for SyntectParser<'a> { - fn parse(&mut self, _path: &str, src: &[u8], dst: &mut Vec) -> Result<(), roxy_core::error::Error> { + fn parse( + &mut self, + _path: &str, + src: &[u8], + dst: &mut Vec, + ) -> Result<(), roxy_core::error::Error> { let file = String::from_utf8_lossy(src).to_string(); let result = CODE_BLOCK_RE .replace_all(file.as_str(), |captures: &Captures| { @@ -44,11 +55,11 @@ impl<'a> Parse for SyntectParser<'a> { &self.syntax_set, &syntax, &self.theme_set.themes[self.theme], - ).unwrap() + ) + .unwrap() }) .to_string(); dst.write_all(result.as_bytes())?; Ok(()) } } -