exclusion?

This commit is contained in:
Mylloon 2024-11-09 16:34:51 +01:00
parent 8e1b036386
commit 47570bf9e3
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -47,7 +47,11 @@ fn compile_patterns(exclusion_list: Vec<String>) -> Vec<Regex> {
.collect()
}
fn get_filetree(dir_path: &str, exclusion_patterns: &Vec<Regex>) -> 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<Regex>) -> 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<Regex>) -> 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<PathRequest>,
exclusion_list: &[String],
exclusion_patterns: &[Regex],
) -> Option<File> {
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<PathRequest>, 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<String>) = 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<Regex> =
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<PathRequest>, 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()),