feat: allow subdirs in posts directory #86

Merged
Anri merged 4 commits from posts-subdir into main 2024-11-14 17:48:08 +01:00
9 changed files with 38 additions and 52 deletions

1
Cargo.lock generated
View file

@ -1095,6 +1095,7 @@ dependencies = [
"serde_yml",
"toml",
"urlencoding",
"walkdir",
]
[[package]]

View file

@ -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"

View file

@ -13,8 +13,8 @@ use crate::routes::{
mod config;
mod template;
mod utils;
mod routes;
mod utils;
#[actix_web::main]
async fn main() -> Result<()> {

View file

@ -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;

View file

@ -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<Config>) -> impl Responder {
Html(build_post(
&path.into_inner().0,

View file

@ -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<Config>) -> impl Responder {

View file

@ -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::<Vec<File>>();
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::<Vec<File>>();
let archived_appdata = if archived_apps.is_empty() {

View file

@ -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")]

View file

@ -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,23 +60,18 @@ impl Hash for Post {
}
pub fn get_posts(location: &str) -> Vec<Post> {
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>>()
},
)
.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();
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(
let file_metadata = std::fs::read_to_string(f.path()).map_or_else(
|_| FileMetadataBlog {
title: Some(file_without_ext.into()),
..FileMetadataBlog::default()
@ -97,8 +93,11 @@ pub fn get_posts(location: &str) -> Vec<Post> {
);
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();