Added interactive mode

This commit is contained in:
p11 2023-11-07 20:26:22 +01:00
parent 64e0637349
commit f2bb22ed6c

View File

@ -170,11 +170,11 @@ fn handle_connection(context: Arc<Mutex<Context>>, 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<Mutex<Context>>, path: PathBuf, mut stream: Tc
return;
}
let (path, num) = if let Some((path, num)) = path.rsplit_once('=') {
(path, num.parse::<usize>().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<Mutex<Context>>, 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<usize>,
start_level: usize,
) {
let mut name = None;
@ -409,16 +417,81 @@ fn handle_relative_connection(
};
if let Ok(pk_file) = File::open(pk_path) {
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, "<a href=\"/{relative_path}={prev}\">&lt; Prev</a>");
} else {
let _ = writeln!(stream, "<font color=\"gray\">&lt; Prev</font>");
}
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, "<a href=\"/{relative_path}={next}\">Next &gt;</a>");
} else {
let _ = writeln!(stream, "<font color=\"gray\">Next &gt;</font>");
}
let mut has_text = false;
convert_extended(
BufReader::new(pk_file)
.lines()
.map(|line| line.unwrap_or_default()),
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, "<a href=\"/{relative_path}=0\">&lt; Play &gt;</a>");
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,15 +507,64 @@ fn handle_relative_connection(
if let Some(pki_path) = pki_path {
if let Ok(pki_file) = File::open(pki_path) {
let _ = writeln!(stream, "<h1>Description</h1>");
let lines = BufReader::new(pki_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;
}
}
convert_subheader(
BufReader::new(pki_file)
.lines()
.map(|line| line.unwrap_or_default()),
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, "<a href=\"/{relative_path}={prev}\">&lt; Prev</a>");
}
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, "<a href=\"/{relative_path}={next}\">Next &gt;</a>");
}
} else {
convert_subheader(lines.map(|line| line.unwrap_or_default()), &mut stream, 1);
}
let _ = writeln!(stream, "<hr>");
let _ = writeln!(stream, "<a href=\"/{parent_path}\">&lt;&lt; Back</a>");
} else {