normalize paths; fixes #3

This commit is contained in:
Rowan 2024-12-04 22:11:51 -06:00
parent bb088ef018
commit b35047a1af
2 changed files with 23 additions and 2 deletions

View file

@ -48,7 +48,7 @@ impl Context {
}
}
pub fn from_files<'a, T: AsRef<Path>>(
pub fn from_files<'a, T: AsRef<Path> + std::fmt::Debug>(
files: Vec<&PathBuf>,
file_path: &'a FilePath<T>,
) -> Result<Context> {

View file

@ -16,7 +16,7 @@ impl<'a, P: AsRef<Path> + 'a> FilePath<'a, P> {
pub fn new(input: &'a P, output: &'a P) -> Self {
Self {
input: Self::make_recursive(input),
root_dir: Self::strip_wildcards(input),
root_dir: Self::strip_wildcards(Self::strip_dot(&input)),
output,
slug_word_limit: Default::default(),
}
@ -30,6 +30,10 @@ impl<'a, P: AsRef<Path> + 'a> FilePath<'a, P> {
!path.as_ref().contains("*")
}
fn strip_dot(path: &P) -> &Path {
path.as_ref().strip_prefix("./").unwrap_or(path.as_ref())
}
fn strip_wildcards<P2: AsRef<Path> + ?Sized>(path: &'a P2) -> PathBuf {
path.as_ref()
.ancestors()
@ -77,3 +81,20 @@ impl<'a, P: AsRef<Path> + 'a> FilePath<'a, P> {
value.as_ref().strip_prefix(&self.root_dir)
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
use super::FilePath;
#[test]
fn relative_paths() {
let relative = FilePath::new(&"./in", &"./out");
let bare = FilePath::new(&"in", &"out");
let path = "in/nested/deeply/test.md" ;
let expected = Path::new("nested/deeply/test.md");
assert_eq!(expected, relative.strip_root(&path).unwrap());
assert_eq!(expected, bare.strip_root(&path).unwrap());
}
}