more localized files + add documentation

This commit is contained in:
Mylloon 2024-12-22 19:33:11 +01:00 committed by Mylloon
parent 77c140a889
commit f725318f56
6 changed files with 51 additions and 29 deletions

View file

@ -145,6 +145,8 @@ Markdown file
Markdown file is stored in `/app/data/index.md` Markdown file is stored in `/app/data/index.md`
> For french clients, `/app/data/index-fr.md` will be read instead.
``` ```
--- ---
name: Option<String> name: Option<String>
@ -188,6 +190,8 @@ Post content
The file is stored at `/app/data/blog/about.md`. The file is stored at `/app/data/blog/about.md`.
> For french clients, `/app/data/blog/about-fr.md` will be read instead.
## Projects ## Projects
Markdown files are stored in `/app/data/projects/apps/` Markdown files are stored in `/app/data/projects/apps/`
@ -214,6 +218,8 @@ files in `archive` subdirectory of `apps`.
The file is stored at `/app/data/projects/about.md`. The file is stored at `/app/data/projects/about.md`.
> For french clients, `/app/data/projects/about-fr.md` will be read instead.
## Contacts ## Contacts
Markdown files are stored in `/app/data/contacts/` Markdown files are stored in `/app/data/contacts/`
@ -254,6 +260,8 @@ For example, `socials` contact files are stored in `/app/data/contacts/socials/`
The file is stored at `/app/data/contacts/about.md`. The file is stored at `/app/data/contacts/about.md`.
> For french clients, `/app/data/contacts/about-fr.md` will be read instead.
## Courses ## Courses
Markdown files are stored in `/app/data/cours/` Markdown files are stored in `/app/data/cours/`

View file

@ -1,5 +1,7 @@
use actix_web::{get, http::header::ContentType, routes, web, HttpResponse, Responder}; use actix_web::{
use cached::proc_macro::once; get, http::header::ContentType, routes, web, HttpRequest, HttpResponse, Responder,
};
use cached::proc_macro::cached;
use ramhorns::Content; use ramhorns::Content;
use crate::{ use crate::{
@ -8,14 +10,17 @@ use crate::{
utils::{ utils::{
markdown::{File, FilePath}, markdown::{File, FilePath},
metadata::MType, metadata::MType,
misc::{make_kw, read_file, Html}, misc::{lang, make_kw, read_file_fallback, Html, Lang},
routes::blog::{build_rss, get_post, get_posts, Post, BLOG_DIR, MIME_TYPE_RSS, POST_DIR}, routes::blog::{build_rss, get_post, get_posts, Post, BLOG_DIR, MIME_TYPE_RSS, POST_DIR},
}, },
}; };
#[get("/blog")] #[get("/blog")]
pub async fn index(config: web::Data<Config>) -> impl Responder { pub async fn index(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
Html(build_index(config.get_ref().to_owned())) Html(build_index(
config.get_ref().to_owned(),
lang(req.headers()),
))
} }
#[derive(Content, Debug)] #[derive(Content, Debug)]
@ -26,19 +31,19 @@ struct BlogIndexTemplate {
no_posts: bool, no_posts: bool,
} }
#[once(time = 60)] #[cached(time = 60)]
fn build_index(config: Config) -> String { fn build_index(config: Config, lang: Lang) -> String {
let blog_dir = format!("{}/{}", config.locations.data_dir, BLOG_DIR); let blog_dir = format!("{}/{}", config.locations.data_dir, BLOG_DIR);
let mut posts = get_posts(&format!("{blog_dir}/{POST_DIR}")); let mut posts = get_posts(&format!("{blog_dir}/{POST_DIR}"));
// Get about // Get about
let about: Option<File> = read_file( let about = read_file_fallback(
FilePath { FilePath {
base: blog_dir, base: blog_dir,
path: "about.md".to_owned(), path: "about.md".to_owned(),
}, },
MType::Generic, MType::Generic,
None, lang,
); );
// Sort from newest to oldest // Sort from newest to oldest

View file

@ -1,5 +1,5 @@
use actix_web::{get, routes, web, HttpRequest, Responder}; use actix_web::{get, routes, web, HttpRequest, Responder};
use cached::proc_macro::once; use cached::proc_macro::cached;
use ramhorns::Content; use ramhorns::Content;
use crate::{ use crate::{
@ -8,7 +8,7 @@ use crate::{
utils::{ utils::{
markdown::{File, FilePath}, markdown::{File, FilePath},
metadata::MType, metadata::MType,
misc::{make_kw, read_file, Html}, misc::{lang, make_kw, read_file_fallback, Html, Lang},
routes::contact::{find_links, read, remove_paragraphs}, routes::contact::{find_links, read, remove_paragraphs},
}, },
}; };
@ -28,8 +28,8 @@ pub fn pages(cfg: &mut web::ServiceConfig) {
} }
#[get("")] #[get("")]
async fn page(config: web::Data<Config>) -> impl Responder { async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
Html(build_page(config.get_ref().to_owned())) Html(build_page(config.get_ref().to_owned(), lang(req.headers())))
} }
#[routes] #[routes]
@ -78,19 +78,19 @@ struct NetworksTemplate {
others: Vec<File>, others: Vec<File>,
} }
#[once(time = 60)] #[cached(time = 60)]
fn build_page(config: Config) -> String { fn build_page(config: Config, lang: Lang) -> String {
let contacts_dir = format!("{}/{}", config.locations.data_dir, CONTACT_DIR); let contacts_dir = format!("{}/{}", config.locations.data_dir, CONTACT_DIR);
let ext = ".md"; let ext = ".md";
// Get about // Get about
let about = read_file( let about = read_file_fallback(
FilePath { FilePath {
base: contacts_dir.clone(), base: contacts_dir.clone(),
path: "about.md".to_owned(), path: "about.md".to_owned(),
}, },
MType::Generic, MType::Generic,
None, lang,
); );
let mut socials = read(&FilePath { let mut socials = read(&FilePath {

View file

@ -8,7 +8,7 @@ use crate::{
utils::{ utils::{
markdown::{File, FilePath}, markdown::{File, FilePath},
metadata::MType, metadata::MType,
misc::{lang, make_kw, read_file, Html, Lang}, misc::{lang, make_kw, read_file, read_file_fallback, Html, Lang},
}, },
}; };
@ -36,14 +36,14 @@ struct StyleAvatar {
#[cached(time = 60)] #[cached(time = 60)]
fn build_page(config: Config, lang: Lang) -> String { fn build_page(config: Config, lang: Lang) -> String {
let (fp, t) = ( let mut file = read_file_fallback(
FilePath { FilePath {
base: config.locations.data_dir.clone(), base: config.locations.data_dir.clone(),
path: "index.md".to_owned(), path: "index.md".to_owned(),
}, },
MType::Index, MType::Index,
lang,
); );
let mut file = read_file(fp.clone(), t, Some(lang)).or(read_file(fp, t, None));
// Default values // Default values
let mut name = config.fc.fullname.clone().unwrap_or_default(); let mut name = config.fc.fullname.clone().unwrap_or_default();

View file

@ -1,5 +1,5 @@
use actix_web::{get, web, Responder}; use actix_web::{get, web, HttpRequest, Responder};
use cached::proc_macro::once; use cached::proc_macro::cached;
use glob::glob; use glob::glob;
use ramhorns::Content; use ramhorns::Content;
@ -9,13 +9,13 @@ use crate::{
utils::{ utils::{
markdown::{File, FilePath}, markdown::{File, FilePath},
metadata::MType, metadata::MType,
misc::{make_kw, read_file, Html}, misc::{lang, make_kw, read_file, read_file_fallback, Html, Lang},
}, },
}; };
#[get("/portfolio")] #[get("/portfolio")]
pub async fn page(config: web::Data<Config>) -> impl Responder { pub async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
Html(build_page(config.get_ref().to_owned())) Html(build_page(config.get_ref().to_owned(), lang(req.headers())))
} }
#[derive(Content, Debug)] #[derive(Content, Debug)]
@ -29,8 +29,8 @@ struct PortfolioTemplate<'a> {
err_msg: &'a str, err_msg: &'a str,
} }
#[once(time = 60)] #[cached(time = 60)]
fn build_page(config: Config) -> String { fn build_page(config: Config, lang: Lang) -> String {
let projects_dir = format!("{}/projects", config.locations.data_dir); let projects_dir = format!("{}/projects", config.locations.data_dir);
let apps_dir = FilePath { let apps_dir = FilePath {
base: format!("{projects_dir}/apps"), base: format!("{projects_dir}/apps"),
@ -39,13 +39,13 @@ fn build_page(config: Config) -> String {
let ext = ".md"; let ext = ".md";
// Get about // Get about
let about = read_file( let about = read_file_fallback(
FilePath { FilePath {
base: projects_dir, base: projects_dir,
path: "about.md".to_owned(), path: "about.md".to_owned(),
}, },
MType::Generic, MType::Generic,
None, lang,
); );
// Get apps // Get apps

View file

@ -58,6 +58,15 @@ impl Responder for Html {
} }
} }
/// Read a file localized, fallback to default file if localized file isn't found
pub fn read_file_fallback(filename: FilePath, expected_file: MType, lang: Lang) -> Option<File> {
read_file(filename.clone(), expected_file, Some(lang)).or(read_file(
filename,
expected_file,
None,
))
}
/// Read a file /// Read a file
pub fn read_file(filename: FilePath, expected_file: MType, lang: Option<Lang>) -> Option<File> { pub fn read_file(filename: FilePath, expected_file: MType, lang: Option<Lang>) -> Option<File> {
reader(filename, expected_file, lang.unwrap_or(Lang::English)) reader(filename, expected_file, lang.unwrap_or(Lang::English))