2024-02-20 13:03:06 +01:00
|
|
|
use std::{collections::HashMap, fs, path::Path};
|
|
|
|
|
|
|
|
pub(crate) struct Parts {
|
|
|
|
parts: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parts {
|
|
|
|
pub(crate) fn new() -> Self {
|
|
|
|
let mut parts = HashMap::new();
|
|
|
|
let parts_path = Path::new("templates/parts");
|
|
|
|
for part in fs::read_dir(parts_path).expect("No templates/parts folder") {
|
|
|
|
let part = part.unwrap();
|
2024-02-20 22:39:44 +01:00
|
|
|
let filename = part.file_name().into_string().unwrap().to_string();
|
2024-02-20 13:03:06 +01:00
|
|
|
let filename = filename.replace(".html", "");
|
|
|
|
let path = format!("{}", part.path().display());
|
|
|
|
|
|
|
|
let template = fs::read_to_string(path).unwrap();
|
|
|
|
|
|
|
|
parts.insert(filename, template);
|
|
|
|
}
|
|
|
|
Self { parts }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn perform(&self, input: String) -> String {
|
|
|
|
let mut input = input;
|
|
|
|
for (key, value) in &self.parts {
|
|
|
|
let replace = format!("{{{{{}}}}}", key);
|
|
|
|
input = input.replace(&replace, value);
|
|
|
|
}
|
|
|
|
input
|
|
|
|
}
|
|
|
|
}
|