treesummaryrefslogcommitdiff
path: root/blog/post-receive
blob: 32124b6960a5f98612572f44c822ce39486e5a0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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>&#x1F4DD;</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 &mdash; Blog", f"<h1>Patrick Schönberger &mdash; Blog</h1>\n{index_html}", "/srv/www/blog/index.html")