#!/usr/bin/env python
import os
import subprocess
def check(args, input=None):
return subprocess.check_output(args, text=True, input=input).strip()
# write a html file
def print_html(title, body, file):
with open(file, "w") as f:
f.write(f"""
{title}
{body}
""")
# config repo and branch
REPO = "/srv/git/blog"
BRANCH = "main"
# go to repo
os.chdir(REPO)
# list files
files = check(["git", "ls-tree", "--name-only", BRANCH]).splitlines()
# list of files with their titles and dates for creating index.html
index_list = []
# loop over files
for f_md in files:
# get created and modified dates
creation_date = check(["git", "log", "--diff-filter=A", "--format=%cd", "--date=format:%Y-%m-%d", "--follow", "--", f_md])
last_modified_date = check(["git", "log", "-1", "--format=%cd", "--date=format:%Y-%m-%d", "--", f_md])
# get markdown from file
md = check(["git", "--no-pager", "show", f"{BRANCH}:{f_md}"])
# convert it to html
html = check(["md2html", "--github", "--fstrikethrough", "--ftables", "--ftasklists", "--funderline", "--fwiki-links"], md)
# read title from first line of markdown
title = md.splitlines()[0][1:].strip()
# create the line under the title
creation_line = f"
{creation_date}{f" (last modified {last_modified_date})" if creation_date != last_modified_date else ""}
"
# strip title from html so we can insert creation_line
html_body = "\n".join(html.splitlines()[1:])
# strip .md from file anme
f = f_md[:-3]
f_html = f"{f}.html"
# add file to index_list
index_list.append((creation_date, f_html, title))
# create html file
print_html(title, f"Home