Crate sluggify

Source
Expand description

§sluggify, Simple slug or clean url generator for Rust.

This library is a simple slug generator or url cleaner for Rust. With default settings, you will get an hyphenized, lowercase, alphanumeric version of any string you please, with any diacritics removed, whitespace and dashes collapsed, and whitespace trimmed.

For getting started with using sluggify, see the Getting started section below. For navigating the documentation of the available modules, see the Modules section below. If you want to contribute to sluggify, see the Contribute section in the repo.

§Getting started

We explain how to use sluggify step-by-step. Users who already have experience with Rust can skip right to Step 3: Use sluggify in your project. Users who already know sluggify might want to jump right into the modules docs

§Step 1: Setting up Rust

Rust can be installed following the instruction for rustup.

§Step 2: Setting up a new Rust project

Since sluggify is a library, you need to setup your own new Rust project to use sluggify. With Rust, projects and their dependencies are managed with the builtin package manager Cargo. To create a new Rust project, issue

cargo new hello_world --bin
cd hello_world

in your terminal. The flag --bin tells Cargo to create an executable project instead of a library. In this section of the Rust docs, you find details about what Cargo just created for you.

Your new project can be compiled with

cargo build

If dependencies in your project are out of date, update with

cargo update

Execute the compiled code with

cargo run

If you are new to Rust, we suggest to proceed with learning Rust via the Rust docs.

§Step 3: Use sluggify in your project

To use sluggify in your Rust project, add the following to your Cargo.toml

[dependencies]
sluggify = "0.1.0"

and import the crate from your source code:

use sluggify::sluggify:: {SluggifyOptions, sluggify};
fn main() {    
   let raw_string = r#"Welcome to the party fellas!"#;

   let cleaned_url = sluggify(raw_string, None); 

   println!("Sluggified version of raw string '{}' is '{}'", raw_string, cleaned_url);
   //Returns Sluggified version of raw string 'Welcome to the party fellas!' is 'welcome-to-the-party-fellas'

}

§You can decide to have whitespaces not trimmed or collapsed

use sluggify::sluggify:: {SluggifyOptions, sluggify};
fn main() {  
let original = "a #$b    ** c   \t  \n  d";
let expected = "a---b-------c---------d";

let options = SluggifyOptions::new().set_collapse_whitespace(false);;

assert_eq!(sluggify(original, Some(options)), expected);

}

§OR you can choose to have more control by configuring how the sluggifying happens.

use sluggify::sluggify:: {SluggifyOptions, sluggify};
fn main() {  
let original = "a #$b    ** c   \t  \n  d";
let expected = "a-b-c-d";
let bad_chars = vec!['*','$', '\t'];

let options = SluggifyOptions::new().set_collapse_whitespace(true).set_disallowed_characters(bad_chars);

assert_eq!(sluggify(original, Some(options)), expected);

}

Modules§

sluggify