diff options
Diffstat (limited to 'src/collect_files.rs')
-rw-r--r-- | src/collect_files.rs | 56 |
1 files changed, 37 insertions, 19 deletions
diff --git a/src/collect_files.rs b/src/collect_files.rs index 64cfc30..9aa4983 100644 --- a/src/collect_files.rs +++ b/src/collect_files.rs @@ -273,27 +273,9 @@ impl Website { if !path.starts_with('/') { path = format!("{}{path}", from.parent_url()); } + let path = make_url_safe(&collapse_path(&path)); - // Iteratively collapse ".." segments. - let mut segments: Vec<&str> = path.split('/') - .filter(|s| !s.is_empty() && *s != ".") - .collect(); - 'outer: loop { - for i in 0..(segments.len().saturating_sub(1)) { - if segments[i] == ".." { - if i == 0 { - segments.remove(0); - } else { - segments.remove(i-1); - segments.remove(i-1); - } - continue 'outer; - } - } - break; - } // Find page with this path in website. - let path = make_url_safe(&segments.join("/")); for page in &self.pages { if page.full_url == path { let root = from.root(); @@ -310,6 +292,21 @@ impl Website { return None; } + pub fn has_static(&self, from: &impl LinkFrom, path: &str) -> Option<String> { + // Attach parent if not an absolute path. + let path = match !path.starts_with('/') { + true => collapse_path(&format!("{}{path}", make_url_safe(from.parent_url()))), + false => collapse_path(path), + }; + for file in &self.static_files { + if file.full_url == path { + let root = from.root(); + return Some(format!("{root}{path}")); + } + } + return None; + } + pub fn has_image(&self, file_name: &str) -> bool { let image_path = format!("images/thumb/{file_name}"); self.static_files.iter().any(|s| s.full_url == image_path) @@ -320,3 +317,24 @@ impl Website { } } + +fn collapse_path(path: &str) -> String { + // Iteratively collapse ".." segments. + let mut segments: Vec<&str> = path.split('/') + .filter(|s| !s.is_empty() && *s != ".") + .collect(); + 'outer: loop { + for i in 0..(segments.len().saturating_sub(1)) { + if segments[i] == ".." { + if i == 0 { + segments.remove(0); + } else { + segments.remove(i-1); + segments.remove(i-1); + } + continue 'outer; + } + } + return segments.join("/"); + } +} |