diff --git a/src/routes/cours.rs b/src/routes/cours.rs index 93b3291..3464930 100644 --- a/src/routes/cours.rs +++ b/src/routes/cours.rs @@ -47,7 +47,11 @@ fn compile_patterns(exclusion_list: Vec) -> Vec { .collect() } -fn get_filetree(dir_path: &str, exclusion_patterns: &Vec) -> FileNode { +fn get_filetree( + dir_path: &str, + exclusion_list: &[String], + exclusion_patterns: &[Regex], +) -> FileNode { let children = std::fs::read_dir(dir_path) .unwrap() .filter_map(Result::ok) @@ -55,7 +59,13 @@ fn get_filetree(dir_path: &str, exclusion_patterns: &Vec) -> FileNode { let entry_path = entry.path(); let entry_name = entry_path.file_name()?.to_string_lossy().to_string(); - // Exclude element with the exclusion_list + // Exclusion checks + if exclusion_list + .iter() + .any(|excluded_term| entry_name.contains(excluded_term.as_str())) + { + return None; + } if exclusion_patterns.iter().any(|re| re.is_match(&entry_name)) { return None; } @@ -68,8 +78,11 @@ fn get_filetree(dir_path: &str, exclusion_patterns: &Vec) -> FileNode { }) } else { // Exclude empty directories - let children_of_children = - get_filetree(entry_path.to_str().unwrap(), exclusion_patterns); + let children_of_children = get_filetree( + entry_path.to_str().unwrap(), + exclusion_list, + exclusion_patterns, + ); if children_of_children.is_dir && children_of_children.children.is_empty() { None } else { @@ -95,16 +108,20 @@ fn get_content( cours_dir: &str, path: &web::Query, exclusion_list: &[String], + exclusion_patterns: &[Regex], ) -> Option { let filename = path.q.as_ref().map_or("index.md", |q| q); - // We should support regex? + // Exclusion checks if exclusion_list .iter() .any(|excluded_term| filename.contains(excluded_term.as_str())) { return None; } + if exclusion_patterns.iter().any(|re| re.is_match(filename)) { + return None; + } read_file( &format!("{cours_dir}/{filename}"), @@ -114,9 +131,23 @@ fn get_content( fn build_page(info: &web::Query, config: Config) -> String { let cours_dir = "data/cours"; - let exclusion_list = config.fc.exclude_courses.unwrap(); - let exclusion_patterns = compile_patterns(exclusion_list.clone()); - let filetree = get_filetree(cours_dir, &exclusion_patterns); + + let (ep, el): (_, Vec) = config + .fc + .exclude_courses + .unwrap() + .into_iter() + .partition(|item| item.starts_with('/')); + + let exclusion_list = { + let mut base = vec!["../".to_owned()]; + base.extend(el); + base + }; + let exclusion_patterns: Vec = + compile_patterns(ep.iter().map(|r| r[1..r.len() - 1].to_owned()).collect()); + + let filetree = get_filetree(cours_dir, &exclusion_list, &exclusion_patterns); config.tmpl.render( "cours.html", @@ -126,7 +157,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, &exclusion_patterns), }, InfosPage { title: Some("Cours".into()),