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
|
"""フロー図の「最終更新」日付を、gitの履歴から自動で入れる。
- 公立版・私立版・簡易版のSVGの右上にある「最終更新:…」を書き換える
- 日付は、図の内容を最後に変えた日。コミット前の変更があれば今日の日付にする
- 図を編集したあと、make-flow-simple.py / make-flow-pdfs.py の前に自動で走る
(どちらのスクリプトからも呼ばれる)
使い方: python scripts/stamp-flow-version.py
"""
import datetime
import pathlib
import re
import subprocess
import sys
sys.stdout.reconfigure(encoding="utf-8")
IMAGES = pathlib.Path("public/ijime-judai-jitai/images")
SVGS = [
IMAGES / "ijime-judai-flow.svg",
IMAGES / "ijime-judai-flow-simple.svg",
IMAGES / "ijime-judai-flow-private.svg",
]
# 「最終更新:」の行(右上・x=1796 y=56)を丸ごと置き換える。
# text-anchor="end" を忘れると、右端からはみ出して見えなくなるので注意
LINE_RE = re.compile(r'<text x="1796" y="56"[^>]*>.*?</text>')
LINE_TAG = (
'<text x="1796" y="56" text-anchor="end" '
"font-family=\"'Noto Sans JP','Yu Gothic','Hiragino Kaku Gothic ProN',Meiryo,sans-serif\" "
'font-size="13" font-weight="700" fill="#37474F">'
)
REIWA_START = 2019 # 令和元年
def reiwa(d: datetime.date) -> str:
"""西暦の日付を「令和X年Y月Z日」にする。"""
n = d.year - REIWA_START + 1
era = "令和元年" if n == 1 else f"令和{n}年"
return f"{era}{d.month}月{d.day}日"
def last_change_date() -> datetime.date:
"""図の内容を最後に変えた日を返す。未コミットの変更があれば今日。"""
paths = [str(p) for p in SVGS]
dirty = any(
subprocess.run(["git", "diff", "--quiet", *opts, "--", *paths]).returncode != 0
for opts in (["--"], ["--cached", "--"])
)
if dirty:
return datetime.date.today()
out = subprocess.run(
["git", "log", "-1", "--format=%ad", "--date=short", "--", *paths],
capture_output=True, text=True, check=True,
).stdout.strip()
return datetime.date.fromisoformat(out) if out else datetime.date.today()
def main():
label = f"最終更新:{reiwa(last_change_date())}"
for svg in SVGS:
text = svg.read_text(encoding="utf-8")
new, n = LINE_RE.subn(LINE_TAG + label + "</text>", text)
if n != 1:
raise SystemExit(f"{svg}: 「最終更新」の行が {n} 箇所(1箇所のはず)")
if new != text:
svg.write_text(new, encoding="utf-8")
print(f"{svg.name}: {label}")
if __name__ == "__main__":
main()
|