From 1a51659875364217c60d68d694112c1c2ce1e2e1 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Fri, 10 Oct 2025 17:19:02 +1300 Subject: Allow folder names to contain a numeric prefix for sorting purposes Markdown files could use a numeric prefix to force files to sort in a particular order, with that prefix being stripped off and ignored by toaster. This commit allows folders to also use a sorting prefix. --- src/collect_files.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src/collect_files.rs') diff --git a/src/collect_files.rs b/src/collect_files.rs index 315a17e..cb785fc 100644 --- a/src/collect_files.rs +++ b/src/collect_files.rs @@ -137,11 +137,7 @@ impl Website { if entry.name.starts_with('.') { return } // Get name and extension. let (mut name, extension) = entry.split_name(); - if let Some((prefix, suffix)) = name.split_once(' ') { - if prefix.chars().all(|c| "0123456789-".contains(c)) { - name = suffix.to_string(); - } - } + name = strip_numeric_prefix(&name); let name_url = make_url_safe(&name); // Get last-modified time. let last_modified = entry.last_modified; @@ -150,7 +146,9 @@ impl Website { let relative_path = source_path.strip_prefix(prefix).unwrap_or_else( |_| fatal!("Path doesn't start with {prefix:?}: {source_path:?}")); let mut parents: Vec<_> = relative_path.components() - .map(|c| c.as_os_str().to_string_lossy().to_string()).collect(); + .map(|c| c.as_os_str().to_string_lossy().to_string()) + .map(|s| strip_numeric_prefix(&s)) + .collect(); parents.pop(); // Remove file segment. // Process each entry. @@ -419,3 +417,13 @@ fn collapse_path(path: &str) -> String { return segments.join("/"); } } + + +fn strip_numeric_prefix(name: &str) -> String { + if let Some((prefix, suffix)) = name.split_once(' ') { + if prefix.chars().all(|c| "0123456789-".contains(c)) { + return suffix.to_string(); + } + } + return name.to_string(); +} -- cgit v1.2.3-70-g09d2