From 9558202a46b6c63d577549b927ec9ba31e261163 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 25 Jan 2024 18:23:12 +0100 Subject: [PATCH] blog genericity --- src/routes/blog.rs | 24 ++++++++++++++++++------ templates/blog/index.html | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/routes/blog.rs b/src/routes/blog.rs index c40a36d..aec3019 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -7,7 +7,7 @@ use ::rss::{ extension::atom::{AtomExtension, Link}, Category, Channel, Guid, Image, Item, }; -use actix_web::{get, web, HttpResponse, Responder}; +use actix_web::{get, http::header::ContentType, web, HttpResponse, Responder}; use cached::proc_macro::once; use chrono::{DateTime, Datelike, Local, NaiveDateTime, Utc}; use chrono_tz::Europe; @@ -27,6 +27,8 @@ use crate::{ }; const MIME_TYPE_RSS: &str = "application/rss+xml"; +const BLOG_DIR: &str = "blog"; +const POST_DIR: &str = "posts"; #[get("/blog")] async fn index(config: web::Data) -> impl Responder { @@ -36,13 +38,19 @@ async fn index(config: web::Data) -> impl Responder { #[derive(Content, Debug)] struct BlogIndexTemplate { navbar: NavBar, + about: Option, posts: Vec, no_posts: bool, } #[once(time = 60)] fn build_index(config: Config) -> String { - let mut posts = get_posts(format!("{}/blog", config.locations.data_dir)); + let blog_dir = format!("{}/{}", config.locations.data_dir, BLOG_DIR); + let mut posts = get_posts(format!("{}/{}", blog_dir, POST_DIR)); + + // Get about + let about: Option = + read_file(&format!("{}/about.md", blog_dir), TypeFileMetadata::Generic); // Sort from newest to oldest posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day)); @@ -55,6 +63,7 @@ fn build_index(config: Config) -> String { blog: true, ..NavBar::default() }, + about, no_posts: posts.is_empty(), posts, }, @@ -82,7 +91,7 @@ struct Post { impl Post { // Fetch the file content fn fetch_content(&mut self, data_dir: &str) { - let blog_dir = format!("{}/blog", data_dir); + let blog_dir = format!("{}/{}/{}", data_dir, BLOG_DIR, POST_DIR); let ext = ".md"; if let Some(file) = read_file( @@ -217,7 +226,7 @@ fn get_post( name: String, data_dir: String, ) -> (Infos, String) { - let blog_dir = format!("{}/blog", data_dir); + let blog_dir = format!("{}/{}/{}", data_dir, BLOG_DIR, POST_DIR); let ext = ".md"; *post = read_file( @@ -272,13 +281,16 @@ fn get_post( #[get("/blog/rss")] async fn rss(config: web::Data) -> impl Responder { HttpResponse::Ok() - .append_header(("content-type", MIME_TYPE_RSS)) + .content_type(ContentType(MIME_TYPE_RSS.parse().unwrap())) .body(build_rss(config.get_ref().to_owned())) } #[once(time = 10800)] // 3h fn build_rss(config: Config) -> String { - let mut posts = get_posts(format!("{}/blog", config.locations.data_dir)); + let mut posts = get_posts(format!( + "{}/{}/{}", + config.locations.data_dir, BLOG_DIR, POST_DIR + )); // Sort from newest to oldest posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day)); diff --git a/templates/blog/index.html b/templates/blog/index.html index ac72f9b..986594d 100644 --- a/templates/blog/index.html +++ b/templates/blog/index.html @@ -16,7 +16,7 @@ {{#data}}

Blog

-

Blog perso, je dis peut-être n'importe quoi 🫶

+ {{#about}} {{&content}} {{/about}} Lien vers le flux RSS {{#no_posts}}