From efe784fe8a6c23248c6698aefab8bf54c5c85900 Mon Sep 17 00:00:00 2001 From: Hassan Abouelela Date: Tue, 23 Aug 2022 12:54:16 +0400 Subject: Support Hyperlinked Tag Group Replacement Signed-off-by: Hassan Abouelela --- pydis_site/apps/content/views/tags.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'pydis_site/apps/content/views') diff --git a/pydis_site/apps/content/views/tags.py b/pydis_site/apps/content/views/tags.py index a8df65db..4f4bb5a2 100644 --- a/pydis_site/apps/content/views/tags.py +++ b/pydis_site/apps/content/views/tags.py @@ -11,7 +11,12 @@ from django.views.generic import TemplateView from pydis_site.apps.content import utils from pydis_site.apps.content.models import Tag -COMMAND_REGEX = re.compile(r"`*!tags? (?P[\w\d-]+)`*") +# The following regex tries to parse a tag command +# It'll read up to two words seperated by spaces +# If the command does not include a group, the tag name will be in the `first` group +# If there's a second word after the command, or if there's a tag group, extra logic +# is necessary to determine whether it's a tag with a group, or a tag with text after it +COMMAND_REGEX = re.compile(r"`*!tags? (?P[\w-]+)(?P [\w-]+)?`*") class TagView(TemplateView): @@ -75,8 +80,23 @@ class TagView(TemplateView): # Check for tags which can be hyperlinked def sub(match: re.Match) -> str: - link = reverse("content:tag", kwargs={"location": match.group("name")}) - return f"[{match.group()}]({link})" + first, second = match.groups() + location = first + text, extra = match.group(), "" + + if second is not None: + # Possibly a tag group + try: + new_location = f"{first}/{second.strip()}" + utils.get_tag(new_location, skip_sync=True) + location = new_location + except Tag.DoesNotExist: + # Not a group, remove the second argument from the link + extra = text[text.find(second):] + text = text[:text.find(second)] + + link = reverse("content:tag", kwargs={"location": location}) + return f"[{text}]({link}){extra}" content = COMMAND_REGEX.sub(sub, content) # Add support for some embed elements -- cgit v1.2.3