60 lines
1.9 KiB
Rust
60 lines
1.9 KiB
Rust
use crate::{MarkdownModule, markdowner};
|
|
use rand::seq::IteratorRandom;
|
|
use std::{fs::ReadDir, path::PathBuf};
|
|
|
|
fn random_image_module(module: &MarkdownModule, directory: &str) -> MarkdownModule {
|
|
let image = random_image(directory);
|
|
let template_content = module.content.clone();
|
|
let template = text_template::Template::from(template_content.as_str());
|
|
let mut values = std::collections::HashMap::new();
|
|
|
|
values.insert(
|
|
"file_path",
|
|
image
|
|
.strip_prefix("./serve/")
|
|
.unwrap()
|
|
.to_str()
|
|
.unwrap_or_default(),
|
|
);
|
|
values.insert(
|
|
"file_name",
|
|
image
|
|
.file_stem()
|
|
.unwrap_or_default()
|
|
.to_str()
|
|
.unwrap_or_default(),
|
|
);
|
|
let module = MarkdownModule {
|
|
path: module.path.clone(),
|
|
content: template.fill_in(&values).to_string(),
|
|
metadata: module.metadata.clone(),
|
|
};
|
|
module.clone()
|
|
}
|
|
|
|
pub fn sidebar_content(target_path: &PathBuf) -> Vec<MarkdownModule> {
|
|
let mut sidebar_modules = markdowner::get_markdown_modules(target_path);
|
|
let sidebar_modules: Vec<MarkdownModule> = sidebar_modules
|
|
.iter()
|
|
.map(|f| match f.metadata.title.as_str() {
|
|
"rats" => random_image_module(f, "rats"),
|
|
"buttons" => random_image_module(f, "buttons"),
|
|
_ => MarkdownModule {
|
|
path: f.path.clone(),
|
|
content: f.content.clone(),
|
|
metadata: f.metadata.clone(),
|
|
},
|
|
})
|
|
.collect();
|
|
sidebar_modules
|
|
}
|
|
|
|
fn random_image(directory: &str) -> PathBuf {
|
|
let rat_image = std::fs::read_dir(PathBuf::from("./serve/assets/img/random/").join(directory))
|
|
.expect("where the fuck are your rat pictures?")
|
|
.map(|f| f.expect("umm what is this?").path())
|
|
.choose(&mut rand::rng())
|
|
.expect("where is my rat");
|
|
println!("{:#?}", rat_image);
|
|
rat_image
|
|
}
|