forked from tip-of-the-week/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss_generator.py
83 lines (64 loc) · 2.48 KB
/
rss_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# coding: utf-8
import re
import sys
import glob
import pathlib
from datetime import datetime
import markdown
def sanitize(text: str) -> str:
return text.replace("<", "<").replace(">", ">")
def gen_feed(tips, url):
title_re = r"\* +(\*\*)?(?P<title>(?(1).+(?<!\*\*)|.+))(?(1)\*\*$|$)"
link_re = r"(?P<link>(?<!\[|\()https?://[\w\./\?&=\-\+#:,;%]+)"
xml = ""
for tip in tips:
link = tip["short_path"]
tip_num = link.replace(".md", "")
date = datetime.fromtimestamp(tip["pub_date"]).strftime("%a, %d %b %Y %H:%M:%S GMT")
title = re.search(title_re, tip["content"], re.RegexFlag.MULTILINE).group("title")
title = sanitize(title)
content = tip["content"]
updated = content
links_seen = set()
for match in re.finditer(link_re, content):
naked_link = match.group("link")
if naked_link not in links_seen:
updated = updated.replace(naked_link, f"[{naked_link}]({naked_link})")
links_seen.add(naked_link)
# TODO: find a way to colorize the code, the documentation advise to have custom css
# The classes should already have been applied to the code thanks to extension 'codehilite'
md = markdown.markdown(
f"<div markdown=1>{updated}</div>",
extensions=["extra", "codehilite", "nl2br"],
output_format="xhtml"
)
md = md.replace("<p></p>", "")
md = md.replace("<details open>", "<details open=true>")
md = "<![CDATA[ %s]]>" % md
xml += f"""<item>
<title>{tip_num} - {title}</title>
<link>{url}{link}</link>
<guid>{url}{link}</guid>
<pubDate>{date}</pubDate>
<description>{md}</description>
</item>\n"""
return xml
def main():
tips = []
files = sorted(glob.glob("tips/*.md"), reverse=True)
for file in files:
path = pathlib.Path(file)
tips.append({
"content": path.read_text(encoding="utf-8"),
"short_path": path.name,
"pub_date": path.lstat().st_ctime,
})
xml = gen_feed(tips, "https://github.com/tip-of-the-week/cpp/blob/master/tips/")
template = pathlib.Path("feed.template.xml")
new_content = template.read_text().replace("<!-- START FEED -->", xml)
pathlib.Path("feed.xml").write_text(new_content)
print("Done.")
return 0
if __name__ == "__main__":
sys.exit(main())