diff options
-rw-r--r-- | pydis_site/apps/resources/templatetags/__init__.py | 3 | ||||
-rw-r--r-- | pydis_site/apps/resources/templatetags/as_icon.py | 14 | ||||
-rw-r--r-- | pydis_site/apps/resources/tests/test_as_icon.py | 28 |
3 files changed, 45 insertions, 0 deletions
diff --git a/pydis_site/apps/resources/templatetags/__init__.py b/pydis_site/apps/resources/templatetags/__init__.py new file mode 100644 index 00000000..2b266b94 --- /dev/null +++ b/pydis_site/apps/resources/templatetags/__init__.py @@ -0,0 +1,3 @@ +from .as_icon import as_icon + +__all__ = ["as_icon"] diff --git a/pydis_site/apps/resources/templatetags/as_icon.py b/pydis_site/apps/resources/templatetags/as_icon.py new file mode 100644 index 00000000..b211407c --- /dev/null +++ b/pydis_site/apps/resources/templatetags/as_icon.py @@ -0,0 +1,14 @@ +from django import template + +register = template.Library() + + +def as_icon(icon: str) -> str: + """Convert icon string in format 'type/icon' to fa-icon HTML classes.""" + icon_type, icon_name = icon.split("/") + if icon_type.lower() == "branding": + icon_type = "fab" + else: + icon_type = "fas" + return f'{icon_type} fa-{icon_name}' diff --git a/pydis_site/apps/resources/tests/test_as_icon.py b/pydis_site/apps/resources/tests/test_as_icon.py new file mode 100644 index 00000000..5b33910d --- /dev/null +++ b/pydis_site/apps/resources/tests/test_as_icon.py @@ -0,0 +1,28 @@ +from django.test import TestCase + +from pydis_site.apps.resources.templatetags import as_icon + + +class TestAsIcon(TestCase): + """Tests for `as_icon` templatetag.""" + + def test_as_icon(self): + """Should return proper icon type class and icon class based on input.""" + test_cases = [ + { + "input": "regular/icon", + "output": "fas fa-icon", + }, + { + "input": "branding/brand", + "output": "fab fa-brand", + }, + { + "input": "fake/my-icon", + "output": "fas fa-my-icon", + } + ] + + for case in test_cases: + with self.subTest(input=case["input"], output=case["output"]): + self.assertEqual(case["output"], as_icon(case["input"])) |