#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""把选题档案 md 转成可直接打开的公网 html（只支持本文用到的几种结构）。"""
import re, html, sys

SRC = '/root/media-hub/topics/2026-11-04-下一条视频-WorkBuddy最好上手的PersonalAgent.md'
OUT = '/root/media-hub/export/拍摄/topic-next-video-1104.html'

lines = open(SRC, encoding='utf-8').read().split('\n')
out, i = [], 0
in_ul = in_ol = False

def close_lists():
    global in_ul, in_ol
    if in_ul:
        out.append('</ul>'); in_ul = False
    if in_ol:
        out.append('</ol>'); in_ol = False

def inline(s):
    s = html.escape(s)
    s = re.sub(r'\*\*(.+?)\*\*', r'<strong>\1</strong>', s)
    return s

while i < len(lines):
    ln = lines[i]
    s = ln.strip()
    if not s:
        close_lists(); i += 1; continue
    if s == '---':
        close_lists(); out.append('<hr>'); i += 1; continue
    if s.startswith('|'):
        close_lists()
        rows = []
        while i < len(lines) and lines[i].strip().startswith('|'):
            cells = [c.strip() for c in lines[i].strip().strip('|').split('|')]
            if not re.fullmatch(r'[-: ]+', ''.join(cells)):
                rows.append(cells)
            i += 1
        out.append('<table>')
        out.append('<thead><tr>' + ''.join('<th>%s</th>' % inline(c) for c in rows[0]) + '</tr></thead>')
        out.append('<tbody>')
        for r in rows[1:]:
            out.append('<tr>' + ''.join('<td>%s</td>' % inline(c) for c in r) + '</tr>')
        out.append('</tbody></table>')
        continue
    if s.startswith('### '):
        close_lists(); out.append('<h3>%s</h3>' % inline(s[4:])); i += 1; continue
    if s.startswith('## '):
        close_lists(); out.append('<h2>%s</h2>' % inline(s[3:])); i += 1; continue
    if s.startswith('# '):
        close_lists(); out.append('<h1>%s</h1>' % inline(s[2:])); i += 1; continue
    if s.startswith('> '):
        close_lists(); out.append('<p class="note">%s</p>' % inline(s[2:])); i += 1; continue
    if s.startswith('- '):
        if not in_ul:
            close_lists(); out.append('<ul>'); in_ul = True
        out.append('<li>%s</li>' % inline(s[2:])); i += 1; continue
    if re.match(r'^\d+\. ', s):
        if not in_ol:
            close_lists(); out.append('<ol>'); in_ol = True
        out.append('<li>%s</li>' % inline(re.sub(r'^\d+\. ', '', s))); i += 1; continue
    close_lists(); out.append('<p>%s</p>' % inline(s)); i += 1

body = '\n'.join(out)
page = '''<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>下一条视频 · 选题档案（Jeanna 原话照录）</title>
<style>
:root{--ink:#22201C;--ink2:#5C564C;--line:#DCD5C7;--bg:#FBF8F1;--card:#FFFDF8;--hl:#B4762A}
*{box-sizing:border-box}
body{margin:0;background:var(--bg);color:var(--ink);font:16px/1.85 "PingFang SC","Microsoft YaHei",system-ui,sans-serif}
.wrap{max-width:880px;margin:0 auto;padding:48px 26px 90px}
h1{font-size:29px;line-height:1.35;margin:0 0 10px;letter-spacing:.5px}
h2{font-size:20px;margin:44px 0 14px;padding-left:12px;border-left:4px solid var(--hl)}
h3{font-size:17px;margin:30px 0 10px;color:#2E2A22}
p{margin:0 0 14px;color:#2A2721}
.note{background:#F3ECDD;border-left:3px solid #C9A063;padding:10px 14px;color:var(--ink2);font-size:14.5px}
ul,ol{margin:0 0 16px;padding-left:24px}
li{margin:0 0 8px}
hr{border:0;border-top:1px dashed var(--line);margin:34px 0}
table{width:100%;border-collapse:collapse;margin:8px 0 22px;font-size:14.5px;background:var(--card)}
th,td{border:1px solid var(--line);padding:10px 12px;vertical-align:top;text-align:left}
th{background:#EFE7D7;font-weight:700;font-size:14px}
td:first-child{white-space:nowrap;font-weight:700;color:#3A3428;width:130px}
strong{color:#1F1C17}
@media print{body{background:#fff}.wrap{padding:0}}
</style></head>
<body><div class="wrap">
@@BODY@@
</div></body></html>
'''.replace('@@BODY@@', body)
open(OUT, 'w', encoding='utf-8').write(page)
print('ok', len(page.encode()))
