Add stylesheet loader

main
trivernis 1 year ago
parent a0a8b20138
commit d3028e7952
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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,

@ -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<PathBuf>,
pub page_styles: HashMap<String, PathBuf>,
}
pub async fn load_stylesheets(base_dir: &PathBuf) -> Result<Stylesheets> {
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,
})
}
Loading…
Cancel
Save