aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/content
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2022-08-13 06:10:38 +0200
committerGravatar Hassan Abouelela <[email protected]>2022-08-13 06:10:38 +0200
commit42124deb7ea5f17bc6faf959baba8e951b567655 (patch)
treed485bbeecc278cff5fa24eba566d5a95a2b5cff0 /pydis_site/apps/content
parentAdd Tags To Content Listings (diff)
Add Tag Page Template
Add a template for the tag page itself, and add a route to use it. Signed-off-by: Hassan Abouelela <[email protected]>
Diffstat (limited to 'pydis_site/apps/content')
-rw-r--r--pydis_site/apps/content/urls.py21
-rw-r--r--pydis_site/apps/content/views/__init__.py3
-rw-r--r--pydis_site/apps/content/views/tags.py56
3 files changed, 76 insertions, 4 deletions
diff --git a/pydis_site/apps/content/urls.py b/pydis_site/apps/content/urls.py
index f8496095..b4ffc07d 100644
--- a/pydis_site/apps/content/urls.py
+++ b/pydis_site/apps/content/urls.py
@@ -3,7 +3,7 @@ from pathlib import Path
from django_distill import distill_path
-from . import views
+from . import utils, views
app_name = "content"
@@ -29,18 +29,33 @@ def __get_all_files(root: Path, folder: typing.Optional[Path] = None) -> list[st
return results
-def get_all_pages() -> typing.Iterator[dict[str, str]]:
+DISTILL_RETURN = typing.Iterator[dict[str, str]]
+
+
+def get_all_pages() -> DISTILL_RETURN:
"""Yield a dict of all page categories."""
for location in __get_all_files(Path("pydis_site", "apps", "content", "resources")):
yield {"location": location}
+def get_all_tags() -> DISTILL_RETURN:
+ """Return all tag names in the repository in static builds."""
+ for tag in utils.get_tags_static():
+ yield {"name": tag.name}
+
+
urlpatterns = [
distill_path("", views.PageOrCategoryView.as_view(), name='pages'),
distill_path(
+ "tags/<str:name>/",
+ views.TagView.as_view(),
+ name="tag",
+ distill_func=get_all_tags
+ ),
+ distill_path(
"<path:location>/",
views.PageOrCategoryView.as_view(),
name='page_category',
distill_func=get_all_pages
- ),
+ )
]
diff --git a/pydis_site/apps/content/views/__init__.py b/pydis_site/apps/content/views/__init__.py
index 70ea1c7a..a969b1dc 100644
--- a/pydis_site/apps/content/views/__init__.py
+++ b/pydis_site/apps/content/views/__init__.py
@@ -1,3 +1,4 @@
from .page_category import PageOrCategoryView
+from .tags import TagView
-__all__ = ["PageOrCategoryView"]
+__all__ = ["PageOrCategoryView", "TagView"]
diff --git a/pydis_site/apps/content/views/tags.py b/pydis_site/apps/content/views/tags.py
new file mode 100644
index 00000000..12e311dc
--- /dev/null
+++ b/pydis_site/apps/content/views/tags.py
@@ -0,0 +1,56 @@
+import re
+
+import frontmatter
+import markdown
+from django.conf import settings
+from django.http import Http404
+from django.urls import reverse
+from django.views.generic import TemplateView
+
+from pydis_site.apps.content import utils
+from pydis_site.apps.content.models.tag import Tag
+
+COMMAND_REGEX = re.compile(r"`*!tags? (?P<name>[\w\d-]+)`*")
+
+
+class TagView(TemplateView):
+ """Handles tag pages."""
+
+ template_name = "content/tag.html"
+
+ def get_context_data(self, **kwargs) -> dict:
+ """Get the relevant context for this tag page."""
+ try:
+ tag = utils.get_tag(kwargs.get("name"))
+ except Tag.DoesNotExist:
+ raise Http404
+
+ context = super().get_context_data(**kwargs)
+ context["page_title"] = tag.name
+ body = frontmatter.parse(tag.body)
+ content = body[1]
+
+ # Check for tags which can be hyperlinked
+ start = 0
+ while match := COMMAND_REGEX.search(content, start):
+ link = reverse("content:tag", kwargs={"name": match.group("name")})
+ content = content[:match.start()] + f"[{match.group()}]({link})" + content[match.end():]
+ start = match.end()
+
+ # Add support for some embed elements
+ if embed := body[0].get("embed"):
+ context["page_title"] = embed["title"]
+ if image := embed.get("image"):
+ content = f"![{embed['title']}]({image['url']})\n\n" + content
+
+ context.update({
+ "page": markdown.markdown(content, extensions=["pymdownx.superfences"]),
+ "tag": tag,
+ })
+
+ context["breadcrumb_items"] = [{
+ "name": utils.get_category(settings.CONTENT_PAGES_PATH / location)["title"],
+ "path": str(location)
+ } for location in [".", "tags"]]
+
+ return context