summaryrefslogtreecommitdiff
path: root/src/string_utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/string_utils.rs')
-rw-r--r--src/string_utils.rs18
1 files changed, 13 insertions, 5 deletions
diff --git a/src/string_utils.rs b/src/string_utils.rs
index 2061110..cceb575 100644
--- a/src/string_utils.rs
+++ b/src/string_utils.rs
@@ -66,11 +66,19 @@ impl From<&str> for Name {
// Turn a string into a tidy URL slug.
pub fn to_slug(text: &str) -> String {
- text.to_ascii_lowercase().chars().filter_map(|c|
- if c.is_alphanumeric() || "-_~.+/#".contains(c) { Some(c) }
- else if c == ' ' { Some('-') }
- else { None } )
- .collect()
+ let mut string = String::new();
+ let mut prev = ' ';
+ for c in text.to_lowercase().chars() {
+ let c = match c == ' ' {
+ true => '-',
+ false => c,
+ };
+ if c.is_alphanumeric() { string.push(c) }
+ if "_~.+/#".contains(c) { string.push(c) }
+ if c == '-' && prev != '-' { string.push(c) }
+ prev = c;
+ }
+ return string;
}
// Prevent link hrefs from breaking out of quotations.