[formatter] add 'g' conversion to sluGify a string (#2410)

This commit is contained in:
Mike Fährmann
2022-08-23 21:14:55 +02:00
parent e4cff67aaa
commit 67bad04dda
5 changed files with 36 additions and 1 deletions

View File

@@ -381,6 +381,7 @@ _CONVERSIONS = {
"T": util.datetime_to_timestamp_string,
"d": text.parse_timestamp,
"U": text.unescape,
"g": text.slugify,
"S": util.to_string,
"s": str,
"r": repr,

View File

@@ -39,6 +39,16 @@ def split_html(txt):
return []
def slugify(value):
"""Convert a string to a URL slug
Adapted from:
https://github.com/django/django/blob/master/django/utils/text.py
"""
value = re.sub(r"[^\w\s-]", "", str(value).lower())
return re.sub(r"[-\s]+", "-", value).strip("-_")
def ensure_http_scheme(url, scheme="https://"):
"""Prepend 'scheme' to 'url' if it doesn't have one"""
if url and not url.startswith(("https://", "http://")):