#!/usr/bin/env python3 """After 04-xx renumbering: fix cross-links and per-doc manifest names across the repo.""" from __future__ import annotations import re from pathlib import Path ROOT = Path(__file__).resolve().parents[1] # Old canonical .md basename -> new basename (apply before per-file yaml fix) MD_MAP: list[tuple[str, str]] = [ ("04-11-nodejs-副本与滚动发布.md", "04-06-nodejs-副本与滚动发布.md"), ("04-10-nodejs-Ingress与Traefik.md", "04-07-nodejs-Ingress与Traefik.md"), ("04-09-nodejs-存储与卷.md", "04-11-nodejs-存储与卷.md"), ("04-08-nodejs-安全上下文.md", "04-10-nodejs-安全上下文.md"), ("04-07-nodejs-调度与亲和.md", "04-09-nodejs-调度与亲和.md"), ("04-06-nodejs-探针与健康检查.md", "04-05-nodejs-探针与健康检查.md"), ("04-05-nodejs-资源请求与限制.md", "04-08-nodejs-资源请求与限制.md"), ("04-04-nodejs-端口与Service.md", "04-02-nodejs-端口与Service.md"), ("04-03-nodejs-环境变量与配置注入.md", "04-04-nodejs-环境变量与配置注入.md"), ("04-02-nodejs-镜像与运行命令.md", "04-03-nodejs-镜像与运行命令.md"), ] SKIP_DIR_NAMES = {".git", "node_modules", "logs"} TEXT_SUFFIXES = {".md", ".yml", ".yaml", ".sh", ".txt", ".example"} def iter_files(): for p in ROOT.rglob("*"): if not p.is_file(): continue if any(x in p.parts for x in SKIP_DIR_NAMES): continue if p.suffix.lower() not in TEXT_SUFFIXES and p.name not in ( ".env.verify.example", ): continue yield p def apply_md_map(content: str) -> str: for old, new in MD_MAP: content = content.replace(old, new) return content def fix_doc_manifests(content: str, doc_id: str) -> str: return re.sub( r"04-\d{2}-nodejs-demo\.yaml", f"04-{doc_id}-nodejs-demo.yaml", content, ) def fix_title(content: str, title_body: str) -> str: lines = content.splitlines() if lines and lines[0].startswith("# "): lines[0] = f"# {title_body}" return "\n".join(lines) + ("\n" if content.endswith("\n") else "") return content def main() -> None: for path in iter_files(): raw = path.read_text(encoding="utf-8") new = apply_md_map(raw) if path.parent.name == "docs" and re.match(r"04-\d{2}-", path.name): m = re.match(r"04-(\d{2})-", path.name) if m: doc_id = m.group(1) new = fix_doc_manifests(new, doc_id) base = path.name.removesuffix(".md") new = fix_title(new, base) path.write_text(new, encoding="utf-8") # verify playbooks: doc_filename must match renumbered docs vf = ROOT / "ansible/playbooks/verify" for yml in sorted(vf.glob("04-*.yml")): m = re.match(r"04-(\d{2})\.yml$", yml.name) if not m: continue nid = m.group(1) text = yml.read_text(encoding="utf-8") # find docs/04-NN-*.md in file after md_map would already be applied dm = re.search(r'doc_filename:\s*"([^"]+)"', text) if not dm: continue old_fn = dm.group(1) if not old_fn.startswith(f"04-{nid}-"): # pick any docs/04-NN-*.md with this NN docs_dir = ROOT / "docs" matches = list(docs_dir.glob(f"04-{nid}-*.md")) if len(matches) == 1: text = re.sub( r'doc_filename:\s*"[^"]+"', f'doc_filename: "{matches[0].name}"', text, count=1, ) yml.write_text(text, encoding="utf-8") if __name__ == "__main__": main()