Use dialogi to parse blocks
This commit is contained in:
parent
a3cb04f213
commit
ea2675151b
37
Cargo.lock
generated
37
Cargo.lock
generated
@ -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"
|
||||
|
||||
@ -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"
|
||||
|
||||
40
src/dialog.rs
Normal file
40
src/dialog.rs
Normal 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)
|
||||
}
|
||||
69
src/main.rs
69
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, "<h1>{title}</h1>");
|
||||
}
|
||||
@ -580,37 +577,37 @@ fn handle_relative_connection(
|
||||
});
|
||||
|
||||
if interactive {
|
||||
let mut sections = Vec::new();
|
||||
let mut current_section = Vec::new();
|
||||
|
||||
let mut add_section = |current_section: Vec<String>| {
|
||||
if current_section.is_empty() {
|
||||
let Ok(dialogs) = parse_map(file_paths.pk) else {
|
||||
fail(stream);
|
||||
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());
|
||||
};
|
||||
|
||||
for line in lines {
|
||||
let line = line.unwrap_or_default();
|
||||
if !line.is_empty() {
|
||||
current_section.push(line);
|
||||
continue;
|
||||
}
|
||||
let mut sections = Vec::new();
|
||||
|
||||
add_section(std::mem::take(&mut current_section));
|
||||
}
|
||||
for dialog_sequence in dialogs {
|
||||
for block in &dialog_sequence.blocks {
|
||||
let mut block_content = Vec::new();
|
||||
|
||||
add_section(current_section);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
let html = html! {
|
||||
div id="story-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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user