#!/usr/bin/env python3 """ 生成 HTML,图片使用相对路径 'images/xxx.png'(确保图片在发布目录的 images 子文件夹中) """ import os import re BASE_DIR = "/root/.openclaw/workspaces/yzr-yxl/projects/yu-zhi-ran/content/published/2026-04-14-上海阳台种菜一年" MD_FILE = os.path.join(BASE_DIR, "final-article.md") OUT_HTML = os.path.join(BASE_DIR, "上海阳台种菜一年_可复制.html") with open(MD_FILE, "r", encoding="utf-8") as f: content = f.read() # 替换图片为 HTML img 标签,保持相对路径 def replace_img(match): alt, path = match.groups() return f'{alt}' content = re.sub(r'!\[(.*?)\]\((images/.*?)\)', replace_img, content) # 转换 Markdown 为 HTML html_lines = [] for line in content.split('\n'): if line.startswith('# '): html_lines.append(f'

{line[2:]}

') elif line.startswith('## '): html_lines.append(f'

{line[3:]}

') elif line.startswith('### '): html_lines.append(f'

{line[4:]}

') elif line.startswith('---'): html_lines.append('
') elif line.startswith('> '): html_lines.append(f'
{line[2:]}
') elif re.match(r'^[-*] ', line): html_lines.append(f'
  • {line[2:]}
  • ') elif re.match(r'^\d+\. ', line): html_lines.append(f'
  • {line[line.find(". ")+2:]}
  • ') elif line.strip() == '': html_lines.append('
    ') else: # 处理行内粗体斜体 tmp = re.sub(r'\*\*(.*?)\*\*', r'\1', line) tmp = re.sub(r'\*(.*?)\*', r'\1', tmp) html_lines.append(f'

    {tmp}

    ') html = f''' 上海阳台种菜一年 {chr(10).join(html_lines)} ''' with open(OUT_HTML, "w", encoding="utf-8") as f: f.write(html) print(f"✅ HTML 已生成: {OUT_HTML}") print(f"📊 字符数: {len(content)}") print(f"🖼️ 图片路径: images/ (需与 HTML 同目录的 images 文件夹)")