diff options
| author | 2022-08-13 06:10:38 +0200 | |
|---|---|---|
| committer | 2022-08-13 06:10:38 +0200 | |
| commit | 42124deb7ea5f17bc6faf959baba8e951b567655 (patch) | |
| tree | d485bbeecc278cff5fa24eba566d5a95a2b5cff0 | |
| parent | Add 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]>
| -rw-r--r-- | pydis_site/apps/content/urls.py | 21 | ||||
| -rw-r--r-- | pydis_site/apps/content/views/__init__.py | 3 | ||||
| -rw-r--r-- | pydis_site/apps/content/views/tags.py | 56 | ||||
| -rw-r--r-- | pydis_site/static/css/content/tag.css | 7 | ||||
| -rw-r--r-- | pydis_site/templates/content/base.html | 2 | ||||
| -rw-r--r-- | pydis_site/templates/content/tag.html | 21 | 
6 files changed, 105 insertions, 5 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 diff --git a/pydis_site/static/css/content/tag.css b/pydis_site/static/css/content/tag.css new file mode 100644 index 00000000..a144ce24 --- /dev/null +++ b/pydis_site/static/css/content/tag.css @@ -0,0 +1,7 @@ +h1.title a { +    color: black; +} + +h1.title a:hover { +    color: #7289DA; +} diff --git a/pydis_site/templates/content/base.html b/pydis_site/templates/content/base.html index 4a19a275..dbd303a1 100644 --- a/pydis_site/templates/content/base.html +++ b/pydis_site/templates/content/base.html @@ -35,7 +35,7 @@      <section class="section">          <div class="container">              <div class="content"> -                <h1 class="title">{{ page_title }}</h1> +                <h1 class="title">{% block title_element %}{{ page_title }}{% endblock %}</h1>                  {% block page_content %}{% endblock %}              </div>          </div> diff --git a/pydis_site/templates/content/tag.html b/pydis_site/templates/content/tag.html new file mode 100644 index 00000000..264f63d0 --- /dev/null +++ b/pydis_site/templates/content/tag.html @@ -0,0 +1,21 @@ +{% extends "content/page.html" %} +{% load static %} + +{% block head %} +    {{ block.super }} +    <link rel="stylesheet" href="{% static 'css/content/tag.css' %}"/> +    <title>{{ tag.name }}</title> +{% endblock %} + +{% block title_element %} +    <div class="level"> +        <div class="level-left">{{ block.super }}</div> +        <div class="level-right"> +            <a class="level-item fab fa-github" href="{{ tag.url }}"></a> +        </div> +    </div> +{% endblock %} + +{% block page_content %} +    {{ block.super }} +{% endblock %} | 
