From 9732ed5ae9ae7e5bc26f8302b436d67d103e740b Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 13 Feb 2023 21:35:08 +0100 Subject: atom feed integrated aswell (with a cost of two new deps) --- src/blog/atom.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/blog/mod.rs | 12 ++++++--- src/main.rs | 1 + 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 src/blog/atom.rs (limited to 'src') diff --git a/src/blog/atom.rs b/src/blog/atom.rs new file mode 100644 index 0000000..e6b931c --- /dev/null +++ b/src/blog/atom.rs @@ -0,0 +1,75 @@ +use super::{ + helper::{get_articles, ArticleMeta}, + rocket_uri_macro_r_blog_article, rocket_uri_macro_r_blog_index, ARTICLE_ROOT, +}; +use crate::{error::MyResult, uri}; +use rocket::get; +use std::{path::PathBuf, str::FromStr}; + +#[get("/blog/feed.atom")] +pub async fn r_blog_atom() -> MyResult { + let entries = get_articles(&PathBuf::from_str(ARTICLE_ROOT).unwrap()) + .await? + .iter() + .map( + |ArticleMeta { + title, + date, + canonical_name, + .. + }| { + let title = horrible_escape_function(title); + let datetime = iso8601::DateTime { + date: date.clone(), + time: iso8601::Time::default(), + }; + let href = uri!(r_blog_article(canonical_name)); + format!( + r#" + + {title} + + tag:metamuffin.org,{date},{title} + {datetime} + N/A + + metamuffin + metamuffin@disroot.org + + "# + ) + }, + ) + .collect::>(); + + let feed_url = uri!(r_blog_atom()); + let index_url = uri!(r_blog_index()); + let now = chrono::Utc::now().to_rfc3339(); + + Ok(format!( + r#" + + metamuffin's blog + where they post pointless stuff + + + urn:uuid:3cf2b704-3d94-4f1f-b194-42798ab5b47c + {now} + + metamuffin + metamuffin@disroot.org + + {} + + "#, + entries.join("\n") + )) +} + +pub fn horrible_escape_function(text: &str) -> String { + text.replace("&", "&") + .replace("<", "<") + .replace(">", ">") + .replace("'", "’") + .replace("\"", """) +} diff --git a/src/blog/mod.rs b/src/blog/mod.rs index 3ac38eb..23f0721 100644 --- a/src/blog/mod.rs +++ b/src/blog/mod.rs @@ -1,3 +1,4 @@ +pub mod atom; pub mod helper; use self::helper::{article_metadata, get_articles}; @@ -5,10 +6,14 @@ use crate::error::MyResult; use crate::layout::{DynScaffold, Scaffold}; use crate::uri; use anyhow::anyhow; +pub use atom::r_blog_atom; +use atom::rocket_uri_macro_r_blog_atom; use rocket::{get, response::Redirect}; use std::{path::PathBuf, str::FromStr}; use tokio::fs::read_to_string; +pub const ARTICLE_ROOT: &'static str = "./blog/articles"; + #[get("/blog")] pub fn r_blog() -> Redirect { Redirect::to(rocket::uri!(r_blog_index())) @@ -17,12 +22,13 @@ pub fn r_blog() -> Redirect { #[get("/blog/index")] pub async fn r_blog_index() -> MyResult> { // TODO this is a major performance issue here. requires O(n) syscalls to complete - let articles = get_articles(&PathBuf::from_str("./blog/articles").unwrap()).await?; + let articles = get_articles(&PathBuf::from_str(ARTICLE_ROOT).unwrap()).await?; Ok(Scaffold { title: "blog index".to_string(), content: markup::new! { h2 { "The Weblog" } - i { "Articles in reverse-chronological order." } + p { i { "Articles in reverse-chronological order." } } + p { a[href=uri!(r_blog_atom())]{ "Atom feed" } } ul { @for a in &articles { li { @@ -37,7 +43,7 @@ pub async fn r_blog_index() -> MyResult> { #[get("/blog/")] pub async fn r_blog_article(name: &str) -> MyResult> { - let apath = PathBuf::from_str("./blog/articles") + let apath = PathBuf::from_str(ARTICLE_ROOT) .unwrap() .join(PathBuf::new().with_file_name(name).with_extension("md")); let a = article_metadata(apath.clone()).await?; diff --git a/src/main.rs b/src/main.rs index 1857cfe..70646c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,7 @@ async fn main() { r_blog, r_blog_index, r_blog_article, + r_blog_atom, r_wellknown_security, r_wellknown_matrix_server, r_wellknown_matrix_client, -- cgit v1.2.3-70-g09d2