diff options
| author | Fuwn <[email protected]> | 2024-11-03 23:14:38 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-11-03 23:14:38 -0800 |
| commit | f8fabd790748925e8d27ad3408a147b7edd531f4 (patch) | |
| tree | 81c4cec2fbf3c6bc58bde6695da845f43b976b46 /src/modules | |
| parent | feat(blog): priority field (diff) | |
| download | locus-f8fabd790748925e8d27ad3408a147b7edd531f4.tar.xz locus-f8fabd790748925e8d27ad3408a147b7edd531f4.zip | |
feat(index): add recent posts section
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/blog.rs | 3 | ||||
| -rw-r--r-- | src/modules/blog/module.rs | 23 | ||||
| -rw-r--r-- | src/modules/blog/post.rs | 17 | ||||
| -rw-r--r-- | src/modules/index.rs | 55 | ||||
| -rw-r--r-- | src/modules/static.rs | 2 |
5 files changed, 94 insertions, 6 deletions
diff --git a/src/modules/blog.rs b/src/modules/blog.rs index f2083fe..573daa5 100644 --- a/src/modules/blog.rs +++ b/src/modules/blog.rs @@ -1,4 +1,5 @@ mod config; mod module; +mod post; -pub use module::module; +pub use module::{module, POSTS}; diff --git a/src/modules/blog/module.rs b/src/modules/blog/module.rs index bfab002..71b3a46 100644 --- a/src/modules/blog/module.rs +++ b/src/modules/blog/module.rs @@ -1,4 +1,5 @@ use { + super::post::Post, crate::{ modules::blog::config::Blog, response::success, @@ -10,9 +11,13 @@ use { collections::HashMap, fs::{self, read_dir}, io::Read, + sync::{LazyLock, Mutex}, }, }; +pub static POSTS: LazyLock<Mutex<Vec<Post>>> = + LazyLock::new(|| Mutex::new(Vec::new())); + #[allow(clippy::too_many_lines)] pub fn module(router: &mut windmark::router::Router) { let paths = read_dir("content/blogs").unwrap(); @@ -239,6 +244,7 @@ pub fn module(router: &mut windmark::router::Router) { .unwrap_or_else(|()| (String::new(), String::new())); let fixed_blog_name = fixed_blog_name_clone_2.clone(); let mut real_title = "Unknown"; + let mut created = ""; xml.add_item(&{ let mut builder = XmlItem::builder(); @@ -270,6 +276,8 @@ pub fn module(router: &mut windmark::router::Router) { if let Some(date) = post.created() { builder.add_field("pubDate", date); + + created = date; } } } @@ -280,13 +288,20 @@ pub fn module(router: &mut windmark::router::Router) { builder }); + let link = format!("/blog/{}/{}", fixed_blog_name, title.to_lowercase()); + let title = format!("{name}, {real_title}"); + + (*POSTS.lock().unwrap()).push(Post::new( + title.clone(), + link.clone(), + created.to_string(), + )); track_mount( router, - &format!("/blog/{}/{}", fixed_blog_name, title.to_lowercase()), + &link, &format!( - "{}, {} ― {}", - name, - real_title, + "{} ― {}", + title, if header.1.is_empty() { "An entry to one of Fuwn's blogs".to_string() } else { diff --git a/src/modules/blog/post.rs b/src/modules/blog/post.rs new file mode 100644 index 0000000..fb697ae --- /dev/null +++ b/src/modules/blog/post.rs @@ -0,0 +1,17 @@ +pub struct Post { + title: String, + link: String, + created_at: String, +} + +impl Post { + pub const fn new(title: String, link: String, created_at: String) -> Self { + Self { title, link, created_at } + } + + pub const fn title(&self) -> &String { &self.title } + + pub const fn link(&self) -> &String { &self.link } + + pub const fn created_at(&self) -> &String { &self.created_at } +} diff --git a/src/modules/index.rs b/src/modules/index.rs new file mode 100644 index 0000000..46ffc74 --- /dev/null +++ b/src/modules/index.rs @@ -0,0 +1,55 @@ +use super::blog::POSTS; + +pub fn module(router: &mut windmark::router::Router) { + crate::route::track_mount( + router, + "/", + "This Gemini capsule's homepage", + move |context| { + crate::response::success( + &format!( + r#"# Fuwn[.me] + +I enjoy writing for archaic systems in dated languages and with artificially imposed constraints, all while aiming for peak performance and minimal design. + +=> https://github.com/Fuwn Projects (GitHub) + +## Recent Posts + +{} + +## More + +I write a lot of software and tooling for the Gemini protocol, the backbone of this site. You can learn more over at GemRest. + +=> https://github.com/gemrest GemRest (GitHub) + +Don't know where to start? Check out the directory or test your luck! + +=> /directory Directory +=> /random I'm Feeling Lucky"#, + { + (*POSTS).lock().map_or_else( + |_| "...".to_string(), + |global_posts| { + let mut posts = global_posts; + + posts.sort_by(|a, b| b.created_at().cmp(a.created_at())); + + posts + .iter() + .map(|post| format!("=> {} {}", post.title(), post.link())) + .collect::<Vec<String>>() + .into_iter() + .take(3) + .collect::<Vec<String>>() + .join("\n") + }, + ) + } + ), + &context, + ) + }, + ); +} diff --git a/src/modules/static.rs b/src/modules/static.rs index fd03d7b..85e4840 100644 --- a/src/modules/static.rs +++ b/src/modules/static.rs @@ -43,7 +43,7 @@ pub fn module(router: &mut windmark::router::Router) { batch_mount!( "pages", router, - ("/", "This Gemini capsule's homepage", "index"), + // ("/", "This Gemini capsule's homepage", "index"), // ("/licensing", "The licensing terms of this Gemini capsule", // "licensing"), ("/readability", "The readability disclosure", "readability"), |