From 017f04c15edaa27b029afde00f587c76fe1188c6 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 22 Dec 2024 19:17:17 +0100 Subject: [PATCH] find correct md file --- .gitignore | 4 ++-- src/config.rs | 6 +++--- src/routes/blog.rs | 1 + src/routes/contact.rs | 1 + src/routes/cours.rs | 1 + src/routes/index.rs | 20 +++++++++++--------- src/routes/portfolio.rs | 3 +++ src/template.rs | 2 +- src/utils/misc.rs | 27 ++++++++++++++++++--------- src/utils/routes/blog.rs | 2 ++ src/utils/routes/contact.rs | 9 ++++++++- 11 files changed, 51 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index e5d75c4..114edb0 100644 --- a/.gitignore +++ b/.gitignore @@ -7,13 +7,13 @@ docker-compose.yml /.vscode # Data -data/index.md +data/index*.md data/contacts/* data/cours/* data/projects/* # Blog -data/blog/*.md +data/blog/about*.md data/blog/posts/* !data/blog/posts/Makefile diff --git a/src/config.rs b/src/config.rs index d1bec25..5058103 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,7 +7,7 @@ use std::{fs::File, io::Write, path::Path}; use crate::template::Template; /// Store the configuration of config/config.toml -#[derive(Clone, Debug, Default, Deserialize)] +#[derive(Clone, Debug, Default, Deserialize, Hash, PartialEq, Eq)] pub struct FileConfiguration { /// http/https pub scheme: Option, @@ -75,14 +75,14 @@ impl FileConfiguration { } // Paths where files are stored -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Hash, PartialEq, Eq)] pub struct Locations { pub static_dir: String, pub data_dir: String, } /// Configuration used internally in the app -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Hash, PartialEq, Eq)] pub struct Config { /// Information given in the config file pub fc: FileConfiguration, diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 0f26237..3fbf6c6 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -38,6 +38,7 @@ fn build_index(config: Config) -> String { path: "about.md".to_owned(), }, MType::Generic, + None, ); // Sort from newest to oldest diff --git a/src/routes/contact.rs b/src/routes/contact.rs index 56c6dcb..ea28cdf 100644 --- a/src/routes/contact.rs +++ b/src/routes/contact.rs @@ -90,6 +90,7 @@ fn build_page(config: Config) -> String { path: "about.md".to_owned(), }, MType::Generic, + None, ); let mut socials = read(&FilePath { diff --git a/src/routes/cours.rs b/src/routes/cours.rs index fb9bdad..5a86bb9 100644 --- a/src/routes/cours.rs +++ b/src/routes/cours.rs @@ -60,6 +60,7 @@ fn get_content( path: filename.to_owned(), }, MType::Cours, + None, ) } diff --git a/src/routes/index.rs b/src/routes/index.rs index 1b5855e..b1067b5 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -1,5 +1,5 @@ -use actix_web::{get, web, Responder}; -use cached::proc_macro::once; +use actix_web::{get, web, HttpRequest, Responder}; +use cached::proc_macro::cached; use ramhorns::Content; use crate::{ @@ -8,13 +8,13 @@ use crate::{ utils::{ markdown::{File, FilePath}, metadata::MType, - misc::{make_kw, read_file, Html}, + misc::{lang, make_kw, read_file, Html, Lang}, }, }; #[get("/")] -pub async fn page(config: web::Data) -> impl Responder { - Html(build_page(config.get_ref().to_owned())) +pub async fn page(req: HttpRequest, config: web::Data) -> impl Responder { + Html(build_page(config.get_ref().to_owned(), lang(req.headers()))) } #[derive(Content, Debug)] @@ -34,15 +34,16 @@ struct StyleAvatar { square: bool, } -#[once(time = 60)] -fn build_page(config: Config) -> String { - let mut file = read_file( +#[cached(time = 60)] +fn build_page(config: Config, lang: Lang) -> String { + let (fp, t) = ( FilePath { - base: config.locations.data_dir, + base: config.locations.data_dir.clone(), path: "index.md".to_owned(), }, MType::Index, ); + let mut file = read_file(fp.clone(), t, Some(lang)).or(read_file(fp, t, None)); // Default values let mut name = config.fc.fullname.clone().unwrap_or_default(); @@ -77,6 +78,7 @@ fn build_page(config: Config) -> String { path: "README.md".to_owned(), }, MType::Generic, + None, ); } diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index e0d3b49..ef12e4f 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -45,6 +45,7 @@ fn build_page(config: Config) -> String { path: "about.md".to_owned(), }, MType::Generic, + None, ); // Get apps @@ -54,6 +55,7 @@ fn build_page(config: Config) -> String { read_file( apps_dir.from(&e.unwrap().to_string_lossy()), MType::Portfolio, + None, ) .unwrap() }) @@ -72,6 +74,7 @@ fn build_page(config: Config) -> String { read_file( apps_dir.from(&e.unwrap().to_string_lossy()), MType::Portfolio, + None, ) .unwrap() }) diff --git a/src/template.rs b/src/template.rs index 52a4f4b..205752d 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,7 +1,7 @@ use ramhorns::{Content, Ramhorns}; /// Structure used in the config variable of the app -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Hash, PartialEq, Eq)] pub struct Template { /// Root directory where templates are stored pub directory: String, diff --git a/src/utils/misc.rs b/src/utils/misc.rs index e70f9fc..d3709cc 100644 --- a/src/utils/misc.rs +++ b/src/utils/misc.rs @@ -59,14 +59,23 @@ impl Responder for Html { } /// Read a file -pub fn read_file(filename: FilePath, expected_file: MType) -> Option { - reader(filename, expected_file) +pub fn read_file(filename: FilePath, expected_file: MType, lang: Option) -> Option { + reader(filename, expected_file, lang.unwrap_or(Lang::English)) } #[cached(time = 600)] -fn reader(filename: FilePath, expected_file: MType) -> Option { - let as_str = filename.to_string(); - let path = Path::new(&as_str); +fn reader(filename: FilePath, expected_file: MType, lang: Lang) -> Option { + let as_str = match lang { + Lang::French => { + let str = filename.to_string(); + let mut parts = str.split('.').collect::>(); + let extension = parts.pop().unwrap_or(""); + let filename = parts.join("."); + &format!("{filename}-fr.{extension}") + } + Lang::English => &filename.to_string(), + }; + let path = Path::new(as_str); if let Ok(metadata) = path.metadata() { // Taille maximale : 30M @@ -78,12 +87,12 @@ fn reader(filename: FilePath, expected_file: MType) -> Option { path.extension().and_then(|ext| { match mime_guess::from_ext(ext.to_str().unwrap_or_default()).first_or_text_plain() { mime if mime == mime::APPLICATION_PDF => { - fs::read(&as_str).map_or(None, |bytes| Some(read_pdf(bytes))) + fs::read(as_str).map_or(None, |bytes| Some(read_pdf(bytes))) } mime if mime.type_() == mime::IMAGE => { - fs::read(&as_str).map_or(None, |bytes| Some(read_img(bytes, &mime))) + fs::read(as_str).map_or(None, |bytes| Some(read_img(bytes, &mime))) } - _ => fs::read_to_string(&as_str).map_or(None, |text| { + _ => fs::read_to_string(as_str).map_or(None, |text| { Some(read_md(&filename, &text, expected_file, None, true)) }), } @@ -119,7 +128,7 @@ pub fn remove_first_letter(s: &str) -> &str { s.chars().next().map(|c| &s[c.len_utf8()..]).unwrap() } -#[derive(Debug)] +#[derive(Hash, PartialEq, Eq, Clone)] pub enum Lang { French, English, diff --git a/src/utils/routes/blog.rs b/src/utils/routes/blog.rs index 56a41df..2c48cee 100644 --- a/src/utils/routes/blog.rs +++ b/src/utils/routes/blog.rs @@ -51,6 +51,7 @@ impl Post { path: format!("{}{ext}", self.url), }, MType::Blog, + None, ) { self.content = Some(file.content); } @@ -149,6 +150,7 @@ pub fn get_post( path: format!("{filename}{ext}"), }, MType::Blog, + None, ); let default = ( diff --git a/src/utils/routes/contact.rs b/src/utils/routes/contact.rs index f8c58bf..c424d29 100644 --- a/src/utils/routes/contact.rs +++ b/src/utils/routes/contact.rs @@ -57,7 +57,14 @@ pub fn remove_paragraphs(list: &mut [File]) { pub fn read(path: &FilePath) -> Vec { glob(&path.to_string()) .unwrap() - .map(|e| read_file(path.from(&e.unwrap().to_string_lossy()), MType::Contact).unwrap()) + .map(|e| { + read_file( + path.from(&e.unwrap().to_string_lossy()), + MType::Contact, + None, + ) + .unwrap() + }) .filter(|f| { !f.metadata .info