Easily create slugs from any string. Supports many languages alphabets. See Supported languages to check for supported languages or to help to add one.
Inspired by Lisp Web Tales.
* (ql:quickload :cl-slug) ; => (:CL-SLUG) * (import '(slug:slugify slug:asciify slug:CamelCaseFy)) ; => T
The main function is called
* (slugify "My new cool article, for the blog (V. 2).") ; => "my-new-cool-article-for-the-blog-v-2" * (slugify "André Miranda") ; => "andre-miranda"
slugify removes any accented character, replacing it with an unaccented equivalent, and any punctuation (a punctuation is a char that returns
alphanumericp) and puts a dash (
-) on it's place. You can change that by binding (of
* (let ((*slug-separator* #\_)) (slugify "Testing the *slug-separator* var...")) ; => "testing_the_slug_separator_var"
slugify also ignores numbers:
* (slugify "one2three4five") ; => "one2three4five"
If you just want to remove accents and punctuation of a given string, use
* (asciify "Eu André!") ; => "Eu Andre!"
Or if you want a CamelCase, use
* (CamelCaseFy "My new camel case string") ; => "MyNewCamelCaseString"
Available languages are stored in
* slug:*available-languages* ((:TR . "Türkçe (Turkish)") (:SV . "Svenska (Swedish)") (:FI . "Suomi (Finnish)") (:UK . "українська (Ukrainian)") (:RU . "Ру́сский (Russian)") (:RO . "Română (Romanian)") (:RM . "Rumàntsch (Romansh)") (:PT . "Português (Portuguese)") (:PL . "Polski (Polish)") (:NO . "Norsk (Norwegian)") (:LT . "Lietuvių (Lithuanian)") (:LV . "Latviešu (Latvian)") (:LA . "Lingua Latīna (Latin)") (:IT . "Italiano (Italian)") (:EL . "ελληνικά (Greek)") (:FR . "Français (French)") (:EO . "Esperanto") (:ES . "Español (Spanish)") (:EN . "English") (:DE . "Deutsch (German)") (:DA . "Dansk (Danish)") (:CS . "Čeština (Czech)") (:CURRENCY . "Currency"))
Available on Quicklisp:
The languages that are supported right now are:
Ported from Django():
At the present moment, adding new languages is a fairly manual process:
- Identify non-ASCII characters in a given language's alphabet
- Establish equivalence between the found characters and ASCII characters
- Write them down in the code.
All those things can actually be done for most of the dominant Western languages, but can't be applied for minor regional languages or many other non-Latin languages from the whole world, like Chinese. It's not generic and not scalable.
I couldn't think of a solution so far for this, but if you know a solution (even a partial one) I'd be glad to hear =].
If you find any bug or inconsistency in the code, or if you find it too hard to use, please, feel free to open an issue.
To run all the defined tests, use:
* (asdf:test-system :cl-slug) ; prints lots of (colorful) stuff... ; => T