1 unstable release

Uses old Rust 2015

0.1.0 Jul 26, 2017

#590 in Text processing

Download history 2334/week @ 2024-01-11 2042/week @ 2024-01-18 2762/week @ 2024-01-25 2453/week @ 2024-02-01 3249/week @ 2024-02-08 2918/week @ 2024-02-15 2329/week @ 2024-02-22 3734/week @ 2024-02-29 4282/week @ 2024-03-07 3666/week @ 2024-03-14 3066/week @ 2024-03-21 2896/week @ 2024-03-28 3327/week @ 2024-04-04 3380/week @ 2024-04-11 3792/week @ 2024-04-18 2234/week @ 2024-04-25

13,421 downloads per month
Used in 40 crates (18 directly)

MIT license

14KB
209 lines

Slugify

Build Status Build status Crates.io docs.rs License: MIT

A utility macro for flexible slug genereation that handles unicode.

The slugify! macro implements a flexible slug generator, allowing for stop words, custom separator and maximum length options. The macro provides both a simple call format that has sane default parameters but also allows there default parameters to be overriden when needed.

Features:

  • Unicode strings support (phonetic conversion).
  • Support for custom slug separator.
  • Stop words filtering.
  • Slug maximum length support.

Usage

This crate is on crates.io and can be used by adding slugify to the dependencies in your project's Cargo.toml

[dependencies]
slugify = "0.1.0"

and this to your crate root:

#[macro_use] extern  crate  slugify;
use slugify::slugify;

Examples

Basic slug generation

assert_eq!(slugify!("hello world"), "hello-world");

Using a custom separator

assert_eq!(slugify!("hello world", separator = "."), "hello.world");
assert_eq!(slugify!("hello world", separator = " "), "hello world");

Stop words filtering

assert_eq!(slugify!("the quick brown fox jumps over the lazy dog", stop_words = "the,fox"), "quick-brown-jumps-over-lazy-dog");

Maximum slug length

assert_eq!(slugify!("the hello world", stop_words = "the", max_length = 5), "hello");

Phonetic Conversion and accented text

assert_eq!(slugify!("影師嗎"), "ying-shi-ma");
assert_eq!(slugify!("Æúű--cool?"), "aeuu-cool");
assert_eq!(slugify!("Nín hǎo. Wǒ shì zhōng guó rén"), "nin-hao-wo-shi-zhong-guo-ren");

Passing multiple optional parameters.

NOTE: the order of optional parameters matters: stop_words, separator and then max_length. All of them are optional, however when specifying more than one optional parameter, this order must be adhered.

assert_eq!(slugify!("the hello world", stop_words = "the", separator = "-"), "hello-world");
assert_eq!(slugify!("the hello world", separator = ".", max_length = 10), "the.hello");
assert_eq!(slugify!("the hello world", stop_words = "the", max_length = 5), "hello");
assert_eq!(slugify!("the hello world", stop_words = "the", separator = "-", max_length = 20), "hello-world");

Dependencies

~2MB
~66K SLoC