Added interactive mode
This commit is contained in:
parent
64e0637349
commit
f2bb22ed6c
162
src/main.rs
162
src/main.rs
@ -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 pk_path = path.to_path_buf();
|
||||||
let mut data_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");
|
pk_path.push("index.pk");
|
||||||
data_path.push("index.dat");
|
data_path.push("index.dat");
|
||||||
|
|
||||||
(None, 0, String::new())
|
(None, 0, String::new(), None)
|
||||||
} else {
|
} else {
|
||||||
let mut pki_path = path.to_path_buf();
|
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;
|
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"));
|
pk_path.push(format!("{path}.pk"));
|
||||||
pki_path.push(format!("{path}.pki"));
|
pki_path.push(format!("{path}.pki"));
|
||||||
data_path.push(format!("{path}.dat"));
|
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") {
|
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,
|
&pk_path,
|
||||||
pki_path.as_ref().map(|path| path.as_ref()),
|
pki_path.as_ref().map(|path| path.as_ref()),
|
||||||
&data_path,
|
&data_path,
|
||||||
|
partial,
|
||||||
start_level,
|
start_level,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -290,6 +297,7 @@ fn handle_relative_connection(
|
|||||||
pk_path: &Path,
|
pk_path: &Path,
|
||||||
pki_path: Option<&Path>,
|
pki_path: Option<&Path>,
|
||||||
data_path: &Path,
|
data_path: &Path,
|
||||||
|
partial: Option<usize>,
|
||||||
start_level: usize,
|
start_level: usize,
|
||||||
) {
|
) {
|
||||||
let mut name = None;
|
let mut name = None;
|
||||||
@ -409,16 +417,81 @@ fn handle_relative_connection(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if let Ok(pk_file) = File::open(pk_path) {
|
if let Ok(pk_file) = File::open(pk_path) {
|
||||||
convert_extended(
|
let lines = BufReader::new(pk_file).lines();
|
||||||
BufReader::new(pk_file)
|
|
||||||
.lines()
|
if let Some(i) = partial {
|
||||||
.map(|line| line.unwrap_or_default()),
|
let mut last_empty = true;
|
||||||
&mut stream,
|
let mut block = 0;
|
||||||
Settings::default()
|
let lines: Vec<_> = lines.map(|line| line.unwrap_or_default()).collect();
|
||||||
.with_handler(handle_entry)
|
let mut lines = lines.into_iter();
|
||||||
.with_start_level(start_level)
|
if i > 0 {
|
||||||
.with_use_textboxes(true),
|
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}\">< Prev</a>");
|
||||||
|
} else {
|
||||||
|
let _ = writeln!(stream, "<font color=\"gray\">< 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 ></a>");
|
||||||
|
} else {
|
||||||
|
let _ = writeln!(stream, "<font color=\"gray\">Next ></font>");
|
||||||
|
}
|
||||||
|
|
||||||
|
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, "<a href=\"/{relative_path}=0\">< Play ></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 {
|
} else {
|
||||||
unreachable!();
|
unreachable!();
|
||||||
}
|
}
|
||||||
@ -434,14 +507,63 @@ fn handle_relative_connection(
|
|||||||
if let Some(pki_path) = pki_path {
|
if let Some(pki_path) = pki_path {
|
||||||
if let Ok(pki_file) = File::open(pki_path) {
|
if let Ok(pki_file) = File::open(pki_path) {
|
||||||
let _ = writeln!(stream, "<h1>Description</h1>");
|
let _ = writeln!(stream, "<h1>Description</h1>");
|
||||||
|
let lines = BufReader::new(pki_file).lines();
|
||||||
|
|
||||||
convert_subheader(
|
if let Some(i) = partial {
|
||||||
BufReader::new(pki_file)
|
let mut last_empty = true;
|
||||||
.lines()
|
let mut block = 0;
|
||||||
.map(|line| line.unwrap_or_default()),
|
let lines: Vec<_> = lines.map(|line| line.unwrap_or_default()).collect();
|
||||||
&mut stream,
|
let mut lines = lines.into_iter();
|
||||||
1,
|
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, "<a href=\"/{relative_path}={prev}\">< 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 ></a>");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
convert_subheader(lines.map(|line| line.unwrap_or_default()), &mut stream, 1);
|
||||||
|
}
|
||||||
|
|
||||||
let _ = writeln!(stream, "<hr>");
|
let _ = writeln!(stream, "<hr>");
|
||||||
let _ = writeln!(stream, "<a href=\"/{parent_path}\"><< Back</a>");
|
let _ = writeln!(stream, "<a href=\"/{parent_path}\"><< Back</a>");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user