Generate the minimum relative path by chopping off the shared stem.
This commit is contained in:
parent
68cae57f16
commit
ab36a60545
@ -55,14 +55,23 @@ fn get_web_path<D: AsRef<Path>, F: AsRef<Path>, P: AsRef<Path>>(
|
|||||||
let containing_file = containing_file.as_ref();
|
let containing_file = containing_file.as_ref();
|
||||||
let containing_file_relative_to_output_directory =
|
let containing_file_relative_to_output_directory =
|
||||||
containing_file.strip_prefix(output_directory)?;
|
containing_file.strip_prefix(output_directory)?;
|
||||||
|
let shared_stem = get_shared_steps(
|
||||||
|
containing_file_relative_to_output_directory
|
||||||
|
.parent()
|
||||||
|
.ok_or("File should exist in a folder.")?,
|
||||||
|
path_from_web_root
|
||||||
|
.parent()
|
||||||
|
.ok_or("File should exist in a folder.")?,
|
||||||
|
)
|
||||||
|
.collect::<PathBuf>();
|
||||||
// Subtracting 1 from the depth to "remove" the file name.
|
// Subtracting 1 from the depth to "remove" the file name.
|
||||||
let depth_from_web_root = containing_file_relative_to_output_directory
|
let depth_from_shared_stem = containing_file_relative_to_output_directory
|
||||||
|
.strip_prefix(&shared_stem)?
|
||||||
.components()
|
.components()
|
||||||
.count()
|
.count()
|
||||||
- 1;
|
- 1;
|
||||||
let prefix = "../".repeat(depth_from_web_root);
|
let final_path = PathBuf::from("../".repeat(depth_from_shared_stem))
|
||||||
// TODO: It should be possible to strip some of the "../" components based on whether the path_from_web_root goes down the same path as the containing file.
|
.join(path_from_web_root.strip_prefix(shared_stem)?);
|
||||||
let final_path = PathBuf::from(prefix).join(path_from_web_root);
|
|
||||||
let final_string = final_path
|
let final_string = final_path
|
||||||
.as_path()
|
.as_path()
|
||||||
.to_str()
|
.to_str()
|
||||||
@ -83,42 +92,19 @@ fn get_web_path<D: AsRef<Path>, F: AsRef<Path>, P: AsRef<Path>>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn count_shared_steps<A: AsRef<Path>, B: AsRef<Path>>(left: A, right: B) -> usize {
|
|
||||||
let left = left.as_ref();
|
|
||||||
let right = right.as_ref();
|
|
||||||
left.components()
|
|
||||||
.zip(right.components())
|
|
||||||
.position(|(l, r)| l != r)
|
|
||||||
.unwrap_or_else(|| left.components().count())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn get_shared_steps<'a>(left: &'a Path, right: &'a Path) -> impl Iterator<Item = Component<'a>> {
|
fn get_shared_steps<'a>(left: &'a Path, right: &'a Path) -> impl Iterator<Item = Component<'a>> {
|
||||||
let foo: Vec<Component<'_>> = left
|
let shared_stem = left
|
||||||
.components()
|
.components()
|
||||||
.zip(right.components())
|
.zip(right.components())
|
||||||
.take_while(|(l, r)| l == r)
|
.take_while(|(l, r)| l == r)
|
||||||
.map(|(l, _r)| l)
|
.map(|(l, _r)| l);
|
||||||
.collect();
|
shared_stem
|
||||||
foo.into_iter()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_count_shared_steps() {
|
|
||||||
assert_eq!(count_shared_steps("", ""), 0);
|
|
||||||
assert_eq!(count_shared_steps("foo.txt", "foo.txt"), 1);
|
|
||||||
assert_eq!(count_shared_steps("cat/foo.txt", "dog/foo.txt"), 0);
|
|
||||||
assert_eq!(
|
|
||||||
count_shared_steps("foo/bar/baz/lorem.txt", "foo/bar/ipsum/dolar.txt"),
|
|
||||||
2
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_shared_steps() {
|
fn test_get_shared_steps() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user