aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/timeline/apps.py
blob: 37a61e937030ce2206c652cda569983e718c4df5 (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
46
47
48
49
50
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("_")

            icon_color = metadata.get("icon_color")
            # Use the pydis blurple as the default background color.
            if not icon_color or metadata["icon"] == "pydis":
                icon_color = "has-background-primary"

            entry = {
                "key": key,
                "slug": slug,
                "title": metadata["title"],
                "date": metadata["date"],
                "icon": metadata["icon"],
                "icon_color": 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)