Moved tab structs and handlers out of relative connection handler
This commit is contained in:
parent
09815b04f4
commit
f7327f31e5
146
src/main.rs
146
src/main.rs
@ -649,6 +649,79 @@ fn write_tab_styles(stream: &mut TcpStream, count: usize) {
|
||||
let _ = writeln!(stream, "</style>");
|
||||
}
|
||||
|
||||
enum TabInfo {
|
||||
Lines(Vec<String>),
|
||||
Chara(Vec<String>),
|
||||
Game(IndexMap<Box<str>, Box<str>>),
|
||||
Description,
|
||||
Comment(Vec<Comment>),
|
||||
}
|
||||
|
||||
struct Tab {
|
||||
title: Box<str>,
|
||||
info: TabInfo,
|
||||
}
|
||||
|
||||
fn entry_handler<W: Write>(
|
||||
path: &Path,
|
||||
relative_path: &str,
|
||||
censored: bool,
|
||||
) -> impl Fn(&str, &mut W, usize) {
|
||||
move |mut entry, output, level| {
|
||||
let level = level + 1;
|
||||
let mut pki_path = path.to_path_buf();
|
||||
let mut audio_path = path.to_path_buf();
|
||||
if let Some((real_entry, _)) = entry.split_once(':') {
|
||||
entry = real_entry
|
||||
}
|
||||
let pki_extension = if censored { "pkc" } else { "pki" };
|
||||
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 {
|
||||
return;
|
||||
};
|
||||
|
||||
let _ = writeln!(
|
||||
output,
|
||||
"<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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn chara_handler<W: Write>(path: &Path, relative_path: &str) -> impl Fn(&str, &mut W, usize) {
|
||||
move |entry, output, _level| {
|
||||
let mut chara_path = path.to_path_buf();
|
||||
chara_path.push(relative_path);
|
||||
chara_path.push(format!("{entry}.chara"));
|
||||
|
||||
let Some(def) = load_character_file(&chara_path) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let html = render_character(entry, &def, relative_path);
|
||||
let _ = output.write_all(html.into_string().as_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_relative_connection(
|
||||
info: Arc<SharedSiteInfo>,
|
||||
mut stream: TcpStream,
|
||||
@ -714,83 +787,10 @@ fn handle_relative_connection(
|
||||
|
||||
section(&mut stream);
|
||||
|
||||
fn entry_handler<W: Write>(
|
||||
path: &Path,
|
||||
relative_path: &str,
|
||||
censored: bool,
|
||||
) -> impl Fn(&str, &mut W, usize) {
|
||||
move |mut entry, output, level| {
|
||||
let level = level + 1;
|
||||
let mut pki_path = path.to_path_buf();
|
||||
let mut audio_path = path.to_path_buf();
|
||||
if let Some((real_entry, _)) = entry.split_once(':') {
|
||||
entry = real_entry
|
||||
}
|
||||
let pki_extension = if censored { "pkc" } else { "pki" };
|
||||
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 {
|
||||
return;
|
||||
};
|
||||
|
||||
let _ = writeln!(
|
||||
output,
|
||||
"<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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn chara_handler<W: Write>(path: &Path, relative_path: &str) -> impl Fn(&str, &mut W, usize) {
|
||||
move |entry, output, _level| {
|
||||
let mut chara_path = path.to_path_buf();
|
||||
chara_path.push(relative_path);
|
||||
chara_path.push(format!("{entry}.chara"));
|
||||
|
||||
let Some(def) = load_character_file(&chara_path) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let html = render_character(entry, &def, relative_path);
|
||||
let _ = output.write_all(html.into_string().as_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
if back {
|
||||
let _ = writeln!(stream, "<h1>{title}</h1>");
|
||||
}
|
||||
|
||||
enum TabInfo {
|
||||
Lines(Vec<String>),
|
||||
Chara(Vec<String>),
|
||||
Game(IndexMap<Box<str>, Box<str>>),
|
||||
Description,
|
||||
Comment(Vec<Comment>),
|
||||
}
|
||||
|
||||
struct Tab {
|
||||
title: Box<str>,
|
||||
info: TabInfo,
|
||||
}
|
||||
|
||||
let mut sections = Vec::new();
|
||||
|
||||
let check_path: &Path = relative_path.as_ref();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user