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 | |
| parent | 2626af3eb33d5bf61a38be34c12988d5f90bca99 (diff) | |
フロー図: 「最終更新」日を自動で入れるようにし、版番号をやめる
版番号(第1版)は内容を変えても手で上げないと古いままになるため、やめた。
代わりに、図の右上の日付を scripts/stamp-flow-version.py が git の履歴から自動で入れるようにした。図の内容を最後に変えた日(未コミットの変更があれば今日)になる。
make-flow-simple.py と make-flow-pdfs.py は、この処理を先に走らせてから成果物を作る。PDFのメタデータの版情報も、SVGの日付から取るようにした。
ページの「改変を認めていない理由」の記述も、版番号から最終更新日の説明に直した。
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/make-flow-pdfs.py | 18 | ||||
| -rw-r--r-- | scripts/make-flow-simple.py | 4 | ||||
| -rw-r--r-- | scripts/stamp-flow-version.py | 71 |
3 files changed, 89 insertions, 4 deletions
diff --git a/scripts/make-flow-pdfs.py b/scripts/make-flow-pdfs.py index 70ad495..e9f7942 100644 --- a/scripts/make-flow-pdfs.py +++ b/scripts/make-flow-pdfs.py @@ -16,6 +16,7 @@ base は SVG の元の高さ(リンク行を追記する前の値)。SVG の """ import pathlib import re +import subprocess import sys import pymupdf @@ -49,8 +50,14 @@ LIC_PUBLIC = ("© 安竹洋平(小平市議会議員)/公立版・私立ç‰ # 私立版と簡易版には課題欄がないので、その但し書きは入れない LIC_NO_KADAI = ("© 安竹洋平(小平市議会議員)/公立版・私立版とも CC BY-ND 4.0" "(切り取り可・無保証)/yasutakeyohei.com/ijime-judai-jitai/flow/") -# 図に合わせた版の情報(PDFのメタデータにも入れる) -EDITION = "第1版(令和8年9月23日)" +# 版の情報は、SVGの右上の「最終更新:…」から取る(stamp-flow-version.py が入れる) +EDITION_RE = re.compile(r'x="1796" y="56"[^>]*>([^<]*)</text>') + + +def edition_of(svg_name): + """SVGの右上の「最終更新:…」を、PDFのメタデータ用に取り出す。""" + m = EDITION_RE.search((IMAGES / svg_name).read_text(encoding="utf-8")) + return m.group(1) if m else "" # (SVGファイル名, 出力の6ページ版, 出力の1ページ版, SVGの元の高さ, フッターのライセンス, 版の名前) JOBS = [ @@ -131,9 +138,9 @@ def build(svg_name, out_6p, out_1p, base_h, license_text, label): out.set_metadata({ "title": f"{TITLE}({label})", "author": "安竹洋平(小平市議会議員)", - "subject": (f"{EDITION}。CC BY-ND 4.0(表示—改変禁止)。公立学校版・私立学校版とも同じライセンス。" + "subject": (f"{edition_of(svg_name)}。CC BY-ND 4.0(表示—改変禁止)。公立学校版・私立学校版とも同じライセンス。" "出典 https://yasutakeyohei.com/ijime-judai-jitai/flow/"), - "keywords": f"いじめ重大事態, フロー図, {label}, {EDITION}, CC BY-ND 4.0, 安竹洋平, 小平市", + "keywords": f"いじめ重大事態, フロー図, {label}, {edition_of(svg_name)}, CC BY-ND 4.0, 安竹洋平, 小平市", "creator": "https://yasutakeyohei.com/ijime-judai-jitai/flow/", }) out.subset_fonts() @@ -149,5 +156,8 @@ def build(svg_name, out_6p, out_1p, base_h, license_text, label): src.close() +# 「最終更新」の日付を先に更新してから、PDFを作る +subprocess.run([sys.executable, "scripts/stamp-flow-version.py"], check=True) + for svg_name, out_6p, out_1p, base_h, license_text, label in JOBS: build(svg_name, out_6p, out_1p, base_h, license_text, label) diff --git a/scripts/make-flow-simple.py b/scripts/make-flow-simple.py index be16cf1..1bc8f4a 100644 --- a/scripts/make-flow-simple.py +++ b/scripts/make-flow-simple.py @@ -11,6 +11,7 @@ 使い方: python scripts/make-flow-simple.py """ import re +import subprocess import sys from pathlib import Path @@ -22,6 +23,9 @@ FF = "'Noto Sans JP','Yu Gothic','Hiragino Kaku Gothic ProN',Meiryo,sans-serif" SHIFT = 525 # 課題欄を外したぶん、下の要素を上へ GAP_Y = 2941 # ここより下の y をずらす +# 「最終更新」の日付を先に更新する(簡易版は公立版SVGから作るため) +subprocess.run([sys.executable, "scripts/stamp-flow-version.py"], check=True) + text = SRC.read_text(encoding="utf-8") # 1) 課題欄(枠・見出し・注記・12項目)を削除 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() |
