forked from pudo/normality
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import string | ||
from typing import Any, Optional | ||
|
||
from normality.cleaning import collapse_spaces, category_replace | ||
from normality.constants import SLUG_CATEGORIES, WS | ||
from normality.transliteration import latinize_text | ||
from normality.stringify import stringify | ||
|
||
VALID_CHARS = string.ascii_lowercase + string.digits + WS | ||
|
||
|
||
def slugify(value: Any, sep: str = "-") -> Optional[str]: | ||
"""A simple slug generator. Slugs are pure ASCII lowercase strings | ||
that can be used in URLs an other places where a name has to be | ||
machine-safe.""" | ||
text = stringify(value) | ||
if text is None: | ||
return None | ||
text = text.replace(sep, WS) | ||
# run this first because it'll give better results on special | ||
# characters. | ||
text = category_replace(text, SLUG_CATEGORIES) | ||
text = latinize_text(text, ascii=True) | ||
text = text.lower() | ||
text = "".join([c for c in text if c in VALID_CHARS]) | ||
text = collapse_spaces(text) | ||
if text is None: | ||
return None | ||
return text.replace(WS, sep) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters