From ea2675151b15391310f9dc963c966e6e51bbc6c7 Mon Sep 17 00:00:00 2001 From: p11 Date: Wed, 9 Apr 2025 21:36:40 +0200 Subject: [PATCH] Use dialogi to parse blocks --- Cargo.lock | 37 +++++++++++++++++++++++++++ Cargo.toml | 1 + src/dialog.rs | 40 +++++++++++++++++++++++++++++ src/main.rs | 71 +++++++++++++++++++++++++++------------------------ 4 files changed, 115 insertions(+), 34 deletions(-) create mode 100644 src/dialog.rs diff --git a/Cargo.lock b/Cargo.lock index 4dad793..95e65d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -53,12 +53,28 @@ dependencies = [ "syn", ] +[[package]] +name = "dialogi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9a6db7119036ce5911a234bd241269027441ab9a761350b74d4af8cf947dc5" +dependencies = [ + "header-parsing", + "thiserror", +] + [[package]] name = "either" version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +[[package]] +name = "header-parsing" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa272d549c15ab350762fde6dc312ab5c1d383febc0218c40ab1a94b5de60e00" + [[package]] name = "itoa" version = "1.0.14" @@ -128,6 +144,7 @@ name = "pukram-server" version = "0.1.0" dependencies = [ "data-stream", + "dialogi", "maud", "percent-encoding", "pukram2html", @@ -183,6 +200,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "unicode-ident" version = "1.0.14" diff --git a/Cargo.toml b/Cargo.toml index 373c87b..8f27a51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ maud = "0.27.0" pukram2html = "0.3.0" data-stream = "0.3.0" rayon = "1.10.0" +dialogi = "0.3.1" diff --git a/src/dialog.rs b/src/dialog.rs new file mode 100644 index 0000000..648cf98 --- /dev/null +++ b/src/dialog.rs @@ -0,0 +1,40 @@ +use std::path::Path; + +use dialogi::{DialogChange, DialogParameter, DialogSequence}; + +pub struct EmptyDialogContext; + +#[derive(Copy, Clone, PartialEq, Eq, Hash)] +pub struct EmptyDialogParameter; + +impl DialogParameter for EmptyDialogParameter { + type Context = EmptyDialogContext; + + fn create(_: &str, _: &mut Self::Context) -> Option { + None + } +} + +pub struct EmptyDialogChange; + +impl DialogChange for EmptyDialogChange { + type Parameter = EmptyDialogParameter; + + fn default_change(_: Self::Parameter) -> Self { + Self + } + + fn value_change( + _: Self::Parameter, + _: &str, + _: &mut <::Parameter as DialogParameter>::Context, + ) -> Self { + Self + } +} + +pub fn parse_map( + story_path: &Path, +) -> Result>, dialogi::ParsingError> { + DialogSequence::map_from_path(story_path, &mut EmptyDialogContext) +} diff --git a/src/main.rs b/src/main.rs index fa92e28..01282d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ use data_stream::{ FromStream, ToStream, collections::SizeSettings, default_settings::PortableSettings, from_stream, to_stream, }; +use dialog::parse_map; use maud::html; use percent_encoding::percent_decode_str; use pukram2html::{Settings, convert, convert_extended, convert_subheader}; @@ -23,6 +24,8 @@ use rayon::prelude::*; mod request; use request::Request; +mod dialog; + fn main() { let Ok(path) = env::current_dir() else { eprintln!("Current directory does not exist!"); @@ -564,12 +567,6 @@ fn handle_relative_connection( } } - let Ok(pk_file) = File::open(file_paths.pk) else { - unreachable!(); - }; - - let lines = BufReader::new(pk_file).lines(); - if !title.is_empty() { let _ = writeln!(stream, "

{title}

"); } @@ -580,38 +577,38 @@ fn handle_relative_connection( }); if interactive { - let mut sections = Vec::new(); - let mut current_section = Vec::new(); - - let mut add_section = |current_section: Vec| { - if current_section.is_empty() { - return; - } - - let mut section_html = Vec::new(); - convert_extended( - current_section, - &mut section_html, - Settings::default() - .with_handler(entry_handler(path, relative_path, censored)) - .with_start_level(start_level) - .with_use_textboxes(true), - ); - sections.push(String::from_utf8_lossy(§ion_html).into_owned()); + let Ok(dialogs) = parse_map(file_paths.pk) else { + fail(stream); + return; }; - for line in lines { - let line = line.unwrap_or_default(); - if !line.is_empty() { - current_section.push(line); - continue; + let mut sections = Vec::new(); + + for dialog_sequence in dialogs { + for block in &dialog_sequence.blocks { + let mut block_content = Vec::new(); + + convert_subheader( + block.lines.iter().map(|l| l.text.as_ref()), + &mut block_content, + start_level, + ); + + let section_html = html! { + fieldset class="dialog-block" { + @if !block.name.is_empty() { + legend { (block.name) } + } + @if let Ok(content) = String::from_utf8(block_content) { + (maud::PreEscaped(content)) + } + } + }; + + sections.push(section_html); } - - add_section(std::mem::take(&mut current_section)); } - add_section(current_section); - let html = html! { div id="story-container" { div class="textbox-container" { @@ -619,7 +616,7 @@ fn handle_relative_connection( @for (index, section) in sections.iter().enumerate() { div class="story-section" data-section-index=(index) style=(format!("display: {};", if index == 0 { "block" } else { "none" })) { - (maud::PreEscaped(section)) + (section) } } } @@ -744,6 +741,12 @@ fn handle_relative_connection( let _ = write!(stream, "{}", html.into_string()); } else { + let Ok(pk_file) = File::open(file_paths.pk) else { + unreachable!(); + }; + + let lines = BufReader::new(pk_file).lines(); + convert_extended( lines.map(Result::unwrap_or_default), &mut stream,