Use dialogi to parse blocks
This commit is contained in:
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)
|
||||
}
|
||||
71
src/main.rs
71
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,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<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(§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,
|
||||
|
||||
Reference in New Issue
Block a user