use actix_web::{get, web, Responder}; use cached::proc_macro::once; use ramhorns::Content; use crate::{ config::Config, template::{InfosPage, NavBar}, utils::{ markdown::{File, TypeFileMetadata}, misc::{make_kw, read_file, Html}, }, }; #[get("/")] pub async fn page(config: web::Data) -> impl Responder { Html(build_page(config.get_ref().to_owned())) } #[derive(Content, Debug)] struct IndexTemplate { navbar: NavBar, name: String, pronouns: Option, file: Option, avatar: String, avatar_caption: String, avatar_style: StyleAvatar, } #[derive(Content, Debug, Default)] struct StyleAvatar { round: bool, square: bool, } #[once(time = 60)] fn build_page(config: Config) -> String { let mut file = read_file( &format!("{}/index.md", config.locations.data_dir), &TypeFileMetadata::Index, ); // Default values let mut name = config.fc.fullname.clone().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(); let mut avatar_style = StyleAvatar { round: true, square: false, }; if let Some(f) = &file { if let Some(m) = &f.metadata.info.index { name = m.name.clone().unwrap_or(name); avatar = m.avatar.clone().unwrap_or(avatar); m.pronouns.clone_into(&mut pronouns); avatar_caption = m.avatar_caption.clone().unwrap_or(avatar_caption); if let Some(style) = m.avatar_style.clone() { if style.trim() == "square" { avatar_style = StyleAvatar { square: true, ..StyleAvatar::default() } } } } } else { file = read_file("README.md", &TypeFileMetadata::Generic); } config.tmpl.render( "index.html", IndexTemplate { navbar: NavBar { index: true, ..NavBar::default() }, file, name, pronouns, avatar, avatar_caption, avatar_style, }, InfosPage { title: config.fc.fullname, desc: Some("Page principale".into()), kw: Some(make_kw(&["index", "étudiant", "accueil"])), }, ) }