from pathlib import Path

import markdown
from weasyprint import HTML

BASE = Path("/var/www/html/share")
STEM = "china-llm-overseas-20260728"
MD_PATH = BASE / f"{STEM}.md"
HTML_PATH = BASE / f"{STEM}.html"
PDF_PATH = BASE / f"{STEM}.pdf"

source = MD_PATH.read_text(encoding="utf-8")
body_source = "## 执行摘要" + source.split("## 执行摘要", 1)[1]

md = markdown.Markdown(
    extensions=["extra", "tables", "footnotes", "toc", "sane_lists"],
    extension_configs={"toc": {"permalink": False, "toc_depth": "2-3"}},
)
body = md.convert(body_source)
toc = md.toc

css = r"""
@page {
  size: A4;
  margin: 18mm 15mm 18mm 17mm;
  @top-left {
    content: "中国大模型厂商海外拓展深度研究";
    font-family: "Noto Sans CJK SC";
    font-size: 7.5pt;
    color: #64748b;
  }
  @top-right {
    content: "2026.07";
    font-family: "Noto Sans CJK SC";
    font-size: 7.5pt;
    color: #64748b;
  }
  @bottom-center {
    content: counter(page);
    font-family: "Noto Sans CJK SC";
    font-size: 8pt;
    color: #64748b;
  }
}
@page:first {
  margin: 0;
  @top-left { content: none; }
  @top-right { content: none; }
  @bottom-center { content: none; }
}
:root {
  --ink: #172033;
  --muted: #5b677a;
  --navy: #12345b;
  --blue: #1769aa;
  --pale: #eef5fa;
  --line: #cbd5e1;
  --soft: #f7f9fb;
}
* { box-sizing: border-box; }
html { font-family: "Noto Sans CJK SC", "WenQuanYi Zen Hei", sans-serif; color: var(--ink); }
body { margin: 0; font-size: 9.6pt; line-height: 1.72; }
.cover {
  height: 297mm;
  padding: 27mm 24mm 22mm 26mm;
  background: linear-gradient(145deg, #0d2743 0%, #173f68 65%, #1e6a93 100%);
  color: white;
  position: relative;
  page-break-after: always;
}
.cover:before {
  content: "";
  position: absolute;
  right: -35mm;
  top: 25mm;
  width: 120mm;
  height: 120mm;
  border: 1px solid rgba(255,255,255,.18);
  border-radius: 50%;
}
.cover:after {
  content: "";
  position: absolute;
  right: 20mm;
  bottom: 30mm;
  width: 72mm;
  height: 72mm;
  border: 12mm solid rgba(255,255,255,.055);
  border-radius: 50%;
}
.cover .eyebrow { font-size: 10pt; letter-spacing: .18em; opacity: .8; margin-top: 8mm; }
.cover h1 { font-size: 30pt; line-height: 1.25; margin: 36mm 0 7mm; font-weight: 700; max-width: 150mm; }
.cover h2 { font-size: 16pt; line-height: 1.5; font-weight: 400; margin: 0; color: #dceaf5; }
.cover .rule { width: 33mm; height: 2px; background: #75c3e6; margin: 16mm 0 10mm; }
.cover .meta { font-size: 10pt; line-height: 1.9; color: #dbe8f2; }
.cover .audience { position: absolute; left: 26mm; bottom: 23mm; font-size: 8.5pt; color: #c7d8e6; }
.toc-page { page-break-after: always; }
.toc-page h1 { margin-top: 0; }
.toc ul { list-style: none; padding-left: 0; }
.toc ul ul { padding-left: 5mm; }
.toc li { margin: 2.3mm 0; border-bottom: 1px dotted #cbd5e1; }
.toc a { color: var(--ink); text-decoration: none; background: white; padding-right: 2mm; }
.report h2 { color: var(--navy); font-size: 17pt; line-height: 1.35; margin: 10mm 0 4mm; padding-bottom: 2.5mm; border-bottom: 1.4px solid var(--blue); break-after: avoid; }
.report h3 { color: #174d78; font-size: 12.5pt; line-height: 1.4; margin: 7mm 0 2.5mm; break-after: avoid; }
.report h4 { color: var(--ink); font-size: 10.5pt; margin: 5mm 0 2mm; break-after: avoid; }
p { margin: 0 0 3mm; text-align: justify; }
strong { color: #0f2f50; }
ul, ol { margin: 2mm 0 3mm; padding-left: 6mm; }
li { margin: 1.2mm 0; }
blockquote { margin: 4mm 0; padding: 3mm 4mm; border-left: 3px solid var(--blue); background: var(--pale); color: #334155; }
hr { border: 0; border-top: 1px solid var(--line); margin: 8mm 0; }
table { width: 100%; border-collapse: collapse; margin: 4mm 0 5mm; font-size: 7.7pt; line-height: 1.45; break-inside: auto; }
thead { display: table-header-group; }
tr { break-inside: avoid; }
th { background: var(--navy); color: white; text-align: left; font-weight: 600; padding: 2.2mm 2mm; border: 1px solid var(--navy); }
td { padding: 2mm; border: 1px solid var(--line); vertical-align: top; }
tbody tr:nth-child(even) td { background: var(--soft); }
a { color: #145e91; overflow-wrap: anywhere; }
.footnote { font-size: 7.5pt; line-height: 1.45; color: #475569; }
.footnote hr { margin-top: 8mm; }
.footnote p { text-align: left; }
.codehilite, code { font-family: "Noto Sans Mono CJK SC", monospace; }
.report > h2:first-child { margin-top: 0; }
.badge {
  display: inline-block;
  padding: 1.2mm 2.5mm;
  margin-right: 2mm;
  border: 1px solid #87acc7;
  border-radius: 2mm;
  color: #174d78;
  background: #f0f7fb;
  font-size: 7.5pt;
}
@media print {
  a { text-decoration: none; }
}
"""

html_doc = f"""<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>中国大模型厂商海外拓展深度研究</title>
<style>{css}</style>
</head>
<body>
<section class="cover">
  <div class="eyebrow">INDUSTRY RESEARCH · 2026</div>
  <h1>中国大模型厂商<br>海外拓展深度研究</h1>
  <h2>布局、进入路径与商业模式</h2>
  <div class="rule"></div>
  <div class="meta">
    研究截止日：2026年7月28日<br>
    证据口径：公司公告、官方文档、监管文件与可靠第三方研究<br>
    <span class="badge">云厂商</span><span class="badge">AI原生厂商</span><span class="badge">区域策略</span><span class="badge">商业模式</span>
  </div>
  <div class="audience">面向投资机构决策者与企业战略部门 · 公开资料研究</div>
</section>
<section class="toc-page">
  <h1>目录</h1>
  <nav class="toc">{toc}</nav>
</section>
<main class="report">{body}</main>
</body>
</html>"""

HTML_PATH.write_text(html_doc, encoding="utf-8")
HTML(filename=str(HTML_PATH), base_url=str(BASE)).write_pdf(str(PDF_PATH))
print(HTML_PATH)
print(PDF_PATH)
