#!/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''
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 re.match(r'^[-*] ', line): html_lines.append(f'
{tmp}
') html = f'''