aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/timeline/apps.py
diff options
context:
space:
mode:
authorGravatar wookie184 <[email protected]>2024-07-18 22:30:15 +0200
committerGravatar GitHub <[email protected]>2024-07-18 22:30:15 +0200
commit9ac9ef90cc14e684fd40c534a36a430968d85e9b (patch)
treee859e884a287952210f2abd24562c956dd6375ec /pydis_site/apps/timeline/apps.py
parentMerge pull request #1365 from python-discord/more-frameworks (diff)
parentMerge branch 'main' into feat/timeline-from-yaml (diff)
Merge pull request #1293 from python-discord/feat/timeline-from-yaml
Markdown + YAML for the timeline
Diffstat (limited to 'pydis_site/apps/timeline/apps.py')
-rw-r--r--pydis_site/apps/timeline/apps.py50
1 files changed, 50 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..37a61e93
--- /dev/null
+++ b/pydis_site/apps/timeline/apps.py
@@ -0,0 +1,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)