Skip to content

Commit 3f8d507

Browse files
author
dalj8690
committed
Added RSS feed support, from /docs folder
1 parent 6082e58 commit 3f8d507

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

.github/workflows/rss-generator.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
branches: [ main ]
66
paths:
7-
- 'docs/**'
7+
- 'docs/**/*.pdf' # Only trigger on PDF changes in docs/
88

99
jobs:
1010
build:

generate_rss.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
import datetime
3+
4+
DOCS_DIR = "docs"
5+
RSS_FILE = os.path.join(DOCS_DIR, "rss.xml")
6+
BASE_URL = "https://damirlj.github.io/modern_cpp_tutorials/docs"
7+
8+
header = f"""<?xml version="1.0" encoding="UTF-8" ?>
9+
<rss version="2.0">
10+
<channel>
11+
<title>Modern C++ Tutorials</title>
12+
<link>{BASE_URL}/</link>
13+
<description>Latest articles in Modern C++ Tutorials</description>
14+
<lastBuildDate>{datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S +0000')}</lastBuildDate>
15+
"""
16+
17+
# Collecting all PDF files in the docs folder, ordered by date
18+
items = []
19+
for file in sorted(os.listdir(DOCS_DIR), reverse=True):
20+
if file.endswith(".pdf"):
21+
title = file.replace("_", " ").replace(".pdf", "").title()
22+
link = f"{BASE_URL}/{file}"
23+
pub_date = datetime.datetime.utcfromtimestamp(os.path.getmtime(os.path.join(DOCS_DIR, file))).strftime('%a, %d %b %Y %H:%M:%S +0000')
24+
items.append(f"""
25+
<item>
26+
<title>{title}</title>
27+
<link>{link}</link>
28+
<guid>{link}</guid>
29+
<pubDate>{pub_date}</pubDate>
30+
</item>""")
31+
32+
footer = """
33+
</channel>
34+
</rss>
35+
"""
36+
37+
# If rss.xml exists, update it; else create a new one
38+
if os.path.exists(RSS_FILE):
39+
with open(RSS_FILE, "r", encoding="utf-8") as f:
40+
content = f.read()
41+
# Remove the old <channel> content before adding new items
42+
new_content = content.split("</channel>")[0] + "</channel>" + footer
43+
with open(RSS_FILE, "w", encoding="utf-8") as f:
44+
f.write(new_content)
45+
else:
46+
# If RSS file doesn't exist, create it from scratch
47+
with open(RSS_FILE, "w", encoding="utf-8") as f:
48+
f.write(header + "\n".join(items) + footer)
49+
50+
print("✅ RSS feed updated for PDFs!")

0 commit comments

Comments
 (0)