The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Replace Spaces In Directory And File Paths With Dashes In Rust

TODO: Pull subtitle into page object
Code
use regex::Regex;

fn scrub_url_path(source: String) -> String {
    let re = Regex::new(r"\s+").unwrap();
    re.replace_all(&source, "-").to_lowercase()
}

A test run looks like this:

Code
fn main() {
    let source = String::from("this Is  123 Some Text");
    let expected = String::from("this-is-123-some-text");
    let result = scrub_url_path(source);
    assert_eq!(expected, result);
}

Features

You'll need to add the _regex__ create with:

Code
cargo add regex

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Replace Spaces In Directory And File Paths With 
Dashes In Rust

I store the content for my site in plain-text files^g:neo^^
in my Grimoire^g:grim^^. After years of avoiding them, 
I've started using spaces in the filenames. They're
what I see when I search and they're easier to parse
with spaces. 

The same filenames are used for building the web 
pages but I *do not** want spaces there. I also 
don't want the uppercase letters that I also use
in the raw filenames. 

This is the little function I use to take care 
of all that for me. 

-- code
-- rust

use regex::Regex;

fn scrub_url_path(source: String) -> String {
    let re = Regex::new(r"\s+").unwrap();
    re.replace_all(&source, "-").to_lowercase()
}

-- p

A test run looks like this:

-- code
-- rust

fn main() {
    let source = String::from("this Is  123 Some Text");
    let expected = String::from("this-is-123-some-text");
    let result = scrub_url_path(source);
    assert_eq!(expected, result);
}

-- h4

Features

-- list

- lowercases all letters

- replaces spaces with dashes

- replaces multiple spaces with a single dash


It doesn't deal with anything else (like question marks)
but I'm not using those. I'll setup for those in the
future by clear listing letters and numbers and 
removing everything else. 

Basic, but helpful.


-- note

You'll need to add the _regex__ create with:

-- code
-- cli

cargo add regex


-- categories
-- Rust
-- WebDev

-- metadata
-- date: 2023-05-13 19:47:20
-- id: 2plb9z6w
-- site: aws
-- type: post
-- status: published