diff options
| author | Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> | 2026-09-24 00:26:12 +0900 |
|---|---|---|
| committer | Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> | 2026-09-24 00:26:12 +0900 |
| commit | b3997405d91239eac82c631d18a48b13181e2820 (patch) | |
| tree | c11ed450298db62f0b7344b7d78e040ff245c122 /scripts/stamp-flow-version.py | |
| parent | 2626af3eb33d5bf61a38be34c12988d5f90bca99 (diff) | |
フロー図: 「最終更新」日を自動で入れるようにし、版番号をやめる
版番号(第1版)は内容を変えても手で上げないと古いままになるため、やめた。
代わりに、図の右上の日付を scripts/stamp-flow-version.py が git の履歴から自動で入れるようにした。図の内容を最後に変えた日(未コミットの変更があれば今日)になる。
make-flow-simple.py と make-flow-pdfs.py は、この処理を先に走らせてから成果物を作る。PDFのメタデータの版情報も、SVGの日付から取るようにした。
ページの「改変を認めていない理由」の記述も、版番号から最終更新日の説明に直した。
Diffstat (limited to 'scripts/stamp-flow-version.py')
| -rw-r--r-- | scripts/stamp-flow-version.py | 71 |
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() |
