From 3ccadd2e0a16e1c205ac8be428e0598616909681 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 1 Apr 2024 16:35:53 +0200 Subject: [PATCH] add exclusions --- Cargo.lock | 1 + Cargo.toml | 4 +--- Documentation.md | 1 + src/config.rs | 4 ++++ src/routes/cours.rs | 29 +++++++++++++++++++++-------- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aa9b4e6..329cd4f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1163,6 +1163,7 @@ dependencies = [ "minify-html", "minify-js 0.6.0", "ramhorns", + "regex", "reqwest", "rss", "serde", diff --git a/Cargo.toml b/Cargo.toml index 49a2748..4099bd2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,4 @@ chrono = { version = "0.4", default-features = false, features = ["clock"]} chrono-tz = "0.8" rss = { version = "2.0", features = ["atom"] } lol_html = "1.2" -base64 = "0.22.0" -mime_guess = "2.0.4" -urlencoding = "2.1.3" +regex = "1.10" diff --git a/Documentation.md b/Documentation.md index c8d37ef..b527524 100644 --- a/Documentation.md +++ b/Documentation.md @@ -106,6 +106,7 @@ onion = "http://youraddress.onion/" app_name = "Nickname" # fallback to 'EWP' if none name = "Firstname" fullname = "Fullname" +exclude_courses = [] ``` ## Link shortener for contacts diff --git a/src/config.rs b/src/config.rs index abe83e2..2a5e74a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -27,6 +27,8 @@ pub struct FileConfig { pub name: Option, /// Fullname of website owner pub fullname: Option, + /// List exclusion for courses + pub exclude_courses: Option>, } impl FileConfig { @@ -37,6 +39,7 @@ impl FileConfig { domain: Some("localhost".into()), port: Some(8080), app_name: Some("EWP".into()), + exclude_courses: Some([].into()), ..FileConfig::default() } } @@ -65,6 +68,7 @@ impl FileConfig { app_name: test(a.app_name, d.app_name), name: test(a.name, d.name), fullname: test(a.fullname, d.fullname), + exclude_courses: test(a.exclude_courses, d.exclude_courses), } } } diff --git a/src/routes/cours.rs b/src/routes/cours.rs index 9451825..ffa8e0d 100644 --- a/src/routes/cours.rs +++ b/src/routes/cours.rs @@ -2,6 +2,7 @@ use std::path::Path; use actix_web::{get, web, Responder}; use ramhorns::Content; +use regex::Regex; use serde::{Deserialize, Serialize}; use crate::{ @@ -38,7 +39,7 @@ struct FileNode { } /// Build the filetree -fn get_filetree(dir_path: &str, exclusion_list: &[&str]) -> FileNode { +fn get_filetree(dir_path: &str, exclusion_list: Vec) -> FileNode { let entries = std::fs::read_dir(dir_path).unwrap(); let mut children = Vec::new(); @@ -47,7 +48,16 @@ fn get_filetree(dir_path: &str, exclusion_list: &[&str]) -> FileNode { let entry_name = entry_path.file_name().and_then(|n| n.to_str()).unwrap(); // We should support regex? - if !exclusion_list.contains(&entry_name) { + if !exclusion_list + .iter() + .any(|pattern| match Regex::new(pattern) { + Ok(re) => re.is_match(entry_name), + Err(e) => { + eprintln!("{e}"); + false + } + }) + { let filename = entry_name.to_string(); if entry_path.is_file() { children.push(FileNode { @@ -56,7 +66,10 @@ fn get_filetree(dir_path: &str, exclusion_list: &[&str]) -> FileNode { children: vec![], }); } else { - children.push(get_filetree(entry_path.to_str().unwrap(), exclusion_list)); + children.push(get_filetree( + entry_path.to_str().unwrap(), + exclusion_list.to_owned(), + )); } } } @@ -76,7 +89,7 @@ fn get_filetree(dir_path: &str, exclusion_list: &[&str]) -> FileNode { fn get_content( cours_dir: &str, path: &web::Query, - exclusion_list: &[&str], + exclusion_list: Vec, ) -> Option { let filename = match &path.q { Some(q) => q, @@ -86,7 +99,7 @@ fn get_content( // We should support regex? if exclusion_list .iter() - .any(|&excluded_term| filename.contains(excluded_term)) + .any(|excluded_term| filename.contains(excluded_term.as_str())) { return None; } @@ -101,8 +114,8 @@ fn get_content( // TODO: Uncomment before release fn build_page(info: web::Query, config: Config) -> String { let cours_dir = "data/cours"; - let exclusion_list = []; - let filetree = get_filetree(cours_dir, &exclusion_list); + let exclusion_list = config.fc.exclude_courses.unwrap(); + let filetree = get_filetree(cours_dir, exclusion_list.to_owned()); config.tmpl.render( "cours.html", @@ -112,7 +125,7 @@ fn build_page(info: web::Query, config: Config) -> String { ..NavBar::default() }, filetree: serde_json::to_string(&filetree).unwrap(), - content: get_content(cours_dir, &info, &exclusion_list), + content: get_content(cours_dir, &info, exclusion_list), }, Infos { page_title: Some("Cours".into()),