From 097a5303e9dc3ea0048a0b298ac920c5e0dbd8e3 Mon Sep 17 00:00:00 2001 From: p11 Date: Sat, 31 May 2025 15:32:05 +0200 Subject: [PATCH] Made tab content rendering a method of the tab info --- src/main.rs | 190 +++++++++++++++++++++++++++++----------------------- 1 file changed, 105 insertions(+), 85 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7878965..bb5844c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -649,6 +649,30 @@ fn write_tab_styles(stream: &mut TcpStream, count: usize) { let _ = writeln!(stream, ""); } +fn render_comments(stream: &mut TcpStream, comments: Vec) { + 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 { Lines(Vec), Chara(Vec), @@ -657,6 +681,77 @@ enum TabInfo { Comment(Vec), } +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, "

Description

"); + + if let Some(audio_path) = &file_paths.audio { + if audio_path.is_file() { + let _ = writeln!( + stream, + "

" + ); + } + } + + 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 { title: Box, info: TabInfo, @@ -904,91 +999,16 @@ fn handle_relative_connection( for (i, Tab { info, .. }) in sections.into_iter().enumerate() { let index = i + 1; let _ = write!(stream, r#"
"#); - match info { - TabInfo::Lines(lines) => { - convert_extended( - lines, - &mut stream, - Settings::default() - .with_handler(entry_handler(path, relative_path, censored)) - .with_start_level(1) - .with_use_textboxes(true), - ); - } - 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, "

Description

"); - - if let Some(audio_path) = &file_paths.audio { - if Path::is_file(audio_path) { - let _ = writeln!( - stream, - "

" - ); - } - } - - 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()); - } - } - } + info.render_tab_content( + &mut stream, + path, + relative_path, + censored, + choice, + progress, + mlc_path.as_deref(), + &file_paths, + ); let _ = write!(stream, "
"); }