mylloon.fr/src/routes/blog.rs

141 lines
4 KiB
Rust
Raw Normal View History

2023-04-14 11:30:58 +02:00
use actix_web::{get, web, HttpResponse, Responder};
use cached::proc_macro::once;
2023-04-20 15:08:09 +02:00
use chrono::{DateTime, Datelike, Utc};
2023-04-14 11:30:58 +02:00
use ramhorns::Content;
use crate::{
config::Config,
2023-04-20 14:41:36 +02:00
template::{get_md_asm, get_md_metadata, read_md_file, Date, File, FileMetadata, Infos},
};
2023-04-14 11:30:58 +02:00
#[get("/blog")]
pub async fn index(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(get_index(config.get_ref().clone()))
}
#[derive(Content)]
2023-04-19 18:55:03 +02:00
struct BlogIndexTemplate {
2023-04-19 20:27:40 +02:00
posts: Option<Vec<Post>>,
}
2023-04-20 14:41:36 +02:00
#[derive(Content)]
2023-04-19 20:27:40 +02:00
struct Post {
title: String,
2023-04-20 14:41:36 +02:00
date: Date,
2023-04-19 20:27:40 +02:00
url: String,
2023-04-19 18:55:03 +02:00
}
2023-04-14 11:30:58 +02:00
#[once(time = 60)]
pub fn get_index(config: Config) -> String {
2023-04-19 21:16:39 +02:00
let location = "data/blog";
2023-04-20 11:57:06 +02:00
2023-04-20 15:08:09 +02:00
let entries = std::fs::read_dir(location)
2023-04-19 20:54:05 +02:00
.unwrap()
2023-04-20 11:57:06 +02:00
.flatten()
.filter(|f| f.path().extension().unwrap() == "md")
.collect::<Vec<std::fs::DirEntry>>();
2023-04-20 15:08:09 +02:00
let mut posts = entries
2023-04-20 11:57:06 +02:00
.iter()
2023-04-19 20:54:05 +02:00
.map(|f| {
2023-04-20 11:57:06 +02:00
let _filename = f.file_name();
let filename = _filename.to_string_lossy();
2023-04-19 20:54:05 +02:00
let file_without_ext = filename.split_at(filename.len() - 3).0;
2023-04-19 21:16:39 +02:00
let file_metadata = match std::fs::read_to_string(format!("{location}/{filename}")) {
Ok(text) => {
let md_tree = get_md_asm(&text);
let md_nodes = md_tree.children().unwrap();
2023-04-20 11:57:06 +02:00
let mut metadata = get_md_metadata(md_nodes);
metadata.title = match metadata.title {
Some(t) => Some(t),
None => Some(file_without_ext.to_string()),
};
metadata
2023-04-19 21:16:39 +02:00
}
2023-04-20 11:57:06 +02:00
Err(_) => FileMetadata {
title: Some(file_without_ext.to_string()),
link: None,
date: None,
},
2023-04-19 21:16:39 +02:00
};
2023-04-19 20:54:05 +02:00
Post {
url: file_without_ext.to_string(),
2023-04-20 11:57:06 +02:00
title: file_metadata.title.unwrap(),
2023-04-20 15:08:09 +02:00
date: file_metadata.date.unwrap_or({
let m = f.metadata().unwrap();
let date = std::convert::Into::<DateTime<Utc>>::into(
m.modified().unwrap_or(m.created().unwrap()),
)
.date_naive();
Date {
day: date.day(),
month: date.month(),
year: date.year(),
}
}),
2023-04-19 20:54:05 +02:00
}
})
.collect::<Vec<Post>>();
2023-04-20 15:08:09 +02:00
// Sort from newest to oldest
2023-04-20 15:19:41 +02:00
posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day));
2023-04-20 15:08:09 +02:00
posts.reverse();
2023-04-20 14:41:36 +02:00
config.tmpl.render(
"blog/index.html",
2023-04-19 20:54:05 +02:00
BlogIndexTemplate {
2023-04-20 15:08:09 +02:00
posts: if posts.is_empty() { None } else { Some(posts) },
2023-04-19 20:54:05 +02:00
},
Infos {
2023-04-19 20:32:04 +02:00
page_title: Some("Blog".to_string()),
page_desc: Some("Liste des posts d'Anri".to_string()),
page_kw: Some(["blog", "blogging"].join(", ")),
},
)
}
#[derive(Content)]
struct BlogPostTemplate {
post: Option<File>,
2023-04-14 11:30:58 +02:00
}
#[get("/blog/{id}")]
2023-04-19 18:55:03 +02:00
pub async fn page(path: web::Path<(String,)>, config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(get_post(path.into_inner().0, config.get_ref().clone()))
2023-04-14 11:30:58 +02:00
}
pub fn get_post(file: String, config: Config) -> String {
2023-04-19 20:08:15 +02:00
let mut post = None;
let infos = _get_post(&mut post, file);
2023-04-19 18:55:03 +02:00
config
.tmpl
.render("blog/post.html", BlogPostTemplate { post }, infos)
}
fn _get_post(post: &mut Option<File>, filename: String) -> Infos {
let blog_dir = "data/blog";
let ext = ".md";
*post = read_md_file(&format!("{blog_dir}/{filename}{ext}"));
let title = match post {
Some(data) => match &data.metadata.info.title {
Some(text) => text,
None => &filename,
},
None => &filename,
};
Infos {
page_title: Some(format!("Post: {}", title)),
page_desc: Some("Blog d'Anri".to_string()),
page_kw: Some(["blog", "blogging", "write", "writing"].join(", ")),
}
2023-04-14 11:30:58 +02:00
}