add metadata for index file
All checks were successful
ci/woodpecker/push/publish Pipeline was successful

This commit is contained in:
Mylloon 2024-01-24 13:09:20 +01:00
parent 17850ae636
commit c18c0adf34
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 49 additions and 15 deletions

View file

@ -49,6 +49,15 @@ pub struct FileMetadataContact {
pub description: Option<String>, pub description: Option<String>,
} }
/// Metadata for index page
#[derive(Default, Deserialize, Content, Debug)]
pub struct FileMetadataIndex {
pub name: Option<String>,
pub pronouns: Option<String>,
pub avatar: Option<String>,
pub avatar_caption: Option<String>,
}
/// Metadata for portfolio cards /// Metadata for portfolio cards
#[derive(Default, Deserialize, Content, Debug)] #[derive(Default, Deserialize, Content, Debug)]
pub struct FileMetadataPortfolio { pub struct FileMetadataPortfolio {
@ -63,6 +72,7 @@ pub enum TypeFileMetadata {
Blog, Blog,
Contact, Contact,
Generic, Generic,
Index,
Portfolio, Portfolio,
} }
@ -72,6 +82,7 @@ pub enum TypeFileMetadata {
pub struct FileMetadata { pub struct FileMetadata {
pub blog: Option<FileMetadataBlog>, pub blog: Option<FileMetadataBlog>,
pub contact: Option<FileMetadataContact>, pub contact: Option<FileMetadataContact>,
pub index: Option<FileMetadataIndex>,
pub portfolio: Option<FileMetadataPortfolio>, pub portfolio: Option<FileMetadataPortfolio>,
} }
@ -246,6 +257,10 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileM
TypeFileMetadata::Generic => FileMetadata { TypeFileMetadata::Generic => FileMetadata {
..FileMetadata::default() ..FileMetadata::default()
}, },
TypeFileMetadata::Index => FileMetadata {
index: Some(deserialize_metadata(text)),
..FileMetadata::default()
},
TypeFileMetadata::Portfolio => FileMetadata { TypeFileMetadata::Portfolio => FileMetadata {
portfolio: Some(deserialize_metadata(text)), portfolio: Some(deserialize_metadata(text)),
..FileMetadata::default() ..FileMetadata::default()
@ -267,6 +282,10 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileM
TypeFileMetadata::Generic => FileMetadata { TypeFileMetadata::Generic => FileMetadata {
..FileMetadata::default() ..FileMetadata::default()
}, },
TypeFileMetadata::Index => FileMetadata {
index: Some(FileMetadataIndex::default()),
..FileMetadata::default()
},
TypeFileMetadata::Portfolio => FileMetadata { TypeFileMetadata::Portfolio => FileMetadata {
portfolio: Some(FileMetadataPortfolio::default()), portfolio: Some(FileMetadataPortfolio::default()),
..FileMetadata::default() ..FileMetadata::default()

View file

@ -19,19 +19,35 @@ async fn page(config: web::Data<Config>) -> impl Responder {
#[derive(Content, Debug)] #[derive(Content, Debug)]
struct IndexTemplate { struct IndexTemplate {
navbar: NavBar, navbar: NavBar,
fullname: String, name: String,
pronouns: Option<String>,
content: Option<File>, content: Option<File>,
avatar: String,
avatar_caption: String,
} }
#[once(time = 60)] #[once(time = 60)]
fn build_page(config: Config) -> String { fn build_page(config: Config) -> String {
let mut content = read_file( let mut file = read_file(
&format!("{}/index.md", config.locations.data_dir), &format!("{}/index.md", config.locations.data_dir),
TypeFileMetadata::Generic, TypeFileMetadata::Index,
); );
if content.is_none() { // Default values
content = read_file("README.md", TypeFileMetadata::Generic); 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( config.tmpl.render(
@ -41,12 +57,11 @@ fn build_page(config: Config) -> String {
index: true, index: true,
..NavBar::default() ..NavBar::default()
}, },
fullname: config content: file,
.fc name,
.fullname pronouns,
.to_owned() avatar,
.unwrap_or("Fullname".to_owned()), avatar_caption,
content,
}, },
Infos { Infos {
page_title: config.fc.fullname, page_title: config.fc.fullname,

View file

@ -10,13 +10,13 @@
{{#data}} {{#data}}
<div> <div>
<span id="name">{{fullname}}</span> <span id="name">{{name}}</span>
<span id="pronouns">(il/lui, he/him)</span> {{#pronouns}}<span id="pronouns">{{pronouns}}</span>{{/pronouns}}
<img <img
id="avatar" id="avatar"
src="/icons/apple-touch-icon.png" src="{{avatar}} "
alt="Avatar" alt="Avatar"
title="Mon avatar, dessiné un jour super rapidement sur Gimp." title="{{avatar_caption}} "
loading="lazy" loading="lazy"
/> />
</div> </div>