From bc412cf281a119c1f238c00d0196502aa5fc56cf Mon Sep 17 00:00:00 2001 From: patrick-scho Date: Tue, 3 Jun 2025 11:20:31 +0200 Subject: [PATCH 1/1] add blog/post-receive --- blog/post-receive | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 blog/post-receive 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""" + + + + + + +{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

{title}

\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"

{c} {t}

" for (c, f, t) in sorted_index_list]) +print_html("Patrick Schönberger — Blog", f"

Patrick Schönberger — Blog

\n{index_html}", "/srv/www/blog/index.html") -- 2.50.1