add default

This commit is contained in:
KitsuneCafe 2024-02-04 14:55:18 -05:00
parent 39c3a9e4b2
commit a0f12e8ecd

View file

@ -3,11 +3,7 @@ use std::io::Write;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::{Captures, Regex, RegexBuilder}; use regex::{Captures, Regex, RegexBuilder};
use roxy_core::roxy::Parse; use roxy_core::roxy::Parse;
use syntect::{ use syntect::{highlighting::ThemeSet, html::highlighted_html_for_string, parsing::SyntaxSet};
highlighting::ThemeSet,
html::highlighted_html_for_string,
parsing::SyntaxSet
};
const CODE_BLOCK_RE: Lazy<Regex> = Lazy::new(|| { const CODE_BLOCK_RE: Lazy<Regex> = Lazy::new(|| {
RegexBuilder::new(r"^```(.+?)\n(.+?)\n```") RegexBuilder::new(r"^```(.+?)\n(.+?)\n```")
@ -24,17 +20,32 @@ pub struct SyntectParser<'a> {
} }
impl<'a> 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 { Self {
syntax_set: SyntaxSet::load_defaults_newlines(), syntax_set,
theme_set: ThemeSet::load_defaults(), theme_set,
theme, 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> { impl<'a> Parse for SyntectParser<'a> {
fn parse(&mut self, _path: &str, src: &[u8], dst: &mut Vec<u8>) -> Result<(), roxy_core::error::Error> { fn parse(
&mut self,
_path: &str,
src: &[u8],
dst: &mut Vec<u8>,
) -> Result<(), roxy_core::error::Error> {
let file = String::from_utf8_lossy(src).to_string(); let file = String::from_utf8_lossy(src).to_string();
let result = CODE_BLOCK_RE let result = CODE_BLOCK_RE
.replace_all(file.as_str(), |captures: &Captures| { .replace_all(file.as_str(), |captures: &Captures| {
@ -44,11 +55,11 @@ impl<'a> Parse for SyntectParser<'a> {
&self.syntax_set, &self.syntax_set,
&syntax, &syntax,
&self.theme_set.themes[self.theme], &self.theme_set.themes[self.theme],
).unwrap() )
.unwrap()
}) })
.to_string(); .to_string();
dst.write_all(result.as_bytes())?; dst.write_all(result.as_bytes())?;
Ok(()) Ok(())
} }
} }