aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/timeline/apps.py
blob: 8f27297f3c925a2dd2d94dd3b083f9f1c93efc60 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from pathlib import Path

from django.apps import AppConfig
import frontmatter
import markdown

from pydis_site import settings


ENTRIES_PATH = Path(settings.BASE_DIR, "pydis_site", "apps", "timeline", "entries")


class TimelineConfig(AppConfig):
    """AppConfig instance for Timeline app."""

    name = 'pydis_site.apps.timeline'

    def ready(self) -> None:
        """Fetch all the timeline entries."""
        self.entries = []

        for path in ENTRIES_PATH.rglob("*.md"):
            metadata, content = frontmatter.parse(path.read_text(encoding="utf-8"))

            md = markdown.Markdown()
            html = str(md.convert(content))

            # Strip `.md` file extension from filename and split it into the
            # date (for sorting) and slug (for linking).
            key, slug = path.name[:-3].split("_")
            entry = {
                "key": key,
                "slug": slug,
                "title": metadata["title"],
                "date": metadata["date"],
                "icon": metadata["icon"],
                # This key might not be used if the icon uses the pydis logo.
                "icon_color": metadata.get("icon_color"),
                "content": html,
            }

            self.entries.append(entry)

        # Sort the entries in reverse-chronological order.
        self.entries.sort(key=lambda e: e['key'], reverse=True)