]> gitweb.ps.run Git - git-hooks/blob - blog/post-receive
add comment to git-hooks/post-receive
[git-hooks] / blog / post-receive
1 #!/usr/bin/env python
2
3 import os
4 import subprocess
5
6 def check(args, input=None):
7   return subprocess.check_output(args, text=True, input=input).strip()
8
9 # write a html file
10 def print_html(title, body, file):
11   with open(file, "w") as f:
12     f.write(f"""<!DOCTYPE html>
13 <html>
14 <head>
15 <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>">
16 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
17 <meta charset="utf-8">
18 <style>
19   :root {{
20     color-scheme: light dark;
21   }}
22   body {{
23     margin: 40px auto;
24     max-width: min(900px, 100vw);
25     line-height: 1.6;
26     font-size: 16px;
27     padding: 0 10px;
28   }}
29   h1, h2, h3 {{
30     line-height: 1.2;
31   }}
32   table {{
33     border-spacing: 15px;
34   }}
35 </style>
36 <title>{title}</title>
37 </head>
38 <body>
39 <center>
40 {body}
41 </center>
42 </body>
43 </html>""")
44
45 # config repo and branch
46 REPO = "/srv/git/blog"
47 BRANCH = "main"
48
49 # go to repo
50 os.chdir(REPO)
51
52 # list files
53 files = check(["git", "ls-tree", "--name-only", BRANCH]).splitlines()
54
55 # list of files with their titles and dates for creating index.html
56 index_list = []
57
58 # loop over files
59 for f_md in files:
60   # get created and modified dates
61   creation_date = check(["git", "log", "--diff-filter=A", "--format=%cd", "--date=format:%Y-%m-%d", "--follow", "--", f_md])
62   last_modified_date = check(["git", "log", "-1", "--format=%cd", "--date=format:%Y-%m-%d", "--", f_md])
63
64   # get markdown from file
65   md = check(["git", "--no-pager", "show", f"{BRANCH}:{f_md}"])
66
67   # convert it to html
68   html = check(["md2html", "--github", "--fstrikethrough", "--ftables", "--ftasklists", "--funderline", "--fwiki-links"], md)
69
70   # read title from first line of markdown
71   title = md.splitlines()[0][1:].strip()
72
73   # create the line under the title
74   creation_line = f"<p>{creation_date}{f" (last modified {last_modified_date})" if creation_date != last_modified_date else ""}</p>"
75
76   # strip title from html so we can insert creation_line
77   html_body = "\n".join(html.splitlines()[1:])
78
79   # strip .md from file anme
80   f = f_md[:-3]
81   f_html = f"{f}.html"
82
83   # add file to index_list
84   index_list.append((creation_date, f_html, title))
85
86   # create html file
87   print_html(title, f"<a href=\"/blog\">Home</a><h1>{title}</h1>\n{creation_line}\n{html_body}", f"/srv/www/blog/{f_html}")
88
89 # sort index_list by creation_date
90 sorted_index_list = sorted(index_list, key=lambda x: x[0], reverse=True)
91
92 # create index.html
93 index_html = "\n".join([f"<p><a href=\"/blog/{f}\">{c} {t}</a></p>" for (c, f, t) in sorted_index_list])
94 print_html("Patrick Schönberger &mdash; Blog", f"<h1>Patrick Schönberger &mdash; Blog</h1>\n{index_html}", "/srv/www/blog/index.html")