Made tab content rendering a method of the tab info

This commit is contained in:
p11 2025-05-31 15:32:05 +02:00
parent f7327f31e5
commit 097a5303e9

View File

@ -649,6 +649,30 @@ fn write_tab_styles(stream: &mut TcpStream, count: usize) {
let _ = writeln!(stream, "</style>"); let _ = writeln!(stream, "</style>");
} }
fn render_comments(stream: &mut TcpStream, comments: Vec<Comment>) {
let html = html! {
h2 { "Comments" }
form method="POST" {
input type="text" name="name" value="anon" placeholder="Name";
br;
textarea rows="5" cols="60" name="text" placeholder="Enter comment..." {}
br;
input type="submit" value="Send!";
}
form method="POST" {
input type="submit" value="💖️" name="up";
input type="submit" value="💔️" name="down";
}
@for comment in comments {
fieldset {
legend { (comment.name) }
(maud::PreEscaped(comment.text))
}
}
};
let _ = stream.write_all(html.into_string().as_bytes());
}
enum TabInfo { enum TabInfo {
Lines(Vec<String>), Lines(Vec<String>),
Chara(Vec<String>), Chara(Vec<String>),
@ -657,6 +681,77 @@ enum TabInfo {
Comment(Vec<Comment>), Comment(Vec<Comment>),
} }
impl TabInfo {
fn render_tab_content(
self,
stream: &mut TcpStream,
path: &Path,
relative_path: &str,
censored: bool,
choice: usize,
progress: &str,
mlc_path: Option<&Path>,
file_paths: &DocumentPaths,
) {
match self {
Self::Lines(lines) => {
convert_extended(
lines,
stream,
Settings::default()
.with_handler(entry_handler(path, relative_path, censored))
.with_start_level(1)
.with_use_textboxes(true),
);
}
Self::Chara(lines) => {
convert_extended(
lines,
stream,
Settings::default()
.with_handler(chara_handler(path, relative_path))
.with_start_level(1)
.with_use_textboxes(true),
);
}
Self::Game(config_map) => {
let _ = render_novel(
config_map,
file_paths.pk,
mlc_path,
file_paths.mld,
relative_path,
stream,
choice,
progress,
);
}
Self::Description => {
let Ok(pki_file) = File::open(file_paths.pki.unwrap()) else {
return;
};
let _ = writeln!(stream, "<h2>Description</h2>");
if let Some(audio_path) = &file_paths.audio {
if audio_path.is_file() {
let _ = writeln!(
stream,
"<p><audio controls src=\"/{relative_path}.mp3\"/></p>"
);
}
}
let lines = BufReader::new(pki_file).lines().map_while(Result::ok);
convert_subheader(lines, stream, 1);
}
Self::Comment(comments) => {
render_comments(stream, comments);
}
}
}
}
struct Tab { struct Tab {
title: Box<str>, title: Box<str>,
info: TabInfo, info: TabInfo,
@ -904,91 +999,16 @@ fn handle_relative_connection(
for (i, Tab { info, .. }) in sections.into_iter().enumerate() { for (i, Tab { info, .. }) in sections.into_iter().enumerate() {
let index = i + 1; let index = i + 1;
let _ = write!(stream, r#"<div class="tab-content" id="content-{index}">"#); let _ = write!(stream, r#"<div class="tab-content" id="content-{index}">"#);
match info { info.render_tab_content(
TabInfo::Lines(lines) => { &mut stream,
convert_extended( path,
lines, relative_path,
&mut stream, censored,
Settings::default() choice,
.with_handler(entry_handler(path, relative_path, censored)) progress,
.with_start_level(1) mlc_path.as_deref(),
.with_use_textboxes(true), &file_paths,
); );
}
TabInfo::Chara(lines) => {
convert_extended(
lines,
&mut stream,
Settings::default()
.with_handler(chara_handler(path, relative_path))
.with_start_level(1)
.with_use_textboxes(true),
);
}
TabInfo::Game(config_map) => {
if render_novel(
config_map,
file_paths.pk,
mlc_path.as_deref(),
file_paths.mld,
relative_path,
&mut stream,
choice,
progress,
)
.is_err()
{
fail(stream);
return;
}
}
TabInfo::Description => {
let Ok(pki_file) = File::open(file_paths.pki.unwrap()) else {
unreachable!();
};
let _ = writeln!(stream, "<h2>Description</h2>");
if let Some(audio_path) = &file_paths.audio {
if Path::is_file(audio_path) {
let _ = writeln!(
stream,
"<p><audio controls src=\"/{relative_path}.mp3\"/></p>"
);
}
}
let lines = BufReader::new(pki_file).lines();
convert_subheader(lines.map(Result::unwrap_or_default), &mut stream, 1);
}
TabInfo::Comment(comments) => {
let html = html! {
h2 { "Comments" }
form method="POST" {
input type="text" name="name" value="anon" placeholder="Name";
br;
textarea rows="5" cols="60" name="text" placeholder="Enter comment..." {}
br;
input type="submit" value="Send!";
}
form method="POST" {
input type="submit" value="💖️" name="up";
input type="submit" value="💔️" name="down";
}
};
let _ = stream.write_all(html.into_string().as_bytes());
for Comment { name, text } in comments {
let html = html! {
fieldset {
legend { (name) }
(maud::PreEscaped(text))
}
};
let _ = stream.write_all(html.into_string().as_bytes());
}
}
}
let _ = write!(stream, "</div>"); let _ = write!(stream, "</div>");
} }