From e9980b1a760c4afd617849663cda19fc69b40f65 Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Sun, 23 May 2021 16:54:34 -0600 Subject: add rss support --- src/pyssg/rss.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/pyssg/rss.py (limited to 'src/pyssg/rss.py') diff --git a/src/pyssg/rss.py b/src/pyssg/rss.py new file mode 100644 index 0000000..07776e1 --- /dev/null +++ b/src/pyssg/rss.py @@ -0,0 +1,51 @@ +import os +import importlib.metadata +from datetime import datetime, timezone + +from .page import Page +from .configuration import Configuration + + +VERSION = importlib.metadata.version('pyssg') +DFORMAT = '%a, %d %b %Y %H:%M:%S %Z' + + +class RSSBuilder: + def __init__(self, config: Configuration, + template: str, + pages: list[Page]): + self.rss: str = template + self.pages: list[Page] = pages + + + def build(self): + # initial base replacements + self.rss = self.rss.replace('$$TITLE', config.title) + self.rss = self.rss.replace('$$LINK', config.base_url) + self.rss = self.rss.replace('$$PYSSGVERSION', VERSION) + items_formatted: str = __get_items_formatted() + self.rss = self.rss.replace('$$ITEMS', items_formatted) + + current_date: str = datetime.now(tz=timezone.utc).strftime(DFORMAT) + self.rss = self.rss.replace('$$CURRENTDATE', current_date) + + with open(os.path.join(config.dst, 'rss.xml'), 'w') as f: + f.write(self.rss) + + + def __get_items_formatted(self) -> str: + # i_f=items formatted for short + i_f: str = '' + for p in pages: + url: str = f'{config.base_url}/{p.name.replace(".md", ".html")}' + date: str = p.c_datetime.strftime(DFORMAT) + + i_f = f'{i_f} \n' + i_f = f'{i_f} {p.title}\n' + i_f = f'{i_f} {url}\n' + i_f = f'{i_f} {p.summary}\n' + i_f = f'{i_f} {url}\n' + i_f = f'{i_f} {date}\n' + i_f = f'{i_f} \n' + + return i_f -- cgit v1.2.3-54-g00ecf