diff options
| author | Ben Bridle <ben@derelict.engineering> | 2026-02-22 10:15:06 +1300 |
|---|---|---|
| committer | Ben Bridle <ben@derelict.engineering> | 2026-02-22 10:15:33 +1300 |
| commit | 700c0ddd79fc6ca01d52250b69b02c1a13d4ddef (patch) | |
| tree | 9c3c31e8d9cde40dbcc689c0abd876e57a10f028 /src/string_utils.rs | |
| parent | 8c2ac6d92f6a4579591f748eebcbca2b9913d92d (diff) | |
| download | toaster-700c0ddd79fc6ca01d52250b69b02c1a13d4ddef.zip | |
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
Diffstat (limited to 'src/string_utils.rs')
| -rw-r--r-- | src/string_utils.rs | 67 |
1 files changed, 66 insertions, 1 deletions
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<H>(&self, hasher: &mut H) where H: std::hash::Hasher { + self.slug().hash(hasher) + } +} + +impl From<String> 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('-') } |
