-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Newsletter/mailing list archives (#1904)
* Upgrade MkDocs * Install feedparser * Implement a hook for rendering RSS feeds * Ignore pycache * Only fetch archives when built in production * Fix typo in placeholder text * Add newsletter and mailing list archives * Temporarily enable newsletters in preview Integration tests should fail due to the FIXME * Introduce a FIXME to fail tests * Remove FIXME
- Loading branch information
1 parent
c80a978
commit 08f5e37
Showing
18 changed files
with
203 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ site/ | |
*.log | ||
.DS_Store | ||
.idea/ | ||
__pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This page will be generated during deployment by | ||
the `hooks/archives.py` hook. If this message is visible | ||
after deployment, something has gone wrong. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# AUTOMATICALLY GENERATED PAGE | ||
|
||
This page will be generated during deployment by | ||
the `scripts/generate_alpha.sh` script. If this message if visible | ||
the `scripts/generate_alpha.sh` script. If this message is visible | ||
after deployment, something has gone wrong. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
archive_feed: https://newsletter.csc.fi/archive/asiakaskoulutus-customer-training/rss | ||
--- | ||
|
||
# Newsletter | ||
|
||
--8<-- "placeholders/archive.md" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
archive_feed: https://postit.csc.fi/sympa/rss/latest_arc/lumi-users?for=365 | ||
--- | ||
|
||
# Mailing list | ||
|
||
--8<-- "placeholders/archive.md" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
archive_feed: https://postit.csc.fi/sympa/rss/latest_arc/mahti-users?for=365 | ||
--- | ||
|
||
# Mailing list | ||
|
||
--8<-- "placeholders/archive.md" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
archive_feed: https://postit.csc.fi/sympa/rss/latest_arc/pouta-users?for=365 | ||
--- | ||
|
||
# Mailing list | ||
|
||
--8<-- "placeholders/archive.md" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
archive_feed: https://postit.csc.fi/sympa/rss/latest_arc/puhti-users?for=365 | ||
--- | ||
|
||
# Mailing list | ||
|
||
--8<-- "placeholders/archive.md" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
archive_feed: https://postit.csc.fi/sympa/rss/latest_arc/rahti-users?for=365 | ||
--- | ||
|
||
# Mailing list | ||
|
||
--8<-- "placeholders/archive.md" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
archive_feed: https://newsletter.csc.fi/archive/tutkimus-research/rss | ||
--- | ||
|
||
# Newsletter | ||
|
||
--8<-- "placeholders/archive.md" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# AUTOMATICALLY GENERATED PAGE | ||
|
||
This page will be generated during deployment by | ||
the `scripts/generate_glossary.sh` script. If this message if visible | ||
the `scripts/generate_glossary.sh` script. If this message is visible | ||
after deployment, something has gone wrong. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
from http import HTTPStatus | ||
from datetime import date | ||
from logging import getLogger | ||
|
||
import feedparser | ||
|
||
|
||
PRODUCTION_ENVIRONMENT = "production" | ||
ENVIRONMENT_KEY = "environment" | ||
URL_KEY = "archive_feed" | ||
|
||
|
||
class ArchiveItem: | ||
def __init__(self, entry): | ||
self.entry = entry | ||
|
||
def __str__(self): | ||
return self.markdown | ||
|
||
@property | ||
def date(self): | ||
return self.to_date_string(self.entry.published_parsed) | ||
|
||
@property | ||
def href(self): | ||
return self.to_https(self.entry.link) | ||
|
||
@property | ||
def markdown(self): | ||
heading = f"### {self.date}" | ||
link = f"[{self.entry.title}]({self.href}){{ target=_blank }}" | ||
return f"{heading}\n{link}\n" | ||
|
||
@staticmethod | ||
def to_date_string(struct_time): | ||
return "{0:%B} {0:%d}, {0:%Y}".format( | ||
date(*[getattr(struct_time, attr) | ||
for attr | ||
in ("tm_year", "tm_mon", "tm_mday")])) | ||
|
||
@staticmethod | ||
def to_https(href): | ||
if not href.startswith("http"): | ||
raise ValueError(f"Protocol must be HTTP(S): '{href}'") | ||
return (href.replace("http:", "https:", 1) | ||
if href.startswith("http:") | ||
else href) | ||
|
||
|
||
class RSSArchive: | ||
def __init__(self, archive_url): | ||
self.archive = feedparser.parse(archive_url, sanitize_html=False) | ||
|
||
@property | ||
def status(self): | ||
return self.archive.get("status", None) | ||
|
||
@property | ||
def title(self): | ||
return self.archive.feed.title | ||
|
||
@property | ||
def entries(self): | ||
return self.archive.entries | ||
|
||
@property | ||
def items(self): | ||
return map(lambda e: str(ArchiveItem(e)), self.entries) | ||
|
||
@property | ||
def markdown(self): | ||
title = self.title | ||
content = "".join(self.items) | ||
return "## {}\n\n{}".format(title, content) | ||
|
||
|
||
class ArchiveHook: | ||
def __init__(self, logger): | ||
self.logger = logger | ||
self.environment = None | ||
|
||
def __log_status(self, feed): | ||
msg = lambda feed, status: f"Archive '{feed.title}': {status.value} {status.phrase}" | ||
if feed.status is not None: | ||
status = HTTPStatus(feed.status) | ||
message = msg(feed, status) | ||
if 100 <= status.value <= 299: | ||
self.logger.info(message) | ||
elif 300 <= status.value <= 399: | ||
self.logger.warning(message) | ||
elif status.value >= 400: | ||
self.logger.error(message) | ||
|
||
def __get_heading(self, markdown): | ||
return markdown.split("\n")[0] + "\n" | ||
|
||
def on_config(self, config): | ||
self.environment = config.extra[ENVIRONMENT_KEY] | ||
|
||
def on_page_markdown(self, markdown, page, config, files): | ||
if self.environment == PRODUCTION_ENVIRONMENT: | ||
try: | ||
archive_url = page.meta[URL_KEY] | ||
except KeyError: | ||
return None | ||
else: | ||
self.logger.info(f"Fetching archive from {archive_url}...") | ||
archive = RSSArchive(archive_url) | ||
self.__log_status(archive) | ||
page.meta["title"] = archive.title | ||
return self.__get_heading(markdown) + archive.markdown | ||
else: | ||
return None | ||
|
||
|
||
hook = ArchiveHook(getLogger("mkdocs")) | ||
on_config = hook.on_config | ||
on_page_markdown = hook.on_page_markdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters