From 700c0ddd79fc6ca01d52250b69b02c1a13d4ddef Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sun, 22 Feb 2026 10:15:06 +1300 Subject: Big rewrite A quick list of everything that's changed: - links to a duplicate heading beneath the same level 1 heading now work - rss feed generation using a .feed file - customisation of the html template using the html.template key - option to use symlinks instead of copying static files - fixed incorrect resolution of internal links - simplified different name forms with the Name type - allow linking to a redirect --- src/string_utils.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) (limited to 'src/string_utils.rs') diff --git a/src/string_utils.rs b/src/string_utils.rs index b23c349..2061110 100644 --- a/src/string_utils.rs +++ b/src/string_utils.rs @@ -1,6 +1,71 @@ +use crate::*; + + +#[derive(Clone)] +pub struct Name { + raw: String, +} + +impl Name { + /// Preserve markdown syntax, return raw string. + pub fn raw(&self) -> String { + self.raw.clone() + } + /// Parse markdown syntax, return styled line. + pub fn styled(&self) -> Line { + Line::from_str(&self.raw) + } + /// Strip out markdown syntax, return plain text. + pub fn plain(&self) -> String { + self.styled().to_string() + } + /// Strip out markdown syntax, return url-safe text. + pub fn slug(&self) -> String { + to_slug(&self.plain()) + } +} + +impl std::fmt::Display for Name { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + self.raw.fmt(f) + } +} + +impl std::fmt::Debug for Name { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + self.raw.fmt(f) + } +} + +impl PartialEq for Name { + fn eq(&self, other: &string_utils::Name) -> bool { + self.slug() == other.slug() + } +} + +impl Eq for Name {} +impl std::hash::Hash for Name { + fn hash(&self, hasher: &mut H) where H: std::hash::Hasher { + self.slug().hash(hasher) + } +} + +impl From for Name { + fn from(raw: String) -> Self { + Self { raw } + } +} + +impl From<&str> for Name { + fn from(raw: &str) -> Self { + Self { raw: raw.to_string() } + } +} + + // Turn a string into a tidy URL slug. -pub fn make_url_safe(text: &str) -> String { +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('-') } -- cgit v1.2.3-70-g09d2