diff --git a/src/main.rs b/src/main.rs index 85eb12b..beb47be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -170,11 +170,11 @@ fn handle_connection(context: Arc>, path: PathBuf, mut stream: Tc let mut pk_path = path.to_path_buf(); let mut data_path = path.to_path_buf(); - let (pki_path, start_level, relative_path) = if relative_path.is_empty() { + let (pki_path, start_level, relative_path, partial) = if relative_path.is_empty() { pk_path.push("index.pk"); data_path.push("index.dat"); - (None, 0, String::new()) + (None, 0, String::new(), None) } else { let mut pki_path = path.to_path_buf(); @@ -186,11 +186,17 @@ fn handle_connection(context: Arc>, path: PathBuf, mut stream: Tc return; } + let (path, num) = if let Some((path, num)) = path.rsplit_once('=') { + (path, num.parse::().ok()) + } else { + (path.as_ref(), None) + }; + pk_path.push(format!("{path}.pk")); pki_path.push(format!("{path}.pki")); data_path.push(format!("{path}.dat")); - (Some(pki_path), 1, path.to_string()) + (Some(pki_path), 1, path.to_string(), num) }; if relative_path.split('/').any(|name| name == "Images") { @@ -240,6 +246,7 @@ fn handle_connection(context: Arc>, path: PathBuf, mut stream: Tc &pk_path, pki_path.as_ref().map(|path| path.as_ref()), &data_path, + partial, start_level, ) } @@ -290,6 +297,7 @@ fn handle_relative_connection( pk_path: &Path, pki_path: Option<&Path>, data_path: &Path, + partial: Option, start_level: usize, ) { let mut name = None; @@ -409,16 +417,81 @@ fn handle_relative_connection( }; if let Ok(pk_file) = File::open(pk_path) { - convert_extended( - BufReader::new(pk_file) - .lines() - .map(|line| line.unwrap_or_default()), - &mut stream, - Settings::default() - .with_handler(handle_entry) - .with_start_level(start_level) - .with_use_textboxes(true), - ); + let lines = BufReader::new(pk_file).lines(); + + if let Some(i) = partial { + let mut last_empty = true; + let mut block = 0; + let lines: Vec<_> = lines.map(|line| line.unwrap_or_default()).collect(); + let mut lines = lines.into_iter(); + if i > 0 { + while let Some(line) = lines.next() { + let empty = line.trim().is_empty(); + if empty == last_empty { + continue; + } + if empty { + block += 1; + if block == i { + break; + } + } + last_empty = empty; + } + } + + if i > 0 { + let prev = i - 1; + let _ = writeln!(stream, "< Prev"); + } else { + let _ = writeln!(stream, "< Prev"); + } + + for line in lines.clone() { + let empty = line.trim().is_empty(); + if empty == last_empty { + continue; + } + if empty { + block += 1; + } + last_empty = empty; + } + if i + 1 < block { + let next = i + 1; + let _ = writeln!(stream, "Next >"); + } else { + let _ = writeln!(stream, "Next >"); + } + + let mut has_text = false; + convert_extended( + lines + .take_while(|line| { + let empty = line.trim().is_empty(); + if !empty { + has_text = true; + } + !empty || !has_text + }) + .chain(std::iter::once(String::new())), + &mut stream, + Settings::default() + .with_handler(handle_entry) + .with_start_level(start_level) + .with_use_textboxes(true), + ); + } else { + let _ = writeln!(stream, "< Play >"); + convert_extended( + lines.map(|line| line.unwrap_or_default()), + &mut stream, + Settings::default() + .with_handler(handle_entry) + .with_start_level(start_level) + .with_use_textboxes(true), + ); + } } else { unreachable!(); } @@ -434,14 +507,63 @@ fn handle_relative_connection( if let Some(pki_path) = pki_path { if let Ok(pki_file) = File::open(pki_path) { let _ = writeln!(stream, "

Description

"); + let lines = BufReader::new(pki_file).lines(); - convert_subheader( - BufReader::new(pki_file) - .lines() - .map(|line| line.unwrap_or_default()), - &mut stream, - 1, - ); + if let Some(i) = partial { + let mut last_empty = true; + let mut block = 0; + let lines: Vec<_> = lines.map(|line| line.unwrap_or_default()).collect(); + let mut lines = lines.into_iter(); + if i > 0 { + while let Some(line) = lines.next() { + let empty = line.trim().is_empty(); + if empty == last_empty { + continue; + } + if empty { + block += 1; + if block == i { + break; + } + } + last_empty = empty; + } + } + convert_subheader( + lines + .clone() + .take_while(|line| { + let empty = line.trim().is_empty(); + last_empty = empty; + empty && !last_empty + }) + .chain(std::iter::once(String::new())), + &mut stream, + 1, + ); + + if i > 0 { + let prev = i - 1; + let _ = writeln!(stream, "< Prev"); + } + + for line in lines { + let empty = line.trim().is_empty(); + if empty == last_empty { + continue; + } + if empty { + block += 1; + } + last_empty = empty; + } + if i + 1 < block { + let next = i + 1; + let _ = writeln!(stream, "Next >"); + } + } else { + convert_subheader(lines.map(|line| line.unwrap_or_default()), &mut stream, 1); + } let _ = writeln!(stream, "
"); let _ = writeln!(stream, "<< Back");