Generate or update a docs/zh/ index page for a section.
(
section_key: str,
docs_page_path: str,
section_title: str,
entries: List[dict],
repo_root: Path,
dry_run: bool = False,
)
| 240 | |
| 241 | |
| 242 | def generate_section_page( |
| 243 | section_key: str, |
| 244 | docs_page_path: str, |
| 245 | section_title: str, |
| 246 | entries: List[dict], |
| 247 | repo_root: Path, |
| 248 | dry_run: bool = False, |
| 249 | ) -> None: |
| 250 | """Generate or update a docs/zh/ index page for a section.""" |
| 251 | docs_page = repo_root / docs_page_path |
| 252 | docs_page.parent.mkdir(parents=True, exist_ok=True) |
| 253 | |
| 254 | # Read existing page to preserve any manually written intro |
| 255 | existing_content = "" |
| 256 | auto_marker = "<!-- AUTO-GENERATED: do not edit below this line -->" |
| 257 | if docs_page.exists(): |
| 258 | existing_content = docs_page.read_text(encoding="utf-8") |
| 259 | |
| 260 | # Build front matter |
| 261 | fm = { |
| 262 | "title": section_title, |
| 263 | "generated": datetime.now().strftime("%Y-%m-%d"), |
| 264 | "doc_count": len(entries), |
| 265 | } |
| 266 | fm_str = "---\n" |
| 267 | fm_str += f"title: {section_title}\n" |
| 268 | fm_str += f"generated: '{fm['generated']}'\n" |
| 269 | fm_str += f"doc_count: {fm['doc_count']}\n" |
| 270 | fm_str += "---\n\n" |
| 271 | |
| 272 | # Build intro section (preserve existing if present, otherwise generate) |
| 273 | if auto_marker in existing_content: |
| 274 | intro = existing_content.split(auto_marker)[0] |
| 275 | else: |
| 276 | intro = fm_str + f"# {section_title}\n\n" |
| 277 | |
| 278 | # Build auto-generated table section |
| 279 | auto_section = f"{auto_marker}\n\n" |
| 280 | auto_section += f"> 共 **{len(entries)}** 篇文档,最后更新:{datetime.now().strftime('%Y-%m-%d')}\n\n" |
| 281 | |
| 282 | if section_key == "nmpa/guidance": |
| 283 | # Group by device category |
| 284 | groups = group_nmpa_guidance(entries) |
| 285 | for group_name, group_entries in sorted(groups.items()): |
| 286 | auto_section += f"## {group_name}\n\n" |
| 287 | auto_section += "| 文档名称 | 文号 | 发布年份 |\n" |
| 288 | auto_section += "|----------|------|----------|\n" |
| 289 | for entry in sorted(group_entries, key=lambda e: e["effective_date"], reverse=True): |
| 290 | auto_section += _entry_row(entry, repo_root) + "\n" |
| 291 | auto_section += "\n" |
| 292 | else: |
| 293 | # Simple flat table |
| 294 | auto_section += "| 文档名称 | 文号 | 发布年份 |\n" |
| 295 | auto_section += "|----------|------|----------|\n" |
| 296 | for entry in sorted(entries, key=lambda e: e["effective_date"], reverse=True): |
| 297 | auto_section += _entry_row(entry, repo_root) + "\n" |
| 298 | auto_section += "\n" |
| 299 |
no test coverage detected