aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/stamp-flow-version.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/stamp-flow-version.py')
-rw-r--r--scripts/stamp-flow-version.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/scripts/stamp-flow-version.py b/scripts/stamp-flow-version.py
new file mode 100644
index 0000000..4a1247e
--- /dev/null
+++ b/scripts/stamp-flow-version.py
@@ -0,0 +1,71 @@
+"""フロー図の「最終更新」日付を、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)を丸ごと置き換える
+LINE_RE = re.compile(r'<text x="1796" y="56"[^>]*>.*?</text>')
+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(
+ f'<text x="1796" y="56" font-family="\'Noto Sans JP\',\'Yu Gothic\','
+ f"'Hiragino Kaku Gothic ProN',Meiryo,sans-serif\" font-size=\"13\" "
+ f'font-weight="700" fill="#37474F">{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()