Defined a new entry handler

This commit is contained in:
p11 2025-04-08 00:29:43 +02:00
parent 3cce8e8aa8
commit 9e180c2258

View File

@ -519,44 +519,50 @@ fn handle_relative_connection(
.rsplit_once('/') .rsplit_once('/')
.map_or(relative_path, |(_, title)| title); .map_or(relative_path, |(_, title)| title);
let handle_entry = |mut entry: &str, output: &mut TcpStream, level: usize| { fn entry_handler(
let level = level + 1; path: &Path,
let mut pki_path = path.to_path_buf(); relative_path: &str,
let mut audio_path = path.to_path_buf(); censored: bool,
if let Some((real_entry, _)) = entry.split_once(':') { ) -> impl Fn(&str, &mut TcpStream, usize) {
entry = real_entry move |mut entry, output, level| {
} let level = level + 1;
let pki_extension = if censored { "pkc" } else { "pki" }; let mut pki_path = path.to_path_buf();
if relative_path.is_empty() { let mut audio_path = path.to_path_buf();
pki_path.push(format!("{entry}.{pki_extension}")); if let Some((real_entry, _)) = entry.split_once(':') {
audio_path.push(format!("{entry}.mp3")); entry = real_entry
} else { }
pki_path.push(format!("{relative_path}/{entry}.{pki_extension}")); let pki_extension = if censored { "pkc" } else { "pki" };
audio_path.push(format!("{relative_path}/{entry}.mp3")); if relative_path.is_empty() {
} pki_path.push(format!("{entry}.{pki_extension}"));
audio_path.push(format!("{entry}.mp3"));
} else {
pki_path.push(format!("{relative_path}/{entry}.{pki_extension}"));
audio_path.push(format!("{relative_path}/{entry}.mp3"));
}
let Ok(file) = File::open(pki_path) else { let Ok(file) = File::open(pki_path) else {
return; return;
}; };
let _ = writeln!(
output,
"<h{level}><a href=\"{relative_path}/{entry}\">{entry}</a></h{level}>"
);
if Path::is_file(&audio_path) {
let _ = writeln!( let _ = writeln!(
output, output,
"<p><audio controls src=\"/{relative_path}/{entry}.mp3\"/></p>" "<h{level}><a href=\"{relative_path}/{entry}\">{entry}</a></h{level}>"
);
if Path::is_file(&audio_path) {
let _ = writeln!(
output,
"<p><audio controls src=\"/{relative_path}/{entry}.mp3\"/></p>"
);
}
convert_subheader(
BufReader::new(file).lines().map(Result::unwrap_or_default),
output,
level,
); );
} }
}
convert_subheader(
BufReader::new(file).lines().map(Result::unwrap_or_default),
output,
level,
);
};
let Ok(pk_file) = File::open(file_paths.pk) else { let Ok(pk_file) = File::open(file_paths.pk) else {
unreachable!(); unreachable!();
@ -571,7 +577,7 @@ fn handle_relative_connection(
lines.map(Result::unwrap_or_default), lines.map(Result::unwrap_or_default),
&mut stream, &mut stream,
Settings::default() Settings::default()
.with_handler(handle_entry) .with_handler(entry_handler(path, relative_path, censored))
.with_start_level(start_level) .with_start_level(start_level)
.with_use_textboxes(true), .with_use_textboxes(true),
); );