61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
gen_changelog_chapter.py
|
|
========================
|
|
把 CHANGELOG.md 转换为 PRISM 全书的 "第 17 章 变更日志" 章节文件,
|
|
便于 build_book.sh 把它当作普通章节加入 PDF。
|
|
|
|
用法:
|
|
python3 gen_changelog_chapter.py <changelog.md> <out_md> <version> <build_date>
|
|
"""
|
|
from __future__ import annotations
|
|
import pathlib
|
|
import sys
|
|
|
|
|
|
def main(changelog: pathlib.Path, out_md: pathlib.Path,
|
|
version: str, build_date: str) -> None:
|
|
raw = changelog.read_text(encoding="utf-8")
|
|
|
|
# 给章节加一个统一的标题(由 build_book.sh 的 chapter_titles 覆盖,
|
|
# 但这里也写一个 H1 以保证 md 渲染时仍是一个完整章节)。
|
|
header = "\n".join([
|
|
"# Chapter 17 — 变更日志 (Changelog)",
|
|
"",
|
|
f"> 本章直接镜像项目根的 [`CHANGELOG.md`](CHANGELOG.md);**当前版本 v{version}, 构建于 {build_date}**。"
|
|
" 每次发版时由 [`tools/gen_changelog_chapter.py`](tools/gen_changelog_chapter.py) 自动同步到 PDF。",
|
|
"",
|
|
])
|
|
|
|
# 把原 CHANGELOG.md 的 H1 (`# Changelog`) + 顶层引言 + 首个 --- 全部跳过,
|
|
# 从第一个真正的 ## 二级标题开始拼接,避免重复说明。
|
|
lines = raw.splitlines()
|
|
body_lines: list[str] = []
|
|
in_body = False
|
|
for ln in lines:
|
|
if not in_body:
|
|
# 第一个 ## 之前的所有内容都跳过
|
|
if ln.startswith("## "):
|
|
in_body = True
|
|
body_lines.append(ln)
|
|
continue
|
|
body_lines.append(ln)
|
|
|
|
full = header + "\n".join(body_lines).rstrip() + "\n"
|
|
out_md.write_text(full, encoding="utf-8")
|
|
print(f"[changelog] wrote {out_md} (v{version}, {build_date})")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 5:
|
|
sys.stderr.write(
|
|
"usage: gen_changelog_chapter.py <changelog.md> <out.md> <version> <build_date>\n"
|
|
)
|
|
sys.exit(1)
|
|
main(
|
|
pathlib.Path(sys.argv[1]),
|
|
pathlib.Path(sys.argv[2]),
|
|
sys.argv[3],
|
|
sys.argv[4],
|
|
)
|