Compare commits

..

2 commits

Author SHA1 Message Date
953db2f968
this resolves the crash but not the weird flicker 2024-12-16 19:11:13 +01:00
d9fae875f4
it crashes past ~60 images 2024-12-16 19:11:00 +01:00
34 changed files with 281 additions and 367 deletions

4
.gitignore vendored
View file

@ -7,13 +7,13 @@ docker-compose.yml
/.vscode
# Data
data/index*.md
data/index.md
data/contacts/*
data/cours/*
data/projects/*
# Blog
data/blog/about*.md
data/blog/*.md
data/blog/posts/*
!data/blog/posts/Makefile

152
Cargo.lock generated
View file

@ -85,7 +85,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb"
dependencies = [
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -199,7 +199,7 @@ dependencies = [
"actix-router",
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -346,9 +346,9 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.95"
version = "1.0.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04"
checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7"
[[package]]
name = "arrayvec"
@ -364,14 +364,14 @@ checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
name = "atom_syndication"
version = "0.12.6"
version = "0.12.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec03a6e158ee0f38bfba811976ae909bc2505a4a2f4049c7e8df47df3497b119"
checksum = "3ee79fb83c725eae67b55218870813d2fc39fd85e4f1583848ef9f4f823cfe7c"
dependencies = [
"chrono",
"derive_builder",
@ -507,7 +507,7 @@ dependencies = [
"proc-macro2",
"quote",
"rustversion",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -607,7 +607,7 @@ dependencies = [
"darling",
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -628,9 +628,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.2.5"
version = "1.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e"
checksum = "9157bbaa6b165880c27a4293a474c91cdcf265cc68cc829bf10be0964a391caf"
dependencies = [
"shlex",
]
@ -706,7 +706,7 @@ dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -723,9 +723,9 @@ checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "comrak"
version = "0.32.0"
version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48ae8f3e7e3f3d424cbb33354fc36943d507327d210aa5794b0192f4be726c6d"
checksum = "453dcb42e33f7b474d7e0db12e0b8d82802c88f35cf5a1d8c297d0dfcecb154f"
dependencies = [
"bon",
"caseless",
@ -813,9 +813,9 @@ dependencies = [
[[package]]
name = "crossbeam-deque"
version = "0.8.6"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
@ -832,9 +832,9 @@ dependencies = [
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
[[package]]
name = "crypto-common"
@ -892,7 +892,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13b588ba4ac1a99f7f2964d24b3d896ddc6bf847ee3855dbd4366f058cfcd331"
dependencies = [
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -922,7 +922,7 @@ dependencies = [
"proc-macro2",
"quote",
"strsim",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -933,7 +933,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
dependencies = [
"darling_core",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -991,7 +991,7 @@ dependencies = [
"darling",
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -1001,7 +1001,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab63b0e2bf4d5928aff72e83a7dace85d7bba5fe12dcc3c5a572d78caffd3f3c"
dependencies = [
"derive_builder_core",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -1014,7 +1014,7 @@ dependencies = [
"proc-macro2",
"quote",
"rustc_version",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -1050,7 +1050,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -1089,29 +1089,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5320ae4c3782150d900b79807611a59a99fc9a1d61d686faafc24b93fc8d7ca"
[[package]]
name = "env_filter"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0"
dependencies = [
"log",
"regex",
]
[[package]]
name = "env_logger"
version = "0.11.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcaee3d8e3cfc3fd92428d477bc97fc29ec8716d180c0d74c643bb26166660e0"
dependencies = [
"anstream",
"anstyle",
"env_filter",
"humantime",
"log",
]
[[package]]
name = "equivalent"
version = "1.0.1"
@ -1140,7 +1117,6 @@ dependencies = [
"chrono-tz",
"comrak",
"cyborgtime",
"env_logger",
"glob",
"lol_html",
"mime_guess",
@ -1192,9 +1168,9 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "foldhash"
version = "0.1.4"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f"
checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2"
[[package]]
name = "foreign-types"
@ -1468,17 +1444,11 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "hyper"
version = "1.5.2"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "256fb8d4bd6413123cc9d91832d78325c48ff41677595be797d90f42969beae0"
checksum = "97818827ef4f364230e16705d4706e2897df2bb60617d6ca15d598025a3c481f"
dependencies = [
"bytes",
"futures-channel",
@ -1496,9 +1466,9 @@ dependencies = [
[[package]]
name = "hyper-rustls"
version = "0.27.5"
version = "0.27.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d191583f3da1305256f22463b9bb0471acad48a4e534a5218b9963e9c1f59b2"
checksum = "08afdbb5c31130e3034af566421053ab03787c640246a446327f550d11bcb333"
dependencies = [
"futures-util",
"http 1.2.0",
@ -1684,7 +1654,7 @@ checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -1802,9 +1772,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "libc"
version = "0.2.169"
version = "0.2.168"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d"
[[package]]
name = "libyml"
@ -1927,7 +1897,7 @@ dependencies = [
"proc-macro2",
"quote",
"regex-syntax",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -2040,9 +2010,9 @@ dependencies = [
[[package]]
name = "miniz_oxide"
version = "0.8.2"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ffbe83022cedc1d264172192511ae958937694cd57ce297164951b8b3568394"
checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1"
dependencies = [
"adler2",
]
@ -2105,9 +2075,9 @@ dependencies = [
[[package]]
name = "object"
version = "0.36.7"
version = "0.36.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87"
checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e"
dependencies = [
"memchr",
]
@ -2163,7 +2133,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -2381,7 +2351,7 @@ dependencies = [
"phf_shared 0.11.2",
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -2461,7 +2431,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033"
dependencies = [
"proc-macro2",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -2575,7 +2545,7 @@ dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -2967,9 +2937,9 @@ dependencies = [
[[package]]
name = "security-framework-sys"
version = "2.13.0"
version = "2.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1863fd3768cd83c56a7f60faa4dc0d403f1b6df0a38c3c25f44b7894e45370d5"
checksum = "fa39c7303dc58b5543c94d22c1766b0d31f2ee58306363ea622b10bbc075eaa2"
dependencies = [
"core-foundation-sys",
"libc",
@ -3018,14 +2988,14 @@ checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
name = "serde_json"
version = "1.0.134"
version = "1.0.133"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d00f4175c42ee48b15416f6193a959ba3a0d67fc699a0db9ad12df9f83991c7d"
checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377"
dependencies = [
"itoa 1.0.14",
"memchr",
@ -3204,9 +3174,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.91"
version = "2.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d53cbcb5a243bd33b7858b1d7f4aca2153490815872d86d955d6ea29f743c035"
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
dependencies = [
"proc-macro2",
"quote",
@ -3230,7 +3200,7 @@ checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -3329,7 +3299,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -3375,9 +3345,9 @@ dependencies = [
[[package]]
name = "tinyvec"
version = "1.8.1"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "022db8904dfa342efe721985167e9fcd16c29b226db4397ed752a761cfce81e8"
checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938"
dependencies = [
"tinyvec_macros",
]
@ -3414,7 +3384,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -3684,7 +3654,7 @@ dependencies = [
"log",
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
"wasm-bindgen-shared",
]
@ -3719,7 +3689,7 @@ checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -3945,7 +3915,7 @@ checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
"synstructure",
]
@ -3967,7 +3937,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]
[[package]]
@ -3987,7 +3957,7 @@ checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
"synstructure",
]
@ -4016,5 +3986,5 @@ checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.91",
"syn 2.0.90",
]

View file

@ -21,7 +21,7 @@ serde_json = "1.0"
minify-html = "0.15"
minify-js = "0.6"
glob = "0.3"
comrak = "0.32"
comrak = "0.31"
reqwest = { version = "0.12", features = ["json"] }
chrono = { version = "0.4.39", default-features = false, features = ["clock"]}
chrono-tz = "0.10"
@ -33,7 +33,6 @@ urlencoding = "2.1"
regex = "1.11"
cyborgtime = "2.1.1"
walkdir = "2.5"
env_logger = "0.11"
[lints.clippy]
pedantic = "warn"

View file

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

View file

@ -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, Hash, PartialEq, Eq)]
#[derive(Clone, Debug, Default, Deserialize)]
pub struct FileConfiguration {
/// http/https
pub scheme: Option<String>,
@ -75,14 +75,14 @@ impl FileConfiguration {
}
// Paths where files are stored
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
#[derive(Clone, Debug)]
pub struct Locations {
pub static_dir: String,
pub data_dir: String,
}
/// Configuration used internally in the app
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
#[derive(Clone, Debug)]
pub struct Config {
/// Information given in the config file
pub fc: FileConfiguration,

View file

@ -1,6 +1,6 @@
use actix_files::Files;
use actix_web::{
middleware::{Compress, DefaultHeaders, Logger},
middleware::{Compress, DefaultHeaders},
web, App, HttpServer,
};
use std::io::Result;
@ -29,12 +29,16 @@ async fn main() -> Result<()> {
config.fc.port.unwrap(),
);
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
println!(
"Listening to {}://{}:{}",
config.clone().fc.scheme.unwrap(),
addr.0,
addr.1
);
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(config.clone()))
.wrap(Logger::default().log_target(config.fc.app_name.clone().unwrap_or_default()))
.wrap(Compress::default())
.wrap(
DefaultHeaders::new()

View file

@ -29,7 +29,6 @@ fn build_securitytxt(config: Config) -> String {
pref_lang: config.fc.lang.unwrap_or_default(),
},
InfosPage::default(),
None,
)
}
@ -57,7 +56,6 @@ fn build_humanstxt(config: Config) -> String {
name: config.fc.fullname.unwrap_or_default(),
},
InfosPage::default(),
None,
)
}
@ -97,6 +95,5 @@ fn build_webmanifest(config: Config) -> String {
url: get_url(config.fc),
},
InfosPage::default(),
None,
)
}

View file

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

View file

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

View file

@ -87,6 +87,5 @@ async fn build_page(config: Config) -> String {
"code",
])),
},
None,
)
}

View file

@ -60,14 +60,13 @@ fn get_content(
path: filename.to_owned(),
},
MType::Cours,
None,
)
}
fn build_page(info: &web::Query<PathRequest>, config: Config) -> String {
let cours_dir = "data/cours";
let (ep, el): (_, Vec<_>) = config
let (ep, el): (_, Vec<String>) = config
.fc
.exclude_courses
.unwrap()
@ -107,6 +106,5 @@ fn build_page(info: &web::Query<PathRequest>, config: Config) -> String {
"digital garden",
])),
},
None,
)
}

View file

@ -1,5 +1,5 @@
use actix_web::{get, web, HttpRequest, Responder};
use cached::proc_macro::cached;
use actix_web::{get, web, Responder};
use cached::proc_macro::once;
use ramhorns::Content;
use crate::{
@ -8,13 +8,13 @@ use crate::{
utils::{
markdown::{File, FilePath},
metadata::MType,
misc::{lang, make_kw, read_file, read_file_fallback, Html, Lang},
misc::{make_kw, read_file, Html},
},
};
#[get("/")]
pub async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
Html(build_page(config.get_ref().to_owned(), lang(req.headers())))
pub async fn page(config: web::Data<Config>) -> impl Responder {
Html(build_page(config.get_ref().to_owned()))
}
#[derive(Content, Debug)]
@ -34,15 +34,14 @@ struct StyleAvatar {
square: bool,
}
#[cached(time = 60)]
fn build_page(config: Config, lang: Lang) -> String {
let (mut file, html_lang) = read_file_fallback(
#[once(time = 60)]
fn build_page(config: Config) -> String {
let mut file = read_file(
FilePath {
base: config.locations.data_dir.clone(),
base: config.locations.data_dir,
path: "index.md".to_owned(),
},
MType::Index,
&lang,
);
// Default values
@ -78,7 +77,6 @@ fn build_page(config: Config, lang: Lang) -> String {
path: "README.md".to_owned(),
},
MType::Generic,
None,
);
}
@ -101,6 +99,5 @@ fn build_page(config: Config, lang: Lang) -> String {
desc: Some("Page principale".into()),
kw: Some(make_kw(&["index", "étudiant", "accueil"])),
},
Some(html_lang),
)
}

View file

@ -32,6 +32,5 @@ fn build_page(config: Config) -> String {
desc: Some("Une page perdu du web".into()),
..InfosPage::default()
},
None,
)
}

View file

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

View file

@ -22,6 +22,5 @@ fn build_page(config: Config) -> String {
desc: Some("Coin reculé de l'internet".into()),
kw: Some(make_kw(&["web3", "blockchain", "nft", "ai"])),
},
None,
)
}

View file

@ -1,9 +1,7 @@
use ramhorns::{Content, Ramhorns};
use crate::utils::misc::Lang;
/// Structure used in the config variable of the app
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
#[derive(Clone, Debug)]
pub struct Template {
/// Root directory where templates are stored
pub directory: String,
@ -53,20 +51,12 @@ struct DataPage<T> {
page_kw: Option<String>,
/// Author's name
page_author: Option<String>,
/// Language used
lang: String,
/// Data needed to render the page
data: T,
}
impl Template {
pub fn render<C: Content>(
&self,
template: &str,
data: C,
info: InfosPage,
lang: Option<String>,
) -> String {
pub fn render<C: Content>(&self, template: &str, data: C, info: InfosPage) -> String {
let mut templates: Ramhorns = Ramhorns::lazy(&self.directory).unwrap();
let tplt = templates.from_file(template).unwrap();
@ -77,7 +67,6 @@ impl Template {
page_desc: info.desc,
page_kw: info.kw,
page_author: self.name.clone(),
lang: lang.unwrap_or(Lang::default()),
data,
})
}

View file

@ -4,6 +4,7 @@ use comrak::nodes::{AstNode, NodeValue};
use comrak::{format_html, parse_document, Arena, ComrakOptions, ListStyleType, Options};
use lol_html::html_content::ContentType;
use lol_html::{element, rewrite_str, HtmlRewriter, RewriteStrSettings, Settings};
use mime_guess::mime;
use ramhorns::Content;
use std::fmt::Debug;
use std::fs;
@ -51,7 +52,7 @@ impl FilePath {
}
/// Options used for parser and compiler MD --> HTML
pub fn get_options(path: Option<FilePath>, metadata_type: MType) -> ComrakOptions<'static> {
pub fn get_options(path: Option<FilePath>, metadata_type: MType) -> ComrakOptions {
comrak::Options {
extension: comrak::ExtensionOptions::builder()
.strikethrough(true)
@ -162,10 +163,15 @@ fn custom_img_size(html: &str) -> String {
rewrite_str(
html,
RewriteStrSettings {
element_content_handlers: vec![element!("img[alt]", |el| {
let alt = el.get_attribute("alt").unwrap();
element_content_handlers: vec![element!("img[alt], svg", |el| {
let alt = el.get_attribute("alt").unwrap_or_default();
let possible_piece = alt.split('|').collect::<Vec<&str>>();
if el.tag_name() == *"svg" {
el.remove_attribute("width");
el.remove_attribute("height");
}
if possible_piece.len() > 1 {
let data = possible_piece.last().unwrap().trim();
@ -255,7 +261,7 @@ fn fix_images_and_integration(
.unwrap()
.to_string();
if let Ok(file_contents) = fs::read(&img_path) {
if let Ok(mut file_contents) = fs::read(&img_path) {
let mime = mime_guess::from_path(&img_path).first_or_octet_stream();
if recursive && mime == "text/markdown" {
let file_str = String::from_utf8_lossy(&file_contents).into_owned();
@ -286,6 +292,16 @@ fn fix_images_and_integration(
// Store the metadata for later merging
additional_metadata.push(data.metadata);
} else if mime == mime::IMAGE_SVG {
let alt = el.get_attribute("alt").unwrap_or_default();
file_contents.drain(0..3);
el.replace(
&format!(
"<svg alt='{alt}'{}",
String::from_utf8(file_contents).unwrap()
),
ContentType::Html,
);
} else {
let image = general_purpose::STANDARD.encode(&file_contents);
el.set_attribute("src", &format!("data:{mime};base64,{image}"))

View file

@ -1,15 +1,12 @@
use std::{fs, os::unix::fs::MetadataExt, path::Path};
use std::{fs, path::Path};
use actix_web::{
http::{
header::{self, ContentType, HeaderMap, TryIntoHeaderValue},
StatusCode,
},
http::header::{self, ContentType, TryIntoHeaderValue},
http::StatusCode,
HttpRequest, HttpResponse, Responder,
};
use base64::{engine::general_purpose, Engine};
use cached::proc_macro::cached;
use mime_guess::mime;
use reqwest::Client;
use crate::config::FileConfiguration;
@ -58,59 +55,21 @@ 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>, String) {
match read_file(filename.clone(), expected_file, Some(lang.clone())) {
None => (
read_file(filename, expected_file, None),
Lang::English.to_string(),
),
data => (data, lang.to_string()),
}
}
/// Read a file
pub fn read_file(filename: FilePath, expected_file: MType, lang: Option<Lang>) -> Option<File> {
reader(filename, expected_file, lang.unwrap_or(Lang::English))
pub fn read_file(filename: FilePath, expected_file: MType) -> Option<File> {
reader(filename, expected_file)
}
#[cached(time = 600)]
fn reader(filename: FilePath, expected_file: MType, lang: Lang) -> Option<File> {
let as_str = match lang {
Lang::French => {
let str = filename.to_string();
let mut parts = str.split('.').collect::<Vec<_>>();
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
if metadata.size() > 30 * 1000 * 1000 {
return None;
}
}
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)))
}
mime if mime.type_() == mime::IMAGE => {
fs::read(as_str).map_or(None, |bytes| Some(read_img(bytes, &mime)))
}
_ => fs::read_to_string(as_str).map_or(None, |text| {
fn reader(filename: FilePath, expected_file: MType) -> Option<File> {
let as_str = filename.to_string();
Path::new(&as_str)
.extension()
.and_then(|ext| match ext.to_str().unwrap() {
"pdf" => fs::read(&as_str).map_or(None, |bytes| Some(read_pdf(bytes))),
_ => fs::read_to_string(&as_str).map_or(None, |text| {
Some(read_md(&filename, &text, expected_file, None, true))
}),
}
})
}
@ -121,59 +80,14 @@ fn read_pdf(data: Vec<u8>) -> File {
metadata: Metadata::default(),
content: format!(
r#"<embed
src="data:{};base64,{pdf}"
src="data:application/pdf;base64,{pdf}"
style="width: 100%; height: 79vh";
>"#,
mime::APPLICATION_PDF
>"#
),
}
}
fn read_img(data: Vec<u8>, mime: &mime::Mime) -> File {
let image = general_purpose::STANDARD.encode(data);
File {
metadata: Metadata::default(),
content: format!("<img src='data:{mime};base64,{image}'>"),
}
}
/// Remove the first character of a string
pub fn remove_first_letter(s: &str) -> &str {
s.chars().next().map(|c| &s[c.len_utf8()..]).unwrap()
}
#[derive(Hash, PartialEq, Eq, Clone)]
pub enum Lang {
French,
English,
}
impl Lang {
pub fn default() -> String {
Lang::French.to_string()
}
}
impl std::fmt::Display for Lang {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Lang::French => write!(f, "fr"),
Lang::English => write!(f, "en"),
}
}
}
/// Get the browser language
pub fn lang(headers: &HeaderMap) -> Lang {
headers
.get("Accept-Language")
.and_then(|lang| lang.to_str().ok())
.and_then(|lang| {
["fr", "fr-FR"]
.into_iter()
.any(|i| lang.contains(i))
.then_some(Lang::French)
})
.unwrap_or(Lang::English)
}

View file

@ -51,7 +51,6 @@ impl Post {
path: format!("{}{ext}", self.url),
},
MType::Blog,
None,
) {
self.content = Some(file.content);
}
@ -150,7 +149,6 @@ pub fn get_post(
path: format!("{filename}{ext}"),
},
MType::Blog,
None,
);
let default = (

View file

@ -57,14 +57,7 @@ pub fn remove_paragraphs(list: &mut [File]) {
pub fn read(path: &FilePath) -> Vec<File> {
glob(&path.to_string())
.unwrap()
.map(|e| {
read_file(
path.from(&e.unwrap().to_string_lossy()),
MType::Contact,
None,
)
.unwrap()
})
.map(|e| read_file(path.from(&e.unwrap().to_string_lossy()), MType::Contact).unwrap())
.filter(|f| {
!f.metadata
.info

View file

@ -58,7 +58,8 @@ main {
}
/* Images */
img {
img,
svg {
max-width: var(--max-width);
}

View file

@ -70,7 +70,8 @@ aside a {
}
/* Image */
main img {
main img,
main svg {
max-width: 100%;
}

View file

@ -37,7 +37,8 @@ main a.anchor {
}
/* Images */
main img {
main img,
main svg {
display: block;
margin: auto;
}
@ -249,6 +250,7 @@ table:not(.hljs-ln) tr th:last-child {
/* Prevent figures from splitting accross pages */
article *:has(img),
article *:has(svg),
table:not(.hljs-ln),
table:not(.hljs-ln) > * {
page-break-inside: avoid;

View file

@ -83,7 +83,7 @@ header nav a:hover {
}
/* Add links */
a:not(:where([href^="#"], [href^="/"])):not(:has(img))::after {
a:not(:where([href^="#"], [href^="/"])):not(:has(img)):not(:has(svg))::after {
content: " (" attr(href) ")";
display: inline-block;
white-space: pre;
@ -96,7 +96,8 @@ header nav a:hover {
}
@media (prefers-color-scheme: dark) {
img {
img,
svg {
filter: brightness(0.8) contrast(1.2);
}
}

View file

@ -4,36 +4,56 @@ const Mode = {
};
/**
* Change the svg color theme based on the mode
* @param {Mode} mode
*/
const svgChangeTheme = (mode) => {
for (const item of document.getElementsByTagName("img")) {
if (!item.src.startsWith("data:image/svg+xml;base64,")) {
// Exclude image who aren't SVG and base64 encoded
continue;
}
/**
* Convert to grayscale
* @param {{r: number, g: number, b: number}} color
* @returns Number between 0 and 255
*/
const colorToGrayscale = (color) => {
const colorToGrayscale = (color) => {
return 0.3 * color.r + 0.59 * color.g + 0.11 * color.b;
};
/**
* Extract colors from an image or SVG element using canvas
* @param {HTMLImageElement | SVGSVGElement} element Image or SVG source element
* @returns {Promise<Array>} Colors representing the element
*/
const extractColors = (element) => {
return new Promise((resolve, reject) => {
// Create a canvas with the same dimensions as the element
const canvas = document.createElement("canvas");
let imageSource;
if (element instanceof SVGSVGElement) {
// SVG element handling
canvas.width = element.viewBox.baseVal.width;
canvas.height = element.viewBox.baseVal.height;
const svgString = new XMLSerializer().serializeToString(element);
const svgBlob = new Blob([svgString], { type: "image/svg+xml" });
const svgUrl = URL.createObjectURL(svgBlob);
imageSource = new Image();
imageSource.src = svgUrl;
imageSource.onload = () => {
processImage(imageSource);
URL.revokeObjectURL(svgUrl);
};
/**
* Extract color using canvas2d
* @param {HTMLImageElement} image Image source
* @returns Colors represeting the image
*/
const extractColors = (image) => {
const canvas = document.createElement("canvas");
canvas.width = image.naturalWidth;
canvas.height = image.naturalHeight;
imageSource.onerror = (error) => {
URL.revokeObjectURL(svgUrl);
reject(error);
};
} else {
// Regular image handling
canvas.width = element.naturalWidth || element.width;
canvas.height = element.naturalHeight || element.height;
processImage(element);
}
const processImage = (img) => {
const ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0);
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(
0,
@ -54,12 +74,52 @@ const svgChangeTheme = (mode) => {
}
}
return colors;
resolve(colors);
};
});
};
// Extract colors
const colors = extractColors(item);
/**
* Change the color theme based on the mode
* @param {Mode} mode
*/
const changeTheme = (mode) => {
// Process SVG elements
for (const svgElement of document.getElementsByTagName("svg")) {
extractColors(svgElement)
.then((colors) => {
applyFilter(mode, svgElement, colors);
})
.catch((error) => {
console.error("Error extracting colors:", error);
});
}
// Process SVG images embedded as base64
for (const item of document.getElementsByTagName("img")) {
if (!item.src.startsWith("data:image/svg+xml;base64,")) {
// Exclude images that aren't SVG and base64 encoded
continue;
}
extractColors(item)
.then((colors) => {
applyFilter(mode, item, colors);
})
.catch((error) => {
console.error("Error extracting colors:", error);
});
}
};
/**
* Apply the filter based on the selected mode
* @param {Mode} mode Current theme
* @param {HTMLImageElement | SVGSVGElement} item Element
* @param {{r: number, g: number, b: number}} colors Array of the colors of the element
* @returns
*/
const applyFilter = (mode, item, colors) => {
// Calculate the average grayscale value
const grayscaleValues = colors.map(colorToGrayscale);
const totalGrayscale = grayscaleValues.reduce((acc, val) => acc + val, 0);
@ -72,26 +132,25 @@ const svgChangeTheme = (mode) => {
if (averageGrayscale < treshold && mode === Mode.Dark) {
item.style = style + dim + " invert(1);";
continue;
return;
}
if (averageGrayscale > treshold && mode === Mode.Light) {
item.style = style + "invert(1);";
continue;
return;
}
if (mode === Mode.Dark) {
item.style = style + `${dim};`;
continue;
return;
}
item.style = "";
}
};
window.addEventListener("load", () => {
// Fix SVG images
svgChangeTheme(
changeTheme(
window.matchMedia("(prefers-color-scheme: dark)").matches
? Mode.Dark
: Mode.Light
@ -101,5 +160,5 @@ window.addEventListener("load", () => {
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", (event) =>
svgChangeTheme(event.matches ? Mode.Dark : Mode.Light)
changeTheme(event.matches ? Mode.Dark : Mode.Light)
);

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="{{lang}}">
<html lang="fr">
<head dir="ltr">
{{>head.html}}
</head>

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="{{lang}}">
<html lang="fr">
<head dir="ltr">
{{>head.html}}
<link

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="{{lang}}">
<html lang="fr">
<head dir="ltr">
{{>head.html}}
<link

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="{{lang}}">
<html lang="fr">
<head dir="ltr">
{{>head.html}}
<link rel="stylesheet" href="/css/contact.css" />

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="{{lang}}">
<html lang="fr">
<head dir="ltr">
{{>head.html}}
<link rel="stylesheet" href="/css/contrib.css" />

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="{{lang}}">
<html lang="fr">
<head dir="ltr">
{{>head.html}}
<link rel="stylesheet" href="/css/cours.css" />

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="{{lang}}">
<html lang="fr">
<head dir="ltr">
{{>head.html}}
<link rel="stylesheet" href="/css/index.css" />

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="{{lang}}">
<html lang="fr">
<head dir="ltr">
{{>head.html}}
<link rel="stylesheet" href="/css/portfolio.css" />

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html class="index" lang="{{lang}}">
<html class="index" lang="fr">
<head dir="ltr">
<title>{{page_title}}{{#page_title}} - {{/page_title}}{{app_name}}</title>
<meta charset="UTF-8" />