Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated dashes to single dashes. Remove characters that aren't alphanumerics, underscores, or hyphens. Convert to lowercase. Also strip leading and trailing whitespace, dashes, and underscores.
(value, allow_unicode=False)
| 464 | |
| 465 | @keep_lazy_text |
| 466 | def slugify(value, allow_unicode=False): |
| 467 | """ |
| 468 | Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated |
| 469 | dashes to single dashes. Remove characters that aren't alphanumerics, |
| 470 | underscores, or hyphens. Convert to lowercase. Also strip leading and |
| 471 | trailing whitespace, dashes, and underscores. |
| 472 | """ |
| 473 | value = str(value) |
| 474 | if allow_unicode: |
| 475 | value = unicodedata.normalize("NFKC", value) |
| 476 | else: |
| 477 | value = ( |
| 478 | unicodedata.normalize("NFKD", value) |
| 479 | .encode("ascii", "ignore") |
| 480 | .decode("ascii") |
| 481 | ) |
| 482 | value = re.sub(r"[^\w\s-]", "", value.lower()) |
| 483 | return re.sub(r"[-\s]+", "-", value).strip("-_") |
| 484 | |
| 485 | |
| 486 | def camel_case_to_spaces(value): |