#!/bin/bash # 生成最终 HTML(内联图片,跳过广告相关图片) python3 - << 'PYEOF' import os, re, base64 BASE_DIR = "/root/openclaw-workspace/projects/yu-zhi-ran/content/published/2026-04-14-上海阳台种菜一年" MD_FILE = os.path.join(BASE_DIR, "final-article-optimized.md") IMAGES_DIR = os.path.join(BASE_DIR, "images") OUT_HTML = os.path.join(BASE_DIR, "上海阳台种菜一年_发布版.html") SKIP_IMAGES = ["08-App截图.png"] with open(MD_FILE, "r", encoding="utf-8") as f: content = f.read() # 预加载图片 base64(排除跳过的) image_cache = {} for fname in os.listdir(IMAGES_DIR): if fname.endswith('.png') and fname not in SKIP_IMAGES: path = os.path.join(IMAGES_DIR, fname) with open(path, "rb") as fimg: image_cache[fname] = base64.b64encode(fimg.read()).decode('utf-8') # 替换图片 def replace_img(match): alt, fname = match.groups() key = os.path.basename(fname) if key in image_cache: return f'{alt}' else: return f'

[图片已省略: {alt}]

' content = re.sub(r'!\[(.*?)\]\((images/.*?)\)', replace_img, content) # MD → 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"🖼️ 内嵌图片: {len(image_cache)} 张(跳过 {len(SKIP_IMAGES)} 张)") print(f"💾 文件大小: {os.path.getsize(OUT_HTML)/1024:.1f} KB") PYEOF