diff options
Diffstat (limited to 'pydis_site/apps/timeline/apps.py')
-rw-r--r-- | pydis_site/apps/timeline/apps.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/pydis_site/apps/timeline/apps.py b/pydis_site/apps/timeline/apps.py new file mode 100644 index 00000000..8f27297f --- /dev/null +++ b/pydis_site/apps/timeline/apps.py @@ -0,0 +1,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) |