cmvr-visual/scripts/render_manuals.py

61 lines
5.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from pathlib import Path
import html,re
root=Path(__file__).resolve().parents[1]
out=root/'frontend/public/help';out.mkdir(parents=True,exist_ok=True)
def inline(value):
value=html.escape(value)
value=re.sub(r'`([^`]+)`',r'<code>\1</code>',value)
value=re.sub(r'\*\*([^*]+)\*\*',r'<strong>\1</strong>',value)
def link(m):
dest=m[2]
if dest=='操作手册_20261111.md':dest='manual.html'
if dest=='鲲鹏长视频制作指南_20261111.md':dest='kunpeng.html'
if not (dest.startswith('https://') or dest in ['manual.html','kunpeng.html']):return m[1]
return '<a href="'+dest+'">'+m[1]+'</a>'
return re.sub(r'\[([^]]+)\]\(([^)]+)\)',link,value)
css='''body{margin:0;background:#f4f7f9;color:#1e293b;font:16px/1.85 "Microsoft YaHei",system-ui,sans-serif}a{color:#09685b}header{background:#112b32;color:#edf7f4;padding:28px max(5vw,20px)}header a{color:#a9ecda;margin-right:22px}main{max-width:1320px;margin:auto;display:grid;grid-template-columns:245px minmax(0,1fr);gap:30px;padding:28px}aside{align-self:start;position:sticky;top:20px;font-size:13px;max-height:90vh;overflow:auto}aside a{display:block;margin:7px 0}article{min-width:0;background:white;padding:36px;border-radius:12px;box-shadow:0 5px 30px #1330360a}h1{font-size:30px;line-height:1.5}h2{font-size:23px;margin-top:38px;border-bottom:1px solid #dbe5e9;padding-bottom:12px;scroll-margin-top:15px}h3{font-size:19px;margin-top:28px}p,li{overflow-wrap:anywhere}li{margin-bottom:10px}code{font:14px/1.6 Consolas,monospace;background:#edf2f5;padding:2px 5px;border-radius:4px;overflow-wrap:anywhere}pre{background:#102731;color:#d9ece7;padding:20px;overflow:auto;border-radius:8px}pre code{background:none;color:inherit;padding:0}blockquote{border-left:4px solid #60ae9a;margin:20px 0;padding:12px 20px;background:#eff7f4}.table-scroll{overflow:auto}table{border-collapse:collapse;width:100%;font-size:14px}th,td{border:1px solid #dbe5e9;padding:10px 12px;text-align:left;min-width:95px;vertical-align:top}th{background:#edf4f3}footer{margin-top:35px;color:#64748b;font-size:13px}@media(max-width:800px){main{display:block;padding:14px}aside{position:static;max-height:none;margin-bottom:20px}article{padding:20px}h1{font-size:25px}body{font-size:15px}}@media print{header,aside{display:none}main{display:block;padding:0}article{box-shadow:none;padding:0}h2{break-after:avoid}table{font-size:10px}a{color:inherit}}'''
for source,target,title in [('操作手册_20261111.md','manual','操作手册'),('鲲鹏长视频制作指南_20261111.md','kunpeng','鲲鹏长视频制作指南')]:
text=(root/'docs'/source).read_text(encoding='utf-8')
(out/(target+'.md')).write_text(text,encoding='utf-8')
lines=text.splitlines();parts=[];toc=[];i=0;seq=0;list_type=None
def close_list():
global list_type
if list_type:parts.append('</'+list_type+'>');list_type=None
while i<len(lines):
s=lines[i]
if s.startswith('```'):
close_list();block=[];i+=1
while i<len(lines) and not lines[i].startswith('```'):block.append(lines[i]);i+=1
parts.append('<pre><code>'+html.escape('\n'.join(block))+'</code></pre>');i+=1;continue
if s.startswith('|') and i+1<len(lines) and re.match(r'^\|[\s:|\-]+$',lines[i+1]):
close_list();rows=[];j=i
while j<len(lines) and lines[j].startswith('|'):
if j!=i+1:rows.append([x.strip() for x in lines[j].strip('|').split('|')])
j+=1
parts.append('<div class="table-scroll"><table><thead><tr>'+''.join('<th>'+inline(c)+'</th>' for c in rows[0])+'</tr></thead><tbody>')
for row in rows[1:]:parts.append('<tr>'+''.join('<td>'+inline(c)+'</td>' for c in row)+'</tr>')
parts.append('</tbody></table></div>');i=j;continue
heading=re.match(r'^(#{1,4}) (.+)$',s)
if heading:
close_list();level=len(heading[1]);seq+=1;key=f'section-{seq}'
parts.append(f'<h{level} id="{key}">'+inline(heading[2])+f'</h{level}>')
if level==2:toc.append(f'<a href="#{key}">'+inline(heading[2])+'</a>')
elif s.startswith('> '):close_list();parts.append('<blockquote>'+inline(s[2:])+'</blockquote>')
else:
item=re.match(r'^(?:([-]) |(\d+)\. )(.+)$',s)
if item:
kind='ul' if item[1] else 'ol'
if list_type!=kind:close_list();parts.append('<'+kind+'>');list_type=kind
parts.append('<li>'+inline(item[3])+'</li>')
else:
close_list()
if s.strip():parts.append('<p>'+inline(s)+'</p>')
i+=1
close_list()
page='<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>'+title+' · CMVR Visual</title><style>'+css+'</style></head><body><header><strong>CMVR Visual · 使用帮助</strong><nav><a href="/workflow">返回工作流</a><a href="manual.html">操作手册</a><a href="kunpeng.html">鲲鹏制作步骤</a><a href="'+target+'.md" download>下载Markdown</a><a href="/examples/kunpeng-storyboard.json" download>下载100秒分镜</a><a href="/examples/kunpeng-storyboard-45s.json" download>下载45秒分镜</a></nav></header><main><aside><strong>目录</strong>'+''.join(toc)+'</aside><article>'+''.join(parts)+'<footer>版本:2026-11-11 · 文档随本项目发布。浏览器可打印为PDF。</footer></article></main></body></html>'
(out/(target+'.html')).write_text(page,encoding='utf-8')
print('Generated 2 HTML manuals with Markdown downloads')