diff --git a/src/rendering/mod.rs b/src/rendering/mod.rs index d73d29c..a7644a0 100644 --- a/src/rendering/mod.rs +++ b/src/rendering/mod.rs @@ -10,6 +10,8 @@ use crate::{ data::{load_page, FolderData}, }; +mod style; + // renders content using the given template folder pub struct ContentRenderer { template_glob: String, diff --git a/src/rendering/style.rs b/src/rendering/style.rs new file mode 100644 index 0000000..bd9c7fe --- /dev/null +++ b/src/rendering/style.rs @@ -0,0 +1,43 @@ +use std::{collections::HashMap, path::PathBuf}; + +use async_walkdir::WalkDir; +use futures::StreamExt; +use miette::{IntoDiagnostic, Result}; + +pub struct Stylesheets { + pub default_style: Option, + pub page_styles: HashMap, +} + +pub async fn load_stylesheets(base_dir: &PathBuf) -> Result { + let mut entries = WalkDir::new(base_dir); + let mut page_styles = HashMap::new(); + + while let Some(res) = entries.next().await { + match res { + Ok(entry) => { + let entry_path = entry.path(); + if entry_path.is_file() { + if let Some(file_name) = entry_path.file_stem() { + let file_name = file_name.to_string_lossy().into_owned(); + page_styles.insert(file_name, entry_path.to_owned()); + } + } + } + Err(e) => return Err(e).into_diagnostic(), + } + } + + let mut default_style = None; + for name in ["style", "default", "stylesheet", "index"] { + if let Some(style) = page_styles.remove(name) { + default_style = Some(style); + break; + } + } + + Ok(Stylesheets { + default_style, + page_styles, + }) +}