add default

This commit is contained in:
KitsuneCafe 2024-02-04 14:55:18 -05:00
parent 39c3a9e4b2
commit a0f12e8ecd
1 changed files with 22 additions and 11 deletions

View File

@ -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<Regex> = 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<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 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(())
}
}