2024-03-31 10:55:42 +00:00
|
|
|
#!/usr/bin/env python3
|
2023-05-22 13:29:20 +00:00
|
|
|
|
|
|
|
from sys import argv, exit
|
|
|
|
from os import walk, path
|
|
|
|
from random import choice, seed
|
|
|
|
from textwrap import indent
|
|
|
|
import time, urllib.parse, re, subprocess
|
|
|
|
|
|
|
|
from article_utils import parse_metadata
|
|
|
|
from rfc822 import stringify_date
|
|
|
|
|
2024-03-31 05:38:30 +00:00
|
|
|
import config
|
|
|
|
|
2023-05-22 13:29:20 +00:00
|
|
|
if len(argv) <= 1:
|
|
|
|
print("No directory was supplied")
|
|
|
|
exit(-1)
|
|
|
|
|
|
|
|
seed()
|
|
|
|
|
|
|
|
# todo: Find the latest pubDate
|
|
|
|
feed = f"""<rss version="2.0">
|
|
|
|
<channel>
|
2024-03-31 05:38:30 +00:00
|
|
|
<title>{config.title}</title>
|
2024-03-31 11:24:01 +00:00
|
|
|
<link>{config.address}</link>
|
2024-03-31 05:38:30 +00:00
|
|
|
<description>{config.description() if callable(config.description) else config.description}</description>
|
|
|
|
<language>{config.language}</language>
|
2023-05-22 13:29:20 +00:00
|
|
|
<lastBuildDate>{stringify_date(time.gmtime(int(time.time())))}</lastBuildDate>
|
|
|
|
"""
|
|
|
|
|
|
|
|
body_taker = re.compile(r"<body>(.*)</body>", re.DOTALL)
|
|
|
|
|
|
|
|
for root, dirs, _ in walk(argv[1]):
|
|
|
|
for d in dirs:
|
|
|
|
metadata = parse_metadata(path.abspath(root + '/' + d + "/page.mmd"))
|
|
|
|
feed += (
|
|
|
|
" <item>\n"
|
|
|
|
f""" <title>{metadata.get("Title", "No title!? ;-;")}</title>\n"""
|
|
|
|
)
|
|
|
|
body = subprocess.getoutput(f"./tools/mmd/build/multimarkdown {root}/{d}/page.mmd")
|
|
|
|
feed += (
|
|
|
|
" <description>\n"
|
|
|
|
" <![CDATA[\n"
|
|
|
|
f"{indent(body_taker.search(body)[1], ' ' * 12)}\n"
|
|
|
|
" ]]>\n"
|
|
|
|
" </description>\n"
|
|
|
|
)
|
|
|
|
if "Date" in metadata:
|
|
|
|
feed += \
|
|
|
|
f""" <pubDate>{stringify_date(metadata["Date"])}</pubDate>\n"""
|
|
|
|
feed += (
|
|
|
|
f""" <guid>/articles/{d}</guid>\n"""
|
2024-03-31 11:24:01 +00:00
|
|
|
f""" <link>{config.address}/articles/{urllib.parse.quote(d)}</link>\n"""
|
2023-05-22 13:29:20 +00:00
|
|
|
" </item>\n"
|
|
|
|
)
|
2023-05-25 14:06:05 +00:00
|
|
|
break
|
2023-05-22 13:29:20 +00:00
|
|
|
|
|
|
|
feed += """ </channel>
|
|
|
|
</rss>"""
|
|
|
|
|
|
|
|
print(feed)
|