Repository navigation
Expand file tree
/
Copy pathmake_profile.py
More file actions
76 lines (62 loc) · 2.48 KB
/
Copy pathmake_profile.py
File metadata and controls
76 lines (62 loc) · 2.48 KB
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
#!/usr/bin/env python3
"""GitHub profile README (banner.md) in the terminalclock style: one monospace panel, no emoji, no
colour badges, only text and the guide's glyphs (· › ╭ ╮ ╰ ╯ │ ─).
python dev/scripts/make_profile.py [out.md] # default: banner.md, from dev/config/profile.json
The panel is a rounded frame holding the About pattern (right-aligned label, plain value). Lines
never run past `width` characters: a long value wraps under its value column. Dark and light
banner images switch with the viewer's GitHub theme (<picture>), like theme "auto" on the site.
"""
import json
import os
import sys
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def wrap(text, width):
lines, cur = [], ""
for word in text.split(" "):
if cur and len(cur) + 1 + len(word) > width:
lines.append(cur)
cur = word
else:
cur = word if not cur else cur + " " + word
lines.append(cur)
return lines
def panel(cfg):
inner = cfg["width"]
label_w = max(len(k) for k, _ in cfg["rows"])
value_w = inner - 2 * 2 - label_w - 2 # 2 spaces padding each side, 2 between label and value
body = [""]
body.append(cfg["title"])
body.extend(wrap(cfg["tagline"], inner - 4))
body.append("")
for label, value in cfg["rows"]:
first, *rest = wrap(value, value_w)
body.append(label.rjust(label_w) + " " + first)
body.extend(" " * label_w + " " + r for r in rest)
body.append("")
out = ["╭" + "─" * inner + "╮"]
for line in body:
out.append("│ " + line.ljust(inner - 2) + "│")
out.append("╰" + "─" * inner + "╯")
assert all(len(x) == inner + 2 for x in out), "panel line too wide"
return "\n".join(out)
def main():
with open(os.path.join(ROOT, "dev", "config", "profile.json"), encoding="utf-8") as fh:
cfg = json.load(fh)
img = cfg["images"]
links = " · ".join(f"[{k}]({u})" for k, u in cfg["links"])
md = f"""<!-- generated by dev/scripts/make_profile.py from dev/config/profile.json: edit that, not this -->
<picture>
<source media="(prefers-color-scheme: dark)" srcset="{img["dark"]}">
<img alt="{img["alt"]}" src="{img["light"]}">
</picture>
```text
{panel(cfg)}
```
{links}
"""
out = sys.argv[1] if len(sys.argv) > 1 else os.path.join(ROOT, "banner.md")
with open(out, "w", encoding="utf-8") as fh:
fh.write(md)
print("wrote", out)
if __name__ == "__main__":
main()