diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 162151a..1a87265 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -31,8 +31,7 @@ fn build_index(config: Config) -> String { let mut posts = get_posts(&format!("{blog_dir}/{POST_DIR}")); // Get about - let about: Option = - read_file(&format!("{blog_dir}/about.md"), &TypeFileMetadata::Generic); + let about: Option = read_file(format!("{blog_dir}/about.md"), TypeFileMetadata::Generic); // Sort from newest to oldest posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day)); diff --git a/src/routes/contact.rs b/src/routes/contact.rs index 5480afe..1328212 100644 --- a/src/routes/contact.rs +++ b/src/routes/contact.rs @@ -85,26 +85,44 @@ fn build_page(config: Config) -> String { // Get about let about = read_file( - &format!("{contacts_dir}/about.md"), - &TypeFileMetadata::Generic, + format!("{contacts_dir}/about.md"), + TypeFileMetadata::Generic, ); let socials_dir = "socials"; let mut socials = glob(&format!("{contacts_dir}/{socials_dir}/*{ext}")) .unwrap() - .map(|e| read_file(&e.unwrap().to_string_lossy(), &TypeFileMetadata::Contact).unwrap()) + .map(|e| { + read_file( + e.unwrap().to_string_lossy().to_string(), + TypeFileMetadata::Contact, + ) + .unwrap() + }) .collect::>(); let forges_dir = "forges"; let mut forges = glob(&format!("{contacts_dir}/{forges_dir}/*{ext}")) .unwrap() - .map(|e| read_file(&e.unwrap().to_string_lossy(), &TypeFileMetadata::Contact).unwrap()) + .map(|e| { + read_file( + e.unwrap().to_string_lossy().to_string(), + TypeFileMetadata::Contact, + ) + .unwrap() + }) .collect::>(); let others_dir = "others"; let mut others = glob(&format!("{contacts_dir}/{others_dir}/*{ext}")) .unwrap() - .map(|e| read_file(&e.unwrap().to_string_lossy(), &TypeFileMetadata::Contact).unwrap()) + .map(|e| { + read_file( + e.unwrap().to_string_lossy().to_string(), + TypeFileMetadata::Contact, + ) + .unwrap() + }) .collect::>(); // Remove paragraphs in custom statements diff --git a/src/routes/cours.rs b/src/routes/cours.rs index 1504703..7dc7508 100644 --- a/src/routes/cours.rs +++ b/src/routes/cours.rs @@ -53,10 +53,7 @@ fn get_content( return None; } - read_file( - &format!("{cours_dir}/{filename}"), - &TypeFileMetadata::Generic, - ) + read_file(format!("{cours_dir}/{filename}"), TypeFileMetadata::Generic) } fn build_page(info: &web::Query, config: Config) -> String { diff --git a/src/routes/index.rs b/src/routes/index.rs index ed5d089..fd0c312 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -36,8 +36,8 @@ struct StyleAvatar { #[once(time = 60)] fn build_page(config: Config) -> String { let mut file = read_file( - &format!("{}/index.md", config.locations.data_dir), - &TypeFileMetadata::Index, + format!("{}/index.md", config.locations.data_dir), + TypeFileMetadata::Index, ); // Default values @@ -67,7 +67,7 @@ fn build_page(config: Config) -> String { } } } else { - file = read_file("README.md", &TypeFileMetadata::Generic); + file = read_file("README.md".to_string(), TypeFileMetadata::Generic); } config.tmpl.render( diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index 41f201a..8f24542 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -5,11 +5,11 @@ use ramhorns::Content; use crate::{ config::Config, + template::{InfosPage, NavBar}, utils::{ markdown::{File, TypeFileMetadata}, misc::{make_kw, read_file, Html}, }, - template::{InfosPage, NavBar}, }; #[get("/portfolio")] @@ -36,14 +36,20 @@ fn build_page(config: Config) -> String { // Get about let about = read_file( - &format!("{projects_dir}/about.md"), - &TypeFileMetadata::Generic, + format!("{projects_dir}/about.md"), + TypeFileMetadata::Generic, ); // Get apps let apps = glob(&format!("{apps_dir}/*{ext}")) .unwrap() - .map(|e| read_file(&e.unwrap().to_string_lossy(), &TypeFileMetadata::Portfolio).unwrap()) + .map(|e| { + read_file( + e.unwrap().to_string_lossy().to_string(), + TypeFileMetadata::Portfolio, + ) + .unwrap() + }) .collect::>(); let appdata = if apps.is_empty() { @@ -55,7 +61,13 @@ fn build_page(config: Config) -> String { // Get archived apps let archived_apps = glob(&format!("{apps_dir}/archive/*{ext}")) .unwrap() - .map(|e| read_file(&e.unwrap().to_string_lossy(), &TypeFileMetadata::Portfolio).unwrap()) + .map(|e| { + read_file( + e.unwrap().to_string_lossy().to_string(), + TypeFileMetadata::Portfolio, + ) + .unwrap() + }) .collect::>(); let archived_appdata = if archived_apps.is_empty() { diff --git a/src/utils/markdown.rs b/src/utils/markdown.rs index a539f0f..5c0a7dd 100644 --- a/src/utils/markdown.rs +++ b/src/utils/markdown.rs @@ -45,7 +45,7 @@ impl<'a> Deserialize<'a> for Tag { } /// Metadata for contact entry -#[derive(Content, Debug, Default, Deserialize)] +#[derive(Content, Debug, Default, Deserialize, Clone)] pub struct FileMetadataContact { pub title: String, pub custom: Option, @@ -56,7 +56,7 @@ pub struct FileMetadataContact { } /// Metadata for index page -#[derive(Content, Debug, Default, Deserialize)] +#[derive(Content, Debug, Default, Deserialize, Clone)] pub struct FileMetadataIndex { pub name: Option, pub pronouns: Option, @@ -66,7 +66,7 @@ pub struct FileMetadataIndex { } /// Metadata for portfolio cards -#[derive(Content, Debug, Default, Deserialize)] +#[derive(Content, Debug, Default, Deserialize, Clone)] pub struct FileMetadataPortfolio { pub title: Option, pub link: Option, @@ -75,6 +75,7 @@ pub struct FileMetadataPortfolio { } /// List of available metadata types +#[derive(Hash, PartialEq, Eq, Clone, Copy)] pub enum TypeFileMetadata { Blog, Contact, @@ -85,7 +86,7 @@ pub enum TypeFileMetadata { /// Structure who holds all the metadata the file have /// Usually all fields are None except one -#[derive(Content, Debug, Default, Deserialize)] +#[derive(Content, Debug, Default, Deserialize, Clone)] pub struct FileMetadata { pub hardbreaks: bool, pub blog: Option, @@ -96,7 +97,7 @@ pub struct FileMetadata { #[allow(clippy::struct_excessive_bools)] /// Global metadata -#[derive(Content, Debug)] +#[derive(Content, Debug, Clone)] pub struct Metadata { pub info: FileMetadata, pub math: bool, @@ -115,7 +116,7 @@ impl Metadata { } /// File description -#[derive(Content, Debug)] +#[derive(Content, Debug, Clone)] pub struct File { pub metadata: Metadata, pub content: String, @@ -247,12 +248,8 @@ fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) { if mime == "text/markdown" { let mut options = get_options(); options.extension.footnotes = false; - let data = read_md( - &img_path, - &file, - &TypeFileMetadata::Generic, - Some(options), - ); + let data = + read_md(&img_path, &file, TypeFileMetadata::Generic, Some(options)); el.replace(&data.content, ContentType::Html); // Store the metadata for later merging @@ -283,7 +280,7 @@ fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) { pub fn read_md( path: &str, raw_text: &str, - metadata_type: &TypeFileMetadata, + metadata_type: TypeFileMetadata, options: Option, ) -> File { let arena = Arena::new(); @@ -333,7 +330,7 @@ fn deserialize_metadata(text: &str) -> } /// Fetch metadata from AST -pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: &TypeFileMetadata) -> FileMetadata { +pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileMetadata { root.children() .map(|node| { let generic = FileMetadata { diff --git a/src/utils/misc.rs b/src/utils/misc.rs index 96699d3..c5dc6eb 100644 --- a/src/utils/misc.rs +++ b/src/utils/misc.rs @@ -53,13 +53,14 @@ impl Responder for Html { } /// Read a file -pub fn read_file(filename: &str, expected_file: &TypeFileMetadata) -> Option { - Path::new(filename) +#[cached] +pub fn read_file(filename: String, expected_file: TypeFileMetadata) -> Option { + Path::new(&filename.clone()) .extension() .and_then(|ext| match ext.to_str().unwrap() { "pdf" => fs::read(filename).map_or(None, |bytes| Some(read_pdf(bytes))), - _ => fs::read_to_string(filename).map_or(None, |text| { - Some(read_md(filename, &text, expected_file, None)) + _ => fs::read_to_string(&filename).map_or(None, |text| { + Some(read_md(&filename, &text, expected_file, None)) }), }) } diff --git a/src/utils/routes/blog.rs b/src/utils/routes/blog.rs index 1170297..2d42359 100644 --- a/src/utils/routes/blog.rs +++ b/src/utils/routes/blog.rs @@ -44,8 +44,8 @@ impl Post { let ext = ".md"; if let Some(file) = read_file( - &format!("{blog_dir}/{}{ext}", self.url), - &TypeFileMetadata::Blog, + format!("{blog_dir}/{}{ext}", self.url), + TypeFileMetadata::Blog, ) { self.content = Some(file.content); } @@ -88,8 +88,7 @@ pub fn get_posts(location: &str) -> Vec { let options = get_options(); let root = parse_document(&arena, &text, &options); - let mut metadata = - get_metadata(root, &TypeFileMetadata::Blog).blog.unwrap(); + let mut metadata = get_metadata(root, TypeFileMetadata::Blog).blog.unwrap(); // Always have a title metadata.title = metadata @@ -143,8 +142,8 @@ pub fn get_post( let ext = ".md"; *post = read_file( - &format!("{blog_dir}/{filename}{ext}"), - &TypeFileMetadata::Blog, + format!("{blog_dir}/{filename}{ext}"), + TypeFileMetadata::Blog, ); let default = (