Basic cours support #44

Merged
Anri merged 67 commits from cours into main 2024-04-01 18:11:49 +02:00
Showing only changes of commit 9dafbae3d5 - Show all commits

View file

@ -38,30 +38,37 @@ struct FileNode {
children: Vec<FileNode>,
}
fn get_filetree(dir_path: &str, exclusion_list: Vec<String>) -> FileNode {
let children = std::fs::read_dir(dir_path).unwrap()
fn compile_patterns(exclusion_list: &[String]) -> Vec<Regex> {
exclusion_list
.iter()
.map(|pattern| Regex::new(pattern).unwrap())
.collect()
}
fn get_filetree(dir_path: &str, exclusion_patterns: Vec<Regex>) -> FileNode {
let children = std::fs::read_dir(dir_path)
.unwrap()
.filter_map(Result::ok)
.filter_map(|entry| {
let entry_path = entry.path();
let entry_name = entry_path.file_name()?.to_str()?;
let entry_name = entry_path.file_name()?.to_string_lossy().to_string();
// Exclude element with the exclusion_list
if exclusion_list.iter().any(|pattern| {
Regex::new(pattern)
.map(|re| re.is_match(entry_name))
.unwrap_or(false)
}) {
if exclusion_patterns.iter().any(|re| re.is_match(&entry_name)) {
return None;
}
if entry_path.is_file() {
Some(FileNode {
name: entry_name.to_string(),
name: entry_name,
is_dir: false,
children: vec![],
})
} else {
Some(get_filetree(entry_path.to_str().unwrap(), exclusion_list.clone()))
Some(get_filetree(
entry_path.to_str().unwrap(),
exclusion_patterns.to_owned(),
))
}
})
.collect();
@ -107,7 +114,8 @@ 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 filetree = get_filetree(cours_dir, exclusion_list.to_owned());
let exclusion_patterns = compile_patterns(&exclusion_list);
let filetree = get_filetree(cours_dir, exclusion_patterns);
config.tmpl.render(
"cours.html",