Skip to content

Commit

Permalink
Add "colour.utilities.slugify" definition.
Browse files Browse the repository at this point in the history
  • Loading branch information
KelSolaar committed Jun 1, 2022
1 parent d4f6012 commit 9bc7f13
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions colour/utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
copy_definition,
validate_method,
optional,
slugify,
)
from .verbose import (
ColourWarning,
Expand Down Expand Up @@ -164,6 +165,7 @@
"copy_definition",
"validate_method",
"optional",
"slugify",
]
__all__ += [
"ColourWarning",
Expand Down
53 changes: 53 additions & 0 deletions colour/utilities/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +23,7 @@
import numpy as np
import re
import subprocess # nosec
import unicodedata
import types
import warnings
from contextlib import contextmanager
Expand Down Expand Up @@ -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("-_")
27 changes: 27 additions & 0 deletions colour/utilities/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
first_item,
validate_method,
optional,
slugify,
)

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


Expand Down Expand Up @@ -510,5 +512,30 @@ 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",
)

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
Expand Up @@ -63,6 +63,7 @@ Common
copy_definition
validate_method
optional
slugify

Array
-----
Expand Down

0 comments on commit 9bc7f13

Please sign in to comment.