2023-04-26 12:50:08 +02:00
|
|
|
use ::rss::{Category, Channel, Image};
|
|
|
|
use actix_web::{dev::ConnectionInfo, get, web, HttpRequest, HttpResponse, Responder};
|
2023-04-14 11:30:58 +02:00
|
|
|
use cached::proc_macro::once;
|
2023-04-20 15:08:09 +02:00
|
|
|
use chrono::{DateTime, Datelike, Utc};
|
2023-04-21 16:48:31 +02:00
|
|
|
use comrak::{parse_document, Arena};
|
2023-04-14 11:30:58 +02:00
|
|
|
use ramhorns::Content;
|
|
|
|
|
2023-04-19 20:17:03 +02:00
|
|
|
use crate::{
|
|
|
|
config::Config,
|
2023-04-21 16:27:06 +02:00
|
|
|
misc::{
|
|
|
|
date::Date,
|
2023-04-21 16:48:31 +02:00
|
|
|
markdown::{get_metadata, get_options, read_file, File, FileMetadata},
|
2023-04-21 16:27:06 +02:00
|
|
|
},
|
|
|
|
template::Infos,
|
2023-04-19 20:17:03 +02:00
|
|
|
};
|
2023-04-14 11:30:58 +02:00
|
|
|
|
|
|
|
#[get("/blog")]
|
|
|
|
pub async fn index(config: web::Data<Config>) -> impl Responder {
|
2023-04-24 12:18:21 +02:00
|
|
|
HttpResponse::Ok().body(build_index(config.get_ref().to_owned()))
|
2023-04-14 11:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Content)]
|
2023-04-19 18:55:03 +02:00
|
|
|
struct BlogIndexTemplate {
|
2023-04-24 18:01:38 +02:00
|
|
|
posts: Vec<Post>,
|
|
|
|
no_posts: bool,
|
2023-04-19 20:27:40 +02:00
|
|
|
}
|
|
|
|
|
2023-04-24 19:15:28 +02:00
|
|
|
#[once(time = 120)]
|
2023-04-20 15:43:25 +02:00
|
|
|
pub fn build_index(config: Config) -> String {
|
|
|
|
let mut posts = get_posts("data/blog");
|
|
|
|
|
|
|
|
// 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 {
|
2023-04-24 18:01:38 +02:00
|
|
|
no_posts: posts.is_empty(),
|
|
|
|
posts,
|
2023-04-20 15:43:25 +02:00
|
|
|
},
|
|
|
|
Infos {
|
2023-04-26 12:07:46 +02:00
|
|
|
page_title: Some("Blog".into()),
|
|
|
|
page_desc: Some("Liste des posts d'Anri".into()),
|
2023-04-20 15:43:25 +02:00
|
|
|
page_kw: Some(["blog", "blogging"].join(", ")),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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-24 18:41:40 +02:00
|
|
|
desc: Option<String>,
|
2023-04-19 18:55:03 +02:00
|
|
|
}
|
2023-04-14 11:30:58 +02:00
|
|
|
|
2023-04-20 15:43:25 +02:00
|
|
|
fn get_posts(location: &str) -> Vec<Post> {
|
2023-04-24 17:34:58 +02:00
|
|
|
let entries = match std::fs::read_dir(location) {
|
|
|
|
Ok(res) => res
|
|
|
|
.flatten()
|
|
|
|
.filter(|f| f.path().extension().unwrap() == "md")
|
|
|
|
.collect::<Vec<std::fs::DirEntry>>(),
|
|
|
|
Err(_) => vec![],
|
|
|
|
};
|
2023-04-20 11:57:06 +02:00
|
|
|
|
2023-04-20 15:43:25 +02:00
|
|
|
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}")) {
|
2023-04-21 16:48:31 +02:00
|
|
|
Ok(text) => {
|
|
|
|
let arena = Arena::new();
|
|
|
|
|
|
|
|
let options = get_options();
|
|
|
|
let root = parse_document(&arena, &text, &options);
|
|
|
|
let mut metadata = get_metadata(root);
|
2023-04-20 11:57:06 +02:00
|
|
|
|
|
|
|
metadata.title = match metadata.title {
|
2023-04-21 16:48:31 +02:00
|
|
|
Some(title) => Some(title),
|
2023-04-26 12:07:46 +02:00
|
|
|
None => Some(file_without_ext.into()),
|
2023-04-20 11:57:06 +02:00
|
|
|
};
|
|
|
|
|
2023-04-21 16:48:31 +02:00
|
|
|
metadata
|
2023-04-19 21:16:39 +02:00
|
|
|
}
|
2023-04-20 11:57:06 +02:00
|
|
|
Err(_) => FileMetadata {
|
2023-04-26 12:07:46 +02:00
|
|
|
title: Some(file_without_ext.into()),
|
2023-04-24 18:41:40 +02:00
|
|
|
..FileMetadata::default()
|
2023-04-20 11:57:06 +02:00
|
|
|
},
|
2023-04-19 21:16:39 +02:00
|
|
|
};
|
|
|
|
|
2023-04-19 20:54:05 +02:00
|
|
|
Post {
|
2023-04-26 12:07:46 +02:00
|
|
|
url: file_without_ext.into(),
|
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-24 18:41:40 +02:00
|
|
|
desc: file_metadata.description,
|
2023-04-19 20:54:05 +02:00
|
|
|
}
|
|
|
|
})
|
2023-04-20 15:43:25 +02:00
|
|
|
.collect::<Vec<Post>>()
|
2023-04-19 20:17:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Content)]
|
|
|
|
struct BlogPostTemplate {
|
|
|
|
post: Option<File>,
|
2023-04-14 11:30:58 +02:00
|
|
|
}
|
|
|
|
|
2023-04-26 10:41:49 +02:00
|
|
|
#[get("/blog/p/{id}")]
|
2023-04-19 18:55:03 +02:00
|
|
|
pub async fn page(path: web::Path<(String,)>, config: web::Data<Config>) -> impl Responder {
|
2023-04-24 12:18:21 +02:00
|
|
|
HttpResponse::Ok().body(build_post(path.into_inner().0, config.get_ref().to_owned()))
|
2023-04-14 11:30:58 +02:00
|
|
|
}
|
|
|
|
|
2023-04-26 10:41:49 +02:00
|
|
|
fn build_post(file: String, config: Config) -> String {
|
2023-04-19 20:08:15 +02:00
|
|
|
let mut post = None;
|
2023-04-20 15:43:25 +02:00
|
|
|
let infos = get_post(&mut post, file);
|
2023-04-19 18:55:03 +02:00
|
|
|
|
|
|
|
config
|
|
|
|
.tmpl
|
2023-04-19 20:17:03 +02:00
|
|
|
.render("blog/post.html", BlogPostTemplate { post }, infos)
|
|
|
|
}
|
|
|
|
|
2023-04-20 15:43:25 +02:00
|
|
|
fn get_post(post: &mut Option<File>, filename: String) -> Infos {
|
2023-04-19 20:17:03 +02:00
|
|
|
let blog_dir = "data/blog";
|
|
|
|
let ext = ".md";
|
|
|
|
|
2023-04-21 16:27:06 +02:00
|
|
|
*post = read_file(&format!("{blog_dir}/{filename}{ext}"));
|
2023-04-19 20:17:03 +02:00
|
|
|
|
|
|
|
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)),
|
2023-04-26 12:07:46 +02:00
|
|
|
page_desc: Some("Blog d'Anri".into()),
|
2023-04-19 20:17:03 +02:00
|
|
|
page_kw: Some(["blog", "blogging", "write", "writing"].join(", ")),
|
|
|
|
}
|
2023-04-14 11:30:58 +02:00
|
|
|
}
|
2023-04-26 12:50:08 +02:00
|
|
|
|
|
|
|
#[get("/blog/rss")]
|
|
|
|
pub async fn rss(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
|
|
|
|
HttpResponse::Ok().body(build_rss(
|
|
|
|
config.get_ref().to_owned(),
|
|
|
|
req.connection_info().to_owned(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
//#[once(time = 10800)] // 3h
|
|
|
|
fn build_rss(config: Config, info: ConnectionInfo) -> String {
|
|
|
|
let mut posts = get_posts("data/blog");
|
|
|
|
|
|
|
|
// 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..);
|
|
|
|
}
|
|
|
|
|
|
|
|
let link_to_site = format!("{}://{}", info.scheme(), info.host());
|
|
|
|
let channel = Channel {
|
|
|
|
title: "Blog d'Anri".into(),
|
|
|
|
link: link_to_site.to_owned(),
|
|
|
|
description: "Un fil qui parle d'infos".into(),
|
|
|
|
language: Some("fr".into()),
|
|
|
|
managing_editor: config.fc.mail.to_owned(),
|
|
|
|
webmaster: config.fc.mail,
|
|
|
|
pub_date: Some(chrono::Utc::now().to_rfc2822().replace("+0000", "GMT")),
|
|
|
|
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 {
|
|
|
|
url: format!(
|
|
|
|
"{}://{}/icons/favicon-32x32.png",
|
|
|
|
info.scheme(),
|
|
|
|
info.host()
|
|
|
|
),
|
|
|
|
title: "Favicon".into(),
|
|
|
|
link: link_to_site,
|
|
|
|
..Image::default()
|
|
|
|
}),
|
|
|
|
items: vec![],
|
|
|
|
..Channel::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
std::str::from_utf8(&channel.write_to(Vec::new()).unwrap())
|
|
|
|
.unwrap()
|
|
|
|
.into()
|
|
|
|
}
|