From f6208bc08110259467d5b7cc66e2ed86901f0394 Mon Sep 17 00:00:00 2001 From: vanten-s Date: Thu, 3 Aug 2023 22:15:46 +0200 Subject: [PATCH] Works I think --- .gitignore | 1 + __main__.py | 37 +++++++++++++++++++++++++++++++++++++ base_feed.xml | 11 +++++++++++ base_item.xml | 3 +++ main.py | 0 5 files changed, 52 insertions(+) create mode 100644 __main__.py create mode 100644 base_feed.xml create mode 100644 base_item.xml delete mode 100644 main.py diff --git a/.gitignore b/.gitignore index 91e58c7..7f5c018 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ venv/ testdata/ +__pycache__/ diff --git a/__main__.py b/__main__.py new file mode 100644 index 0000000..d31c8ce --- /dev/null +++ b/__main__.py @@ -0,0 +1,37 @@ +import markdown +import pathlib +import os + +current_dir = pathlib.Path(__file__).parent + +with (current_dir / "base_feed.xml").open("r") as f: + base_feed = f.read() + +with (current_dir / "base_item.xml").open("r") as f: + base_item = f.read() + +def convert_case(string: str): + while string.find("_") != -1: + string = string.replace("_" + string[string.index("_") + 1], string[string.index("_") +1].upper()) + + return string + +def article_to_xml(article_path: pathlib.Path): + with (article_path / "article.md").open("r") as f: + markdown_data = f.read() + + markdown_instance = markdown.Markdown(extensions=['meta', 'nl2br']) + html = markdown_instance.convert(markdown_data) + metadata = markdown_instance.Meta + tags = [f"<{convert_case(key)}>{metadata[key][0]}" for key in metadata.keys()] + tags.append(f"\n{html}\n") + return base_item.format(tags="\n".join(tags)) + +def make_rss_feed(path, channel_name, channel_link, description): + articles = "".join([article_to_xml(path) for path in (pathlib.Path.cwd() / path).glob("*")]) + return base_feed \ + .replace("{title}", channel_name) \ + .replace("{link}", channel_link) \ + .replace("{description}", description) \ + .replace("{items}", articles) + diff --git a/base_feed.xml b/base_feed.xml new file mode 100644 index 0000000..22ab714 --- /dev/null +++ b/base_feed.xml @@ -0,0 +1,11 @@ + + + + +{title} +{link} +{description} +{items} + + + diff --git a/base_item.xml b/base_item.xml new file mode 100644 index 0000000..c16f303 --- /dev/null +++ b/base_item.xml @@ -0,0 +1,3 @@ + +{tags} + diff --git a/main.py b/main.py deleted file mode 100644 index e69de29..0000000