Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "colour.utilities.slugify" definition.
Browse files Browse the repository at this point in the history
KelSolaar committed Jun 11, 2022

Unverified

This user has not yet uploaded their public signing key.
1 parent 09413da commit 0d175fb
Showing 5 changed files with 102 additions and 0 deletions.
5 changes: 5 additions & 0 deletions BIBLIOGRAPHY.bib
Original file line number Diff line number Diff line change
@@ -948,6 +948,11 @@ @misc{DigitalCinemaInitiatives2007b
author = {{Digital Cinema Initiatives}},
year = 2007,
}
@misc{DjangoSoftwareFoundation2022,
title = {Slugify},
author = {{Django Software Foundation}},
year = 2022,
}
@misc{Dolby2016a,
title = {{{WHAT IS ICTCP}}? - {{INTRODUCTION}}},
author = {{Dolby}},
2 changes: 2 additions & 0 deletions colour/utilities/__init__.py
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@
copy_definition,
validate_method,
optional,
slugify,
)
from .verbose import (
ColourWarning,
@@ -164,6 +165,7 @@
"copy_definition",
"validate_method",
"optional",
"slugify",
]
__all__ += [
"ColourWarning",
53 changes: 53 additions & 0 deletions colour/utilities/common.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,9 @@
References
----------
- :cite:`DjangoSoftwareFoundation2022` : Django Software Foundation. (2022).
slugify. Retrieved June 1, 2022, from https://github.com/django/django/\
blob/0dd29209091280ccf34e07c9468746c396b7778e/django/utils/text.py#L400
- :cite:`Kienzle2011a` : Kienzle, P., Patel, N., & Krycka, J. (2011).
refl1d.numpyerrors - Refl1D v0.6.19 documentation. Retrieved January 30,
2015, from
@@ -20,6 +23,7 @@
import numpy as np
import re
import subprocess # nosec
import unicodedata
import types
import warnings
from contextlib import contextmanager
@@ -1380,3 +1384,52 @@ def optional(value: Optional[T], default: T) -> T:
return default
else:
return value


def slugify(object_: Any, allow_unicode: Boolean = False) -> str:
"""
Generate a *SEO* friendly and human-readable slug from given object.
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.
Parameters
----------
object_
Object to convert to a slug.
allow_unicode
Whether to allow unicode characters in the generated slug.
Returns
-------
:class:`str`
Generated slug.
References
----------
:cite:`DjangoSoftwareFoundation2022`
Examples
--------
>>> slugify(
... " Jack & Jill like numbers 1,2,3 and 4 and silly characters ?%.$!/"
... )
'jack-jill-like-numbers-123-and-4-and-silly-characters'
"""

value = str(object_)

if allow_unicode:
value = unicodedata.normalize("NFKC", value)
else:
value = (
unicodedata.normalize("NFKD", value)
.encode("ascii", "ignore")
.decode("ascii")
)

value = re.sub(r"[^\w\s-]", "", value.lower())

return re.sub(r"[-\s]+", "-", value).strip("-_")
41 changes: 41 additions & 0 deletions colour/utilities/tests/test_common.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@

import numpy as np
import unittest
import unicodedata
from functools import partial

from colour.hints import Any, Floating, Number, Tuple
@@ -23,6 +24,7 @@
first_item,
validate_method,
optional,
slugify,
)

__author__ = "Colour Developers"
@@ -47,6 +49,7 @@
"TestFirstItem",
"TestValidateMethod",
"TestOptional",
"TestSlugify",
]


@@ -510,5 +513,43 @@ def test_optional(self):
self.assertEqual(optional(None, "Bar"), "Bar")


class TestSlugify(unittest.TestCase):
"""
Define :func:`colour.utilities.common.slugify` definition unit tests
methods.
"""

def test_slugify(self):
"""Test :func:`colour.utilities.common.optional` definition."""

self.assertEqual(
slugify(
" Jack & Jill like numbers 1,2,3 and 4 and "
"silly characters ?%.$!/"
),
"jack-jill-like-numbers-123-and-4-and-silly-characters",
)

self.assertEqual(
slugify("Un \xe9l\xe9phant \xe0 l'or\xe9e du bois"),
"un-elephant-a-loree-du-bois",
)

# NOTE: Our "utilities/unicode_to_ascii.py" utility script normalises
# the reference string.
self.assertEqual(
unicodedata.normalize(
"NFD",
slugify(
"Un \xe9l\xe9phant \xe0 l'or\xe9e du bois",
allow_unicode=True,
),
),
"un-éléphant-à-lorée-du-bois",
)

self.assertEqual(slugify(123), "123")


if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions docs/colour.utilities.rst
Original file line number Diff line number Diff line change
@@ -63,6 +63,7 @@ Common
copy_definition
validate_method
optional
slugify

Array
-----

0 comments on commit 0d175fb

Please sign in to comment.