#!/usr/bin/env python3
import markdown
import pathlib
import sys
import re
from datetime import datetime
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 = """
ktyl.dev
https://ktyl.dev/blog/index.html
mostly computer stuff!
"""
footer = ""
# regex patterns
title_pattern = re.compile("
(.+)
")
path_pattern = re.compile("(.+)\/(\d{4})\/(\d{1,2})\/(\d{1,2})\/(.+).md")
def make_item(path):
str = "\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 = None
m = title_pattern.match(html)
if m is None:
title = path.split('/')[-1]
else:
title = m.group(1)
str += f"{title}\n"
# link
url = "/".join(pathlib.Path(path).parts[1:])
url = url.replace(".md", ".html")
link = f"https://ktyl.dev/blog/{url}"
str += f"{link}\n"
# content
description = html
description = re.sub('<', '<', description)
description = re.sub('>', '>', description)
str += f"{description}\n"
# does the path have a date in it
if (path_pattern.match(path)):
date = re.sub(path_pattern, r'\2-\3-\4', path)
else:
date = datetime.now()
str += f"{date}\n"
str += ""
return str
# print everything!
print(header)
for p in posts:
print(make_item(p))
print(footer)