From 411ea64fcb68935c9460635ee9d34448aa3ec31b Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 12 Nov 2024 01:14:48 +0100 Subject: [PATCH 1/4] walkdir --- Cargo.lock | 1 + Cargo.toml | 1 + src/utils/routes/blog.rs | 17 +++++++---------- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7381c93..b8d9515 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1095,6 +1095,7 @@ dependencies = [ "serde_yml", "toml", "urlencoding", + "walkdir", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 5ed8802..7246a9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ mime_guess = "2.0" urlencoding = "2.1" regex = "1.10" cyborgtime = "2.1.1" +walkdir = "2.5" [lints.clippy] pedantic = "warn" diff --git a/src/utils/routes/blog.rs b/src/utils/routes/blog.rs index 78ac80f..b960f18 100644 --- a/src/utils/routes/blog.rs +++ b/src/utils/routes/blog.rs @@ -12,6 +12,7 @@ use chrono::{DateTime, Datelike, Local, NaiveDateTime, Utc}; use chrono_tz::Europe; use comrak::{parse_document, Arena}; use ramhorns::Content; +use walkdir::WalkDir; use crate::{ config::Config, @@ -59,16 +60,12 @@ impl Hash for Post { } pub fn get_posts(location: &str) -> Vec { - std::fs::read_dir(location) - .map_or_else( - |_| vec![], - |res| { - res.flatten() - .filter(|f| f.path().extension().map_or(false, |ext| ext == "md")) - .collect::>() - }, - ) - .iter() + WalkDir::new(location) + .into_iter() + .filter_map(Result::ok) + .filter(|entry| { + entry.file_type().is_file() && entry.path().extension().is_some_and(|s| s == "md") + }) .filter_map(|f| { let fname = f.file_name(); let filename = fname.to_string_lossy(); -- 2.45.2 From e8f69a34b1971249254f5b2ba18371c5e646b63a Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 14 Nov 2024 03:28:50 +0100 Subject: [PATCH 2/4] correct url --- src/utils/routes/blog.rs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/utils/routes/blog.rs b/src/utils/routes/blog.rs index b960f18..8ea63b8 100644 --- a/src/utils/routes/blog.rs +++ b/src/utils/routes/blog.rs @@ -71,31 +71,33 @@ pub fn get_posts(location: &str) -> Vec { let filename = fname.to_string_lossy(); let file_without_ext = filename.split_at(filename.len() - 3).0; - 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 file_metadata = std::fs::read_to_string(f.path()).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(root, MType::Blog).blog.unwrap(); + let options = get_options(); + let root = parse_document(&arena, &text, &options); + let mut metadata = get(root, MType::Blog).blog.unwrap(); - // Always have a title - metadata.title = metadata - .title - .map_or_else(|| Some(file_without_ext.into()), Some); + // Always have a title + metadata.title = metadata + .title + .map_or_else(|| Some(file_without_ext.into()), Some); - metadata - }, - ); + metadata + }, + ); if file_metadata.publish == Some(true) && file_metadata.draft != Some(true) { + let url = + f.path().to_string_lossy().strip_prefix(location).unwrap()[1..].to_owned(); + Some(Post { - url: file_without_ext.into(), + url: url[..url.len() - 3].to_owned(), title: file_metadata.title.unwrap(), date: file_metadata.date.unwrap_or({ let m = f.metadata().unwrap(); -- 2.45.2 From a3ad87dc05c68dc20de54b6fbb697ce34506ff3b Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 14 Nov 2024 03:39:46 +0100 Subject: [PATCH 3/4] accept all paths? --- src/routes/blog.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 5e8d3e9..6e27f65 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -67,7 +67,7 @@ struct BlogPostTemplate { toc: String, } -#[get("/blog/p/{id}")] +#[get("/blog/p/{id:.*}")] pub async fn page(path: web::Path<(String,)>, config: web::Data) -> impl Responder { Html(build_post( &path.into_inner().0, -- 2.45.2 From 6c16315a07bd556da59c181cf7dbfa66ded90b79 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 14 Nov 2024 17:34:15 +0100 Subject: [PATCH 4/4] cargo fmt --- src/main.rs | 2 +- src/routes/agreements.rs | 2 +- src/routes/not_found.rs | 2 +- src/routes/portfolio.rs | 21 +++------------------ src/routes/web3.rs | 2 +- 5 files changed, 7 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 56d2d5b..cf9dab7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,8 +13,8 @@ use crate::routes::{ mod config; mod template; -mod utils; mod routes; +mod utils; #[actix_web::main] async fn main() -> Result<()> { diff --git a/src/routes/agreements.rs b/src/routes/agreements.rs index 7b92b39..3f584a2 100644 --- a/src/routes/agreements.rs +++ b/src/routes/agreements.rs @@ -1,4 +1,4 @@ -use crate::{config::Config, utils::misc::get_url, template::InfosPage}; +use crate::{config::Config, template::InfosPage, utils::misc::get_url}; use actix_web::{get, http::header::ContentType, routes, web, HttpResponse, Responder}; use cached::proc_macro::once; use ramhorns::Content; diff --git a/src/routes/not_found.rs b/src/routes/not_found.rs index 178ef6b..276bf78 100644 --- a/src/routes/not_found.rs +++ b/src/routes/not_found.rs @@ -4,8 +4,8 @@ use ramhorns::Content; use crate::{ config::Config, - utils::misc::{get_url, Html}, template::{InfosPage, NavBar}, + utils::misc::{get_url, Html}, }; pub async fn page(config: web::Data) -> impl Responder { diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index 6e67c05..b4091f3 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -36,21 +36,12 @@ fn build_page(config: Config) -> String { let ext = ".md"; // Get about - let about = read_file( - format!("{projects_dir}/about.md"), - MType::Generic, - ); + let about = read_file(format!("{projects_dir}/about.md"), MType::Generic); // Get apps let apps = glob(&format!("{apps_dir}/*{ext}")) .unwrap() - .map(|e| { - read_file( - e.unwrap().to_string_lossy().to_string(), - MType::Portfolio, - ) - .unwrap() - }) + .map(|e| read_file(e.unwrap().to_string_lossy().to_string(), MType::Portfolio).unwrap()) .collect::>(); let appdata = if apps.is_empty() { @@ -62,13 +53,7 @@ fn build_page(config: Config) -> String { // Get archived apps let archived_apps = glob(&format!("{apps_dir}/archive/*{ext}")) .unwrap() - .map(|e| { - read_file( - e.unwrap().to_string_lossy().to_string(), - MType::Portfolio, - ) - .unwrap() - }) + .map(|e| read_file(e.unwrap().to_string_lossy().to_string(), MType::Portfolio).unwrap()) .collect::>(); let archived_appdata = if archived_apps.is_empty() { diff --git a/src/routes/web3.rs b/src/routes/web3.rs index 86a98eb..1c00d6e 100644 --- a/src/routes/web3.rs +++ b/src/routes/web3.rs @@ -3,8 +3,8 @@ use cached::proc_macro::once; use crate::{ config::Config, - utils::misc::{make_kw, Html}, template::InfosPage, + utils::misc::{make_kw, Html}, }; #[get("/web3")] -- 2.45.2