diff --git a/Cargo.lock b/Cargo.lock index 700cecd..6cc8577 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -598,9 +598,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pest" -version = "2.7.6" +version = "2.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f200d8d83c44a45b21764d1916299752ca035d15ecd46faca3e9a2a2bf6ad06" +checksum = "219c0dcc30b6a27553f9cc242972b67f75b60eb0db71f0b5462f38b058c41546" dependencies = [ "memchr", "thiserror", @@ -609,9 +609,9 @@ dependencies = [ [[package]] name = "pest_derive" -version = "2.7.6" +version = "2.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcd6ab1236bbdb3a49027e920e693192ebfe8913f6d60e294de57463a493cfde" +checksum = "22e1288dbd7786462961e69bfd4df7848c1e37e8b74303dbdab82c3a9cdd2809" dependencies = [ "pest", "pest_generator", @@ -619,9 +619,9 @@ dependencies = [ [[package]] name = "pest_generator" -version = "2.7.6" +version = "2.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a31940305ffc96863a735bef7c7994a00b325a7138fdbc5bda0f1a0476d3275" +checksum = "1381c29a877c6d34b8c176e734f35d7f7f5b3adaefe940cb4d1bb7af94678e2e" dependencies = [ "pest", "pest_meta", @@ -632,9 +632,9 @@ dependencies = [ [[package]] name = "pest_meta" -version = "2.7.6" +version = "2.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7ff62f5259e53b78d1af898941cdcdccfae7385cf7d793a6e55de5d05bb4b7d" +checksum = "d0934d6907f148c22a3acbda520c7eed243ad7487a30f51f6ce52b58b7077a8a" dependencies = [ "once_cell", "pest", @@ -835,7 +835,7 @@ dependencies = [ [[package]] name = "roxy_core" version = "0.1.0" -source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-core.git#3059a3b77492be751cba36d012557bd9622c62a7" +source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-core.git#7839db8b062698adfe81a86d2a0cf041a6711456" [[package]] name = "roxy_markdown_parser" @@ -859,7 +859,7 @@ dependencies = [ [[package]] name = "roxy_syntect" version = "0.1.0" -source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-syntect.git#39c3a9e4b2f294936df5141b717efcbbe22deea7" +source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-syntect.git#48601fc5e6e0ee0e753c892f0eb42a9b0b48be99" dependencies = [ "once_cell", "regex", diff --git a/README.md b/README.md index 01ee79d..cd7a2cc 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ Roxy will read each file from `INPUT` for valid files. It will look for `toml` f Currently, Roxy only has two configuration keys ```toml +# config.toml +[syntect] theme = "base16-ocean.dark" # the name of the theme for syntax highlighting themes = ["./themes/base16-ocean.dark.tmTheme"] ``` diff --git a/src/config.rs b/src/config.rs index 065fbe6..6825ebc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,23 +2,13 @@ use std::str::FromStr; use serde::Deserialize; -#[derive(Debug, Default, Deserialize)] -pub(crate) struct Config { - pub theme: Option, - pub themes: Vec, +pub(crate) trait Merge { + fn merge(self, other: Self) -> Self; } -impl Config { - pub fn merge(self, other: Config) -> Self { - Self { - theme: self.theme.or(other.theme), - themes: if self.themes.is_empty() { - other.themes - } else { - self.themes - }, - } - } +#[derive(Debug, Default, Deserialize)] +pub(crate) struct Config { + pub syntect: Syntect, } impl FromStr for Config { @@ -29,3 +19,26 @@ impl FromStr for Config { } } +impl Merge for Config { + fn merge(self, other: Self) -> Self { + Self { + syntect: self.syntect.merge(other.syntect), + } + } +} + +#[derive(Debug, Default, Deserialize)] +pub(crate) struct Syntect { + pub theme: Option, + pub theme_dir: Option, +} + +impl Merge for Syntect { + fn merge(self, other: Syntect) -> Self { + Self { + theme: self.theme.or(other.theme), + theme_dir: self.theme_dir.or(other.theme_dir) + } + } +} + diff --git a/src/main.rs b/src/main.rs index cebaf0f..27603ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ pub mod config; pub mod context; mod file_path; -use config::Config; +use config::{Config, Merge}; use context::Context; use file_path::FilePath; use roxy_core::error::Error; @@ -19,6 +19,7 @@ use std::{ io::{BufReader, Read}, path::{Path, PathBuf}, }; +use syntect::{highlighting::ThemeSet, parsing::SyntaxSet}; use toml::Table; use glob::glob; @@ -87,7 +88,10 @@ fn context_from_meta_files<'a, T: AsRef>( Ok(context) } -fn copy_static>(files: &Vec<&PathBuf>, file_path: &FilePath) -> Result<(), Error> { +fn copy_static>( + files: &Vec<&PathBuf>, + file_path: &FilePath, +) -> Result<(), Error> { for file in files { let output = file_path.to_output(file)?; fs::create_dir_all(output.parent().unwrap())?; @@ -112,16 +116,20 @@ fn main() -> Result<(), Box> { let (meta, files): (Vec<&PathBuf>, Vec<&PathBuf>) = files.iter().partition(|f| f.extension().unwrap() == "toml"); - let (content, files): (Vec<&PathBuf>, Vec<&PathBuf>) = files - .iter() - .partition(|f| { - let ext = f.extension().and_then(ffi::OsStr::to_str).unwrap(); - CONTENT_EXT.contains(&ext) - }); + let (content, files): (Vec<&PathBuf>, Vec<&PathBuf>) = files.iter().partition(|f| { + let ext = f.extension().and_then(ffi::OsStr::to_str).unwrap(); + CONTENT_EXT.contains(&ext) + }); let mut context = context_from_meta_files(&meta, &file_path)?; - let theme = config.theme.unwrap_or(DEFAULT_THEME.to_string()); + let theme = config.syntect.theme.unwrap_or(DEFAULT_THEME.to_string()); + let syntax_set = SyntaxSet::load_defaults_newlines(); + let theme_set = if let Some(dir) = config.syntect.theme_dir { + ThemeSet::load_from_folder(dir)? + } else { + ThemeSet::load_defaults() + }; for file in content { let file_name = file.with_extension("html"); @@ -131,7 +139,7 @@ fn main() -> Result<(), Box> { let mut preformatter = MarkdownTeraPreformatter::new(); parser.push(&mut preformatter); - let mut syntect = SyntectParser::new(theme.as_str()); + let mut syntect = SyntectParser::new(&syntax_set, &theme_set, theme.as_str()); parser.push(&mut syntect); let mut md = MarkdownParser::new();