From c18c0adf3483e74ab1fffd6915f2b98eb0f4886d Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 24 Jan 2024 13:09:20 +0100 Subject: [PATCH] add metadata for index file --- src/misc/markdown.rs | 19 +++++++++++++++++++ src/routes/index.rs | 37 ++++++++++++++++++++++++++----------- templates/index.html | 8 ++++---- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/misc/markdown.rs b/src/misc/markdown.rs index 49e077f..7b68b80 100644 --- a/src/misc/markdown.rs +++ b/src/misc/markdown.rs @@ -49,6 +49,15 @@ pub struct FileMetadataContact { pub description: Option, } +/// Metadata for index page +#[derive(Default, Deserialize, Content, Debug)] +pub struct FileMetadataIndex { + pub name: Option, + pub pronouns: Option, + pub avatar: Option, + pub avatar_caption: Option, +} + /// Metadata for portfolio cards #[derive(Default, Deserialize, Content, Debug)] pub struct FileMetadataPortfolio { @@ -63,6 +72,7 @@ pub enum TypeFileMetadata { Blog, Contact, Generic, + Index, Portfolio, } @@ -72,6 +82,7 @@ pub enum TypeFileMetadata { pub struct FileMetadata { pub blog: Option, pub contact: Option, + pub index: Option, pub portfolio: Option, } @@ -246,6 +257,10 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileM TypeFileMetadata::Generic => FileMetadata { ..FileMetadata::default() }, + TypeFileMetadata::Index => FileMetadata { + index: Some(deserialize_metadata(text)), + ..FileMetadata::default() + }, TypeFileMetadata::Portfolio => FileMetadata { portfolio: Some(deserialize_metadata(text)), ..FileMetadata::default() @@ -267,6 +282,10 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileM TypeFileMetadata::Generic => FileMetadata { ..FileMetadata::default() }, + TypeFileMetadata::Index => FileMetadata { + index: Some(FileMetadataIndex::default()), + ..FileMetadata::default() + }, TypeFileMetadata::Portfolio => FileMetadata { portfolio: Some(FileMetadataPortfolio::default()), ..FileMetadata::default() diff --git a/src/routes/index.rs b/src/routes/index.rs index 5ff067d..bf9db99 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -19,19 +19,35 @@ async fn page(config: web::Data) -> impl Responder { #[derive(Content, Debug)] struct IndexTemplate { navbar: NavBar, - fullname: String, + name: String, + pronouns: Option, content: Option, + avatar: String, + avatar_caption: String, } #[once(time = 60)] fn build_page(config: Config) -> String { - let mut content = read_file( + let mut file = read_file( &format!("{}/index.md", config.locations.data_dir), - TypeFileMetadata::Generic, + TypeFileMetadata::Index, ); - if content.is_none() { - content = read_file("README.md", TypeFileMetadata::Generic); + // Default values + let mut name = config.fc.fullname.to_owned().unwrap_or_default(); + let mut pronouns = None; + let mut avatar = "/icons/apple-touch-icon.png".to_owned(); + let mut avatar_caption = "EWP avatar".to_owned(); + + if let Some(f) = &file { + if let Some(m) = &f.metadata.info.index { + name = m.name.to_owned().unwrap_or(name); + avatar = m.avatar.to_owned().unwrap_or(avatar); + pronouns = m.pronouns.to_owned(); + avatar_caption = m.avatar_caption.to_owned().unwrap_or(avatar_caption); + } + } else { + file = read_file("README.md", TypeFileMetadata::Generic); } config.tmpl.render( @@ -41,12 +57,11 @@ fn build_page(config: Config) -> String { index: true, ..NavBar::default() }, - fullname: config - .fc - .fullname - .to_owned() - .unwrap_or("Fullname".to_owned()), - content, + content: file, + name, + pronouns, + avatar, + avatar_caption, }, Infos { page_title: config.fc.fullname, diff --git a/templates/index.html b/templates/index.html index 3eb1916..6c06b7b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -10,13 +10,13 @@ {{#data}}
- {{fullname}} - (il/lui, he/him) + {{name}} + {{#pronouns}}{{pronouns}}{{/pronouns}} Avatar