generate rss feed from blog resolves #1

This commit is contained in:
ktyl 2022-06-03 16:12:32 +01:00
parent 691daf2651
commit c23098e982
4 changed files with 94 additions and 13 deletions

View File

@ -24,6 +24,7 @@ HTML_INCLUDES = $(shell find $(SRC_DIR)/inc_html -name *.html)
CSS_INCLUDES = $(shell find $(SRC_DIR)/inc_css -name *.css) CSS_INCLUDES = $(shell find $(SRC_DIR)/inc_css -name *.css)
BLOG_INDEX = $(OUT_DIR)/blog.html BLOG_INDEX = $(OUT_DIR)/blog.html
BLOG_RSS = $(BLOG_OUT_DIR)/index.xml
BLOG_INDEX_LINKS = $(BLOG_TMP_DIR)/blogindexlinks.html BLOG_INDEX_LINKS = $(BLOG_TMP_DIR)/blogindexlinks.html
BLOG_TARGETS = $(BLOG_PAGES:$(BLOG_SRC_DIR)/%.md=$(BLOG_OUT_DIR)/%.html) BLOG_TARGETS = $(BLOG_PAGES:$(BLOG_SRC_DIR)/%.md=$(BLOG_OUT_DIR)/%.html)
@ -45,9 +46,16 @@ deploy: site
$(OUT_DIR)/%.html: $(ROOT_DIR)/%.html $(HTML_INCLUDES) $(BLOG_INDEX_LINKS) | $(OUT_DIR) $(OUT_DIR)/%.html: $(ROOT_DIR)/%.html $(HTML_INCLUDES) $(BLOG_INDEX_LINKS) | $(OUT_DIR)
python ppp/ppp.py $< $(HTML_INCLUDES) $(BLOG_INDEX_LINKS) > $@ python ppp/ppp.py $< $(HTML_INCLUDES) $(BLOG_INDEX_LINKS) > $@
blog: $(BLOG_TARGETS) | $(BLOG_TMP_DIR) $(OUT_DIR)/%.css: $(ROOT_DIR)/%.css $(CSS_INCLUDES) | $(OUT_DIR)
python ppp/ppp.py $< $(CSS_INCLUDES) > $@
blog-index: $(BLOG_INDEX) $(OUT_DIR):
mkdir -p $@
blog: $(BLOG_TARGETS) $(BLOG_RSS) | $(BLOG_TMP_DIR)
$(BLOG_RSS): $(BLOG_PAGES)
python scripts/mkblogrss.py $(BLOG_PAGES) > $@
$(BLOG_INDEX_LINKS): $(BLOG_TARGETS) | $(BLOG_TMP_DIR) $(BLOG_INDEX_LINKS): $(BLOG_TARGETS) | $(BLOG_TMP_DIR)
python scripts/mkblogindex.py $(BLOG_TARGETS) > $@ python scripts/mkblogindex.py $(BLOG_TARGETS) > $@
@ -66,14 +74,6 @@ $(BLOG_OUT_DIR): | $(OUT_DIR)
$(BLOG_TMP_DIR): $(BLOG_TMP_DIR):
mkdir -p $@ mkdir -p $@
$(OUT_DIR)/%.html: $(ROOT_DIR)/%.html $(HTML_INCLUDES) $(BLOG_INDEX_LINKS) | $(OUT_DIR)
python ppp/ppp.py $< $(HTML_INCLUDES) $(BLOG_INDEX_LINKS) > $@
$(OUT_DIR)/%.css: $(ROOT_DIR)/%.css $(CSS_INCLUDES) | $(OUT_DIR)
python ppp/ppp.py $< $(CSS_INCLUDES) > $@
$(OUT_DIR):
mkdir -p $@
clean: clean:
rm -rf $(OUT_DIR) $(BLOG_TMP_DIR) rm -rf $(OUT_DIR) $(BLOG_TMP_DIR)

View File

@ -28,7 +28,12 @@ posts.reverse()
# for each file we want to output an <a> tag with a relative href to the site root # for each file we want to output an <a> tag with a relative href to the site root
for path in posts: for path in posts:
date = re.sub(path_pattern, r'<span class="post-date">\2-\3-\4</span>', path) m = re.match(path_pattern, path)
year = m.group(2)
month = m.group(3).rjust(2, '0')
day = m.group(4).rjust(2, '0')
date = f'<span class="post-date">{year}-{month}-{day}</span>'
title = "" title = ""
with open(path) as f: with open(path) as f:

76
scripts/mkblogrss.py Normal file
View File

@ -0,0 +1,76 @@
#!/usr/bin/env python3
import markdown
import pathlib
import sys
import re
def print_usage():
print("\nusage: python mkblogrss.py POSTS\n")
print("\n")
print("\t\tPOSTS\tfilepaths of blog posts")
# check args for at least one file path
if len(sys.argv) < 2:
print_usage()
sys.exit(1)
# posts are arguments from index 1 onwards
posts = sys.argv[1:]
# header and footer to enclose feed items
header = """<?xml version="1.0" encoding="utf-8" ?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Just Testing</title>
<link>https://ktyl.dev/blog/index.html</link>
<description>Vaguely technical "blogging" from Cat</description>
<atom:link href="https://ktyl.dev/blog/index.xml" rel="self" type="application/rss+xml"/>
"""
footer = "</channel></rss>"
# regex patterns
title_pattern = re.compile("<h1>(.+)</h1>")
path_pattern = re.compile("(.+)\/(\d{4})\/(\d{1,2})\/(\d{1,2})\/(.+).md")
def make_item(path):
str = "<item>\n"
# get the HTML version of the file
text = ""
with open(path) as f:
text = f.read()
html = markdown.markdown(text, extensions=["fenced_code"])
# title
title = ""
m = title_pattern.match(html)
title = m.group(1)
str += f"<title>{title}</title>\n"
# link
url = "/".join(pathlib.Path(path).parts[2:])
url = url.replace(".md", ".html")
link = f"https://ktyl.dev/blog/{url}"
str += f"<link>{link}</link>\n"
# content
description = html
description = re.sub('<', '&lt;', description)
description = re.sub('>', '&gt;', description)
str += f"<description>{description}</description>\n"
# pub date
date = re.sub(path_pattern, r'\2-\3-\4', path)
str += f"<pubDate>{date}</pubDate>\n"
str += "</item>"
return str
# print everything!
print(header)
for p in posts:
print(make_item(p))
print(footer)

View File

@ -1,7 +1,7 @@
<div class="socials"> <div class="socials">
<a href="mailto:me@ktyl.dev"><i class="fas fa-envelope"></i></a>
<a href="https://ktyl.dev/blog/index.xml"><i class="fas fa-rss"></i></a>
<a href="https://github.com/ktyldev/"><i class="fab fa-github"></i></a> <a href="https://github.com/ktyldev/"><i class="fab fa-github"></i></a>
<a href="https://ktyl.itch.io/"><i class="fab fa-itch-io"></i></a> <a href="https://ktyl.itch.io/"><i class="fab fa-itch-io"></i></a>
<a href="https://instagram.com/ktyldev"><i class="fab fa-instagram"></i></a>
<a href="https://linkedin.com/in/cat-flynn-08751611b/"><i class="fab fa-linkedin"></i></a> <a href="https://linkedin.com/in/cat-flynn-08751611b/"><i class="fab fa-linkedin"></i></a>
<a href="https://youtube.com/channel/UCwAHiEt20BuQtih7yYFAokA"><i class="fab fa-youtube"></i></a>
</div> </div>