diff options
| -rw-r--r-- | blog/post-receive | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/blog/post-receive b/blog/post-receive new file mode 100644 index 0000000..32124b6 --- /dev/null +++ b/blog/post-receive @@ -0,0 +1,94 @@ +#!/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"""<!DOCTYPE html> +<html> +<head> +<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📝</text></svg>"> +<meta name="viewport" content="width=device-width, initial-scale=1.0" /> +<meta charset="utf-8"> +<style> + :root {{ + color-scheme: light dark; + }} + body {{ + margin: 40px auto; + max-width: min(900px, 100vw); + line-height: 1.6; + font-size: 16px; + padding: 0 10px; + }} + h1, h2, h3 {{ + line-height: 1.2; + }} + table {{ + border-spacing: 15px; + }} +</style> +<title>{title}</title> +</head> +<body> +<center> +{body} +</center> +</body> +</html>""") + +# 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"<p>{creation_date}{f" (last modified {last_modified_date})" if creation_date != last_modified_date else ""}</p>" + + # 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"<a href=\"/blog\">Home</a><h1>{title}</h1>\n{creation_line}\n{html_body}", f"/srv/www/blog/{f_html}") + +# sort index_list by creation_date +sorted_index_list = sorted(index_list, key=lambda x: x[0], reverse=True) + +# create index.html +index_html = "\n".join([f"<p><a href=\"/blog/{f}\">{c} {t}</a></p>" for (c, f, t) in sorted_index_list]) +print_html("Patrick Schönberger — Blog", f"<h1>Patrick Schönberger — Blog</h1>\n{index_html}", "/srv/www/blog/index.html") |
