mylloon.fr/src/routes/blog.rs

394 lines
12 KiB
Rust
Raw Normal View History

2023-04-26 14:19:02 +02:00
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
};
2023-04-26 16:51:19 +02:00
use ::rss::{
extension::atom::{AtomExtension, Link},
Category, Channel, Guid, Image, Item,
};
2024-01-25 18:23:12 +01:00
use actix_web::{get, http::header::ContentType, web, HttpResponse, Responder};
2023-04-14 11:30:58 +02:00
use cached::proc_macro::once;
2023-04-26 13:49:05 +02:00
use chrono::{DateTime, Datelike, Local, NaiveDateTime, Utc};
use chrono_tz::Europe;
2023-04-21 16:48:31 +02:00
use comrak::{parse_document, Arena};
2023-04-14 11:30:58 +02:00
use ramhorns::Content;
use crate::{
config::Config,
misc::{
date::Date,
markdown::{get_metadata, get_options, File, FileMetadataBlog, TypeFileMetadata},
utils::{get_url, make_kw, read_file, Html},
},
2024-05-28 20:26:58 +02:00
template::{InfosPage, NavBar},
};
2023-04-14 11:30:58 +02:00
2023-04-26 16:57:37 +02:00
const MIME_TYPE_RSS: &str = "application/rss+xml";
2024-01-25 18:23:12 +01:00
const BLOG_DIR: &str = "blog";
const POST_DIR: &str = "posts";
2023-04-26 16:51:19 +02:00
2023-04-14 11:30:58 +02:00
#[get("/blog")]
2023-10-24 11:50:30 +02:00
async fn index(config: web::Data<Config>) -> impl Responder {
Html(build_index(config.get_ref().to_owned()))
2023-04-14 11:30:58 +02:00
}
2023-05-02 13:34:43 +02:00
#[derive(Content, Debug)]
2023-04-19 18:55:03 +02:00
struct BlogIndexTemplate {
navbar: NavBar,
2024-01-25 18:23:12 +01:00
about: Option<File>,
2023-04-24 18:01:38 +02:00
posts: Vec<Post>,
no_posts: bool,
2023-04-19 20:27:40 +02:00
}
2023-10-21 23:08:03 +02:00
#[once(time = 60)]
2023-10-24 11:50:30 +02:00
fn build_index(config: Config) -> String {
2024-01-25 18:23:12 +01:00
let blog_dir = format!("{}/{}", config.locations.data_dir, BLOG_DIR);
2024-05-28 20:26:58 +02:00
let mut posts = get_posts(&format!("{blog_dir}/{POST_DIR}"));
2024-01-25 18:23:12 +01:00
// Get about
let about: Option<File> =
2024-05-28 20:26:58 +02:00
read_file(&format!("{blog_dir}/about.md"), &TypeFileMetadata::Generic);
// Sort from newest to oldest
posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day));
posts.reverse();
config.tmpl.render(
"blog/index.html",
BlogIndexTemplate {
navbar: NavBar {
blog: true,
..NavBar::default()
},
2024-01-25 18:23:12 +01:00
about,
2023-04-24 18:01:38 +02:00
no_posts: posts.is_empty(),
posts,
},
2024-05-28 20:26:58 +02:00
InfosPage {
title: Some("Blog".into()),
desc: Some(format!(
2023-04-28 12:49:48 +02:00
"Liste des posts d'{}",
config.fc.name.unwrap_or_default()
)),
2024-05-28 20:26:58 +02:00
kw: Some(make_kw(&["blog", "blogging"])),
},
)
}
2023-05-02 13:34:43 +02:00
#[derive(Content, Debug)]
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-24 18:41:40 +02:00
desc: Option<String>,
2023-04-26 17:17:48 +02:00
content: Option<String>,
2023-05-02 16:02:50 +02:00
tags: Vec<String>,
2023-04-19 18:55:03 +02:00
}
2023-04-14 11:30:58 +02:00
2023-04-26 17:17:48 +02:00
impl Post {
// Fetch the file content
2024-01-24 11:52:20 +01:00
fn fetch_content(&mut self, data_dir: &str) {
2024-05-28 20:26:58 +02:00
let blog_dir = format!("{data_dir}/{BLOG_DIR}/{POST_DIR}");
2023-04-26 14:19:02 +02:00
let ext = ".md";
if let Some(file) = read_file(
&format!("{blog_dir}/{}{ext}", self.url),
2024-05-28 20:26:58 +02:00
&TypeFileMetadata::Blog,
) {
2023-04-26 17:17:48 +02:00
self.content = Some(file.content);
}
}
}
impl Hash for Post {
fn hash<H: Hasher>(&self, state: &mut H) {
if let Some(content) = &self.content {
2024-05-28 20:26:58 +02:00
content.hash(state);
2023-04-26 14:19:02 +02:00
}
}
}
2024-05-28 20:26:58 +02:00
fn get_posts(location: &str) -> Vec<Post> {
2024-05-28 20:58:41 +02:00
let entries = std::fs::read_dir(location).map_or_else(
|_| vec![],
|res| {
res.flatten()
.filter(|f| f.path().extension().map_or(false, |ext| ext == "md"))
.collect::<Vec<std::fs::DirEntry>>()
},
);
2023-04-20 11:57:06 +02:00
entries
2023-04-20 11:57:06 +02:00
.iter()
2023-05-02 13:05:42 +02:00
.filter_map(|f| {
2024-05-28 20:26:58 +02:00
let fname = f.file_name();
let filename = fname.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
2024-05-28 20:58:41 +02:00
let file_metadata = std::fs::read_to_string(format!("{location}/{filename}"))
.map_or_else(
|_| FileMetadataBlog {
title: Some(file_without_ext.into()),
..FileMetadataBlog::default()
},
|text| {
let arena = Arena::new();
let options = get_options();
let root = parse_document(&arena, &text, &options);
let mut metadata =
get_metadata(root, &TypeFileMetadata::Blog).blog.unwrap();
// Always have a title
metadata.title = metadata
.title
.map_or_else(|| Some(file_without_ext.into()), Some);
metadata
},
);
if file_metadata.publish == Some(true) {
2023-05-02 13:05:42 +02:00
Some(Post {
url: file_without_ext.into(),
title: file_metadata.title.unwrap(),
date: file_metadata.date.unwrap_or({
let m = f.metadata().unwrap();
let date = std::convert::Into::<DateTime<Utc>>::into(
2024-05-28 20:58:41 +02:00
m.modified().unwrap_or_else(|_| m.created().unwrap()),
2023-05-02 13:05:42 +02:00
)
.date_naive();
Date {
day: date.day(),
month: date.month(),
year: date.year(),
}
}),
desc: file_metadata.description,
content: None,
2023-05-02 16:02:50 +02:00
tags: file_metadata
.tags
.unwrap_or_default()
.iter()
2024-05-28 20:26:58 +02:00
.map(|t| t.name.clone())
2023-05-02 16:02:50 +02:00
.collect(),
2023-05-02 13:05:42 +02:00
})
} else {
None
2023-04-19 20:54:05 +02:00
}
})
.collect::<Vec<Post>>()
}
2023-05-02 13:34:43 +02:00
#[derive(Content, Debug)]
struct BlogPostTemplate {
navbar: NavBar,
post: Option<File>,
2023-05-02 23:10:36 +02:00
toc: String,
2023-04-14 11:30:58 +02:00
}
2023-04-26 10:41:49 +02:00
#[get("/blog/p/{id}")]
2023-10-24 11:50:30 +02:00
async fn page(path: web::Path<(String,)>, config: web::Data<Config>) -> impl Responder {
2024-05-28 20:26:58 +02:00
Html(build_post(
&path.into_inner().0,
config.get_ref().to_owned(),
))
2023-04-14 11:30:58 +02:00
}
2024-05-28 20:26:58 +02:00
fn build_post(file: &str, config: Config) -> String {
2023-04-19 20:08:15 +02:00
let mut post = None;
2024-01-24 11:52:20 +01:00
let (infos, toc) = get_post(
&mut post,
file,
2024-05-28 20:26:58 +02:00
&config.fc.name.unwrap_or_default(),
&config.locations.data_dir,
2024-01-24 11:52:20 +01:00
);
2023-04-19 18:55:03 +02:00
config.tmpl.render(
"blog/post.html",
BlogPostTemplate {
navbar: NavBar {
blog: true,
..NavBar::default()
},
post,
toc,
},
infos,
)
}
2024-01-24 11:52:20 +01:00
fn get_post(
post: &mut Option<File>,
2024-05-28 20:26:58 +02:00
filename: &str,
name: &str,
data_dir: &str,
) -> (InfosPage, String) {
let blog_dir = format!("{data_dir}/{BLOG_DIR}/{POST_DIR}");
let ext = ".md";
*post = read_file(
&format!("{blog_dir}/{filename}{ext}"),
2024-05-28 20:26:58 +02:00
&TypeFileMetadata::Blog,
);
2023-10-24 13:43:08 +02:00
let default = (
2024-05-28 20:26:58 +02:00
filename,
2023-10-24 13:43:08 +02:00
&format!("Blog d'{name}"),
Vec::new(),
String::new(),
);
let (title, desc, tags, toc) = match post {
2023-05-02 14:37:32 +02:00
Some(data) => (
match &data.metadata.info.blog.as_ref().unwrap().title {
2023-05-02 14:37:32 +02:00
Some(text) => text,
2023-05-02 23:10:36 +02:00
None => default.0,
2023-05-02 14:37:32 +02:00
},
2023-10-24 13:43:08 +02:00
match &data.metadata.info.blog.as_ref().unwrap().description {
Some(desc) => desc,
None => default.1,
},
match &data.metadata.info.blog.as_ref().unwrap().tags {
2023-05-02 14:37:32 +02:00
Some(tags) => tags.clone(),
2023-10-24 13:43:08 +02:00
None => default.2,
2023-05-02 23:10:36 +02:00
},
match &data.metadata.info.blog.as_ref().unwrap().toc {
2023-05-02 23:10:36 +02:00
// TODO: Generate TOC
Some(true) => String::new(),
2023-10-24 13:43:08 +02:00
_ => default.3,
2023-05-02 14:37:32 +02:00
},
),
2023-05-02 23:10:36 +02:00
None => default,
};
2023-05-02 23:10:36 +02:00
(
2024-05-28 20:26:58 +02:00
InfosPage {
title: Some(format!("Post: {title}")),
desc: Some(desc.clone()),
kw: Some(make_kw(
2023-10-24 12:48:16 +02:00
&["blog", "blogging", "write", "writing"]
.into_iter()
.chain(tags.iter().map(|t| t.name.as_str()))
.collect::<Vec<_>>(),
2024-05-28 20:26:58 +02:00
)),
2023-05-02 23:10:36 +02:00
},
toc,
)
2023-04-14 11:30:58 +02:00
}
2023-04-26 12:50:08 +02:00
#[get("/blog/rss")]
2023-10-24 11:50:30 +02:00
async fn rss(config: web::Data<Config>) -> impl Responder {
2023-04-26 15:16:57 +02:00
HttpResponse::Ok()
2024-01-25 18:23:12 +01:00
.content_type(ContentType(MIME_TYPE_RSS.parse().unwrap()))
2023-10-24 11:50:30 +02:00
.body(build_rss(config.get_ref().to_owned()))
2023-04-26 12:50:08 +02:00
}
2023-04-26 16:22:54 +02:00
#[once(time = 10800)] // 3h
2023-10-24 11:50:30 +02:00
fn build_rss(config: Config) -> String {
2024-05-28 20:26:58 +02:00
let mut posts = get_posts(&format!(
2024-01-25 18:23:12 +01:00
"{}/{}/{}",
config.locations.data_dir, BLOG_DIR, POST_DIR
));
2023-04-26 12:50:08 +02:00
// Sort from newest to oldest
posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day));
posts.reverse();
// Only the 20 newest
let max = 20;
if posts.len() > max {
posts.drain(max..);
}
2023-10-24 11:50:30 +02:00
let link_to_site = get_url(config.fc.clone());
2024-05-28 20:26:58 +02:00
let author = if let (Some(mail), Some(name)) = (config.fc.mail, config.fc.fullname.clone()) {
2023-04-26 16:21:57 +02:00
Some(format!("{mail} ({name})"))
} else {
None
};
2023-04-28 12:49:48 +02:00
let title = format!("Blog d'{}", config.fc.name.unwrap_or_default());
2023-04-26 16:51:19 +02:00
let lang = "fr";
2023-04-26 12:50:08 +02:00
let channel = Channel {
2024-05-28 20:26:58 +02:00
title: title.clone(),
link: link_to_site.clone(),
2023-04-26 13:49:05 +02:00
description: "Un fil qui parle d'informatique notamment".into(),
2023-04-26 16:51:19 +02:00
language: Some(lang.into()),
2024-05-28 20:26:58 +02:00
managing_editor: author.clone(),
2023-04-26 16:21:57 +02:00
webmaster: author,
2023-04-26 13:49:05 +02:00
pub_date: Some(Local::now().to_rfc2822()),
2023-04-26 12:50:08 +02:00
categories: ["blog", "blogging", "write", "writing"]
.iter()
.map(|&c| Category {
name: c.into(),
..Category::default()
})
.collect(),
generator: Some("ewp with rss crate".into()),
docs: Some("https://www.rssboard.org/rss-specification".into()),
image: Some(Image {
2024-05-28 20:26:58 +02:00
url: format!("{link_to_site}/icons/favicon-32x32.png"),
title: title.clone(),
link: link_to_site.clone(),
2023-04-26 12:50:08 +02:00
..Image::default()
}),
2023-04-26 13:49:05 +02:00
items: posts
2023-04-26 17:17:48 +02:00
.iter_mut()
.map(|p| {
// Get post data
2024-01-24 11:52:20 +01:00
p.fetch_content(&config.locations.data_dir);
2023-04-26 17:17:48 +02:00
// Build item
Item {
2024-05-28 20:26:58 +02:00
title: Some(p.title.clone()),
2023-04-26 17:17:48 +02:00
link: Some(format!("{}/blog/p/{}", link_to_site, p.url)),
2024-05-28 20:26:58 +02:00
description: p.content.clone(),
2023-05-02 16:02:50 +02:00
categories: p
.tags
.iter()
.map(|c| Category {
name: c.to_owned(),
..Category::default()
})
.collect(),
2023-04-26 17:17:48 +02:00
guid: Some(Guid {
value: format!("urn:hash:{}", {
let mut hasher = DefaultHasher::new();
p.hash(&mut hasher);
hasher.finish()
}),
permalink: false,
2023-04-26 14:19:02 +02:00
}),
2023-04-26 17:17:48 +02:00
pub_date: Some(
NaiveDateTime::parse_from_str(
&format!("{}-{}-{} 13:12:00", p.date.day, p.date.month, p.date.year),
"%d-%m-%Y %H:%M:%S",
)
.unwrap()
.and_local_timezone(Europe::Paris)
.unwrap()
.to_rfc2822(),
),
..Item::default()
}
2023-04-26 13:49:05 +02:00
})
.collect(),
2023-04-26 16:51:19 +02:00
atom_ext: Some(AtomExtension {
links: vec![Link {
2024-05-28 20:26:58 +02:00
href: format!("{link_to_site}/blog/rss"),
2023-04-26 16:51:19 +02:00
rel: "self".into(),
hreflang: Some(lang.into()),
2023-04-26 16:59:00 +02:00
mime_type: Some(MIME_TYPE_RSS.into()),
2023-04-28 12:49:48 +02:00
title: Some(title),
2023-04-26 16:51:19 +02:00
length: None,
}],
}),
2023-04-26 12:50:08 +02:00
..Channel::default()
};
std::str::from_utf8(&channel.write_to(Vec::new()).unwrap())
.unwrap()
.into()
}