Use dialogi to parse blocks

This commit is contained in:
p11 2025-04-09 21:36:40 +02:00
parent a3cb04f213
commit ea2675151b
4 changed files with 115 additions and 34 deletions

37
Cargo.lock generated
View File

@ -53,12 +53,28 @@ dependencies = [
"syn", "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]] [[package]]
name = "either" name = "either"
version = "1.15.0" version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]]
name = "header-parsing"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa272d549c15ab350762fde6dc312ab5c1d383febc0218c40ab1a94b5de60e00"
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.14" version = "1.0.14"
@ -128,6 +144,7 @@ name = "pukram-server"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"data-stream", "data-stream",
"dialogi",
"maud", "maud",
"percent-encoding", "percent-encoding",
"pukram2html", "pukram2html",
@ -183,6 +200,26 @@ dependencies = [
"unicode-ident", "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]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.14" version = "1.0.14"

View File

@ -9,3 +9,4 @@ maud = "0.27.0"
pukram2html = "0.3.0" pukram2html = "0.3.0"
data-stream = "0.3.0" data-stream = "0.3.0"
rayon = "1.10.0" rayon = "1.10.0"
dialogi = "0.3.1"

40
src/dialog.rs Normal file
View File

@ -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<Self> {
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 <<Self as DialogChange>::Parameter as DialogParameter>::Context,
) -> Self {
Self
}
}
pub fn parse_map(
story_path: &Path,
) -> Result<Vec<DialogSequence<EmptyDialogChange, EmptyDialogParameter>>, dialogi::ParsingError> {
DialogSequence::map_from_path(story_path, &mut EmptyDialogContext)
}

View File

@ -15,6 +15,7 @@ use data_stream::{
FromStream, ToStream, collections::SizeSettings, default_settings::PortableSettings, FromStream, ToStream, collections::SizeSettings, default_settings::PortableSettings,
from_stream, to_stream, from_stream, to_stream,
}; };
use dialog::parse_map;
use maud::html; use maud::html;
use percent_encoding::percent_decode_str; use percent_encoding::percent_decode_str;
use pukram2html::{Settings, convert, convert_extended, convert_subheader}; use pukram2html::{Settings, convert, convert_extended, convert_subheader};
@ -23,6 +24,8 @@ use rayon::prelude::*;
mod request; mod request;
use request::Request; use request::Request;
mod dialog;
fn main() { fn main() {
let Ok(path) = env::current_dir() else { let Ok(path) = env::current_dir() else {
eprintln!("Current directory does not exist!"); 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() { if !title.is_empty() {
let _ = writeln!(stream, "<h1>{title}</h1>"); let _ = writeln!(stream, "<h1>{title}</h1>");
} }
@ -580,38 +577,38 @@ fn handle_relative_connection(
}); });
if interactive { if interactive {
let mut sections = Vec::new(); let Ok(dialogs) = parse_map(file_paths.pk) else {
let mut current_section = Vec::new(); fail(stream);
return;
let mut add_section = |current_section: Vec<String>| {
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(&section_html).into_owned());
}; };
for line in lines { let mut sections = Vec::new();
let line = line.unwrap_or_default();
if !line.is_empty() { for dialog_sequence in dialogs {
current_section.push(line); for block in &dialog_sequence.blocks {
continue; 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! { let html = html! {
div id="story-container" { div id="story-container" {
div class="textbox-container" { div class="textbox-container" {
@ -619,7 +616,7 @@ fn handle_relative_connection(
@for (index, section) in sections.iter().enumerate() { @for (index, section) in sections.iter().enumerate() {
div class="story-section" data-section-index=(index) div class="story-section" data-section-index=(index)
style=(format!("display: {};", if index == 0 { "block" } else { "none" })) { 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()); let _ = write!(stream, "{}", html.into_string());
} else { } else {
let Ok(pk_file) = File::open(file_paths.pk) else {
unreachable!();
};
let lines = BufReader::new(pk_file).lines();
convert_extended( convert_extended(
lines.map(Result::unwrap_or_default), lines.map(Result::unwrap_or_default),
&mut stream, &mut stream,