exclusion?
This commit is contained in:
parent
8e1b036386
commit
47570bf9e3
1 changed files with 40 additions and 9 deletions
|
@ -47,7 +47,11 @@ fn compile_patterns(exclusion_list: Vec<String>) -> Vec<Regex> {
|
||||||
.collect()
|
.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)
|
let children = std::fs::read_dir(dir_path)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.filter_map(Result::ok)
|
.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_path = entry.path();
|
||||||
let entry_name = entry_path.file_name()?.to_string_lossy().to_string();
|
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)) {
|
if exclusion_patterns.iter().any(|re| re.is_match(&entry_name)) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
@ -68,8 +78,11 @@ fn get_filetree(dir_path: &str, exclusion_patterns: &Vec<Regex>) -> FileNode {
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
// Exclude empty directories
|
// Exclude empty directories
|
||||||
let children_of_children =
|
let children_of_children = get_filetree(
|
||||||
get_filetree(entry_path.to_str().unwrap(), exclusion_patterns);
|
entry_path.to_str().unwrap(),
|
||||||
|
exclusion_list,
|
||||||
|
exclusion_patterns,
|
||||||
|
);
|
||||||
if children_of_children.is_dir && children_of_children.children.is_empty() {
|
if children_of_children.is_dir && children_of_children.children.is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
|
@ -95,16 +108,20 @@ fn get_content(
|
||||||
cours_dir: &str,
|
cours_dir: &str,
|
||||||
path: &web::Query<PathRequest>,
|
path: &web::Query<PathRequest>,
|
||||||
exclusion_list: &[String],
|
exclusion_list: &[String],
|
||||||
|
exclusion_patterns: &[Regex],
|
||||||
) -> Option<File> {
|
) -> Option<File> {
|
||||||
let filename = path.q.as_ref().map_or("index.md", |q| q);
|
let filename = path.q.as_ref().map_or("index.md", |q| q);
|
||||||
|
|
||||||
// We should support regex?
|
// Exclusion checks
|
||||||
if exclusion_list
|
if exclusion_list
|
||||||
.iter()
|
.iter()
|
||||||
.any(|excluded_term| filename.contains(excluded_term.as_str()))
|
.any(|excluded_term| filename.contains(excluded_term.as_str()))
|
||||||
{
|
{
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
if exclusion_patterns.iter().any(|re| re.is_match(filename)) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
read_file(
|
read_file(
|
||||||
&format!("{cours_dir}/{filename}"),
|
&format!("{cours_dir}/{filename}"),
|
||||||
|
@ -114,9 +131,23 @@ fn get_content(
|
||||||
|
|
||||||
fn build_page(info: &web::Query<PathRequest>, config: Config) -> String {
|
fn build_page(info: &web::Query<PathRequest>, config: Config) -> String {
|
||||||
let cours_dir = "data/cours";
|
let cours_dir = "data/cours";
|
||||||
let exclusion_list = config.fc.exclude_courses.unwrap();
|
|
||||||
let exclusion_patterns = compile_patterns(exclusion_list.clone());
|
let (ep, el): (_, Vec<String>) = config
|
||||||
let filetree = get_filetree(cours_dir, &exclusion_patterns);
|
.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(
|
config.tmpl.render(
|
||||||
"cours.html",
|
"cours.html",
|
||||||
|
@ -126,7 +157,7 @@ fn build_page(info: &web::Query<PathRequest>, config: Config) -> String {
|
||||||
..NavBar::default()
|
..NavBar::default()
|
||||||
},
|
},
|
||||||
filetree: serde_json::to_string(&filetree).unwrap(),
|
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 {
|
InfosPage {
|
||||||
title: Some("Cours".into()),
|
title: Some("Cours".into()),
|
||||||
|
|
Loading…
Reference in a new issue