{title_html}
目錄
- {toc}
#!/usr/bin/env python3
"""Convert the GVFG Markdown reference to a branded, UPDF-editable PDF."""
from __future__ import annotations
import argparse
import base64
import html
import os
import re
import shutil
import subprocess
import tempfile
import time
import zipfile
from pathlib import Path
CSS = r"""
@page { size: A4; margin: 0 0 17mm; }
* { box-sizing: border-box; }
body { margin: 0; color: #172033; font-family: "Microsoft JhengHei", "Segoe UI", sans-serif; font-size: 9.4pt; line-height: 1.48; }
.page-layout { width: 100%; border: 0; border-collapse: collapse; margin: 0; padding: 0; }
.page-layout > thead { display: table-header-group; }
.page-layout > thead > tr.banner-row > td { width: 100%; height: 25.03mm; padding: 0; border: 0; background: #000; }
.page-layout > thead > tr.header-spacer > td { width: 100%; height: 6mm; padding: 0; border: 0; background: #fff; }
.page-layout > thead img { display: block; width: 210mm; height: 25.03mm; object-fit: fill; }
.page-layout > tbody > tr > td { padding: 0 15mm; border: 0; vertical-align: top; }
.content { position: relative; z-index: 1; }
h1 { color: #0b3a69; font-size: 25pt; margin: 0 0 16pt; padding-bottom: 8pt; border-bottom: 3px solid #2e75b6; }
h2 { color: #0b3a69; font-size: 17pt; margin: 20pt 0 8pt; padding-bottom: 4pt; border-bottom: 1px solid #9fbad2; break-after: avoid; }
h3 { color: #174f7c; font-size: 12.5pt; margin: 15pt 0 6pt; break-after: avoid; }
p { margin: 4pt 0 8pt; }
ul, ol { margin: 4pt 0 8pt 20pt; padding: 0; }
li { margin: 2pt 0; }
code { font-family: Consolas, "Courier New", monospace; font-size: 8.6pt; color: #7a1f45; background: #f3f5f7; padding: .5pt 2.5pt; border-radius: 2px; }
pre { background: #f3f5f7; border-left: 3px solid #2e75b6; padding: 7pt 9pt; white-space: pre-wrap; overflow-wrap: anywhere; break-inside: avoid; }
pre code { color: #172033; background: transparent; padding: 0; }
.content table { width: 100%; border-collapse: collapse; margin: 6pt 0 11pt; font-size: 8.2pt; break-inside: auto; }
.content thead { display: table-header-group; }
.content tr { break-inside: avoid; }
.content th { color: white; background: #2e75b6; text-align: left; font-weight: 600; }
.content th, .content td { border: 1px solid #b7c7d6; padding: 4pt 5pt; vertical-align: top; overflow-wrap: anywhere; }
.content tbody tr:nth-child(even) { background: rgba(245,248,251,.92); }
a { color: #155b91; text-decoration: none; }
.toc { columns: 2; column-gap: 22pt; list-style: none; margin: 0 0 16pt; padding: 10pt 12pt; background: rgba(244,247,250,.92); border: 1px solid #c8d5e1; }
.toc li { break-inside: avoid; margin: 2pt 0; }
.toc-3 { padding-left: 10pt; font-size: 8.7pt; }
.toc-page { break-after: page; page-break-after: always; }
"""
def first_existing(candidates: list[Path]) -> Path | None:
for candidate in candidates:
if candidate and candidate.is_file():
return candidate
return None
def find_pwsh() -> Path:
found = shutil.which("pwsh.exe")
candidates = []
if found:
candidates.append(Path(found))
candidates.append(
Path.home()
/ ".cache/codex-runtimes/codex-primary-runtime/dependencies/native/powershell/pwsh.exe"
)
result = first_existing(candidates)
if not result:
raise RuntimeError("PowerShell 7 (pwsh.exe) was not found; Markdown conversion needs ConvertFrom-Markdown.")
return result
def find_edge() -> Path:
candidates = []
for variable in ("ProgramFiles(x86)", "ProgramFiles"):
root = os.environ.get(variable)
if root:
candidates.append(Path(root) / "Microsoft/Edge/Application/msedge.exe")
result = first_existing(candidates)
if not result:
raise RuntimeError("Microsoft Edge was not found.")
return result
def markdown_to_html(markdown: str, pwsh: Path) -> str:
command = (
"$ErrorActionPreference='Stop';"
"[Console]::InputEncoding=[Text.UTF8Encoding]::new($false);"
"[Console]::OutputEncoding=[Text.UTF8Encoding]::new($false);"
"$OutputEncoding=[Text.UTF8Encoding]::new($false);"
"$source=[Console]::In.ReadToEnd();"
"(ConvertFrom-Markdown -InputObject $source).Html"
)
completed = subprocess.run(
[str(pwsh), "-NoLogo", "-NoProfile", "-Command", command],
input=markdown.encode("utf-8"),
capture_output=True,
check=False,
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
)
if completed.returncode:
details = completed.stderr.decode("utf-8", errors="replace").strip()
raise RuntimeError(f"Markdown conversion failed:\n{details}")
return completed.stdout.decode("utf-8")
def add_heading_ids_and_toc(body: str) -> tuple[str, str]:
items: list[str] = []
counter = 0
pattern = re.compile(r'
{title_html}目錄
|