d63074be71
- 所有自动化脚本、测试文件路径从旧工作区迁移到 /root/openclaw-workspace/projects/yu-zhi-ran/ - 保持项目独立于 OpenClaw 工作区管理 - 后端 run.sh 路径更新
75 lines
2.9 KiB
Python
75 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
生成 HTML,图片使用相对路径 'images/xxx.png'(确保图片在发布目录的 images 子文件夹中)
|
|
"""
|
|
|
|
import os
|
|
import re
|
|
|
|
BASE_DIR = "/root/openclaw-workspace/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'<img src="{path}" alt="{alt}" style="max-width:100%; margin:20px 0; display:block;">'
|
|
|
|
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'<h1>{line[2:]}</h1>')
|
|
elif line.startswith('## '):
|
|
html_lines.append(f'<h2>{line[3:]}</h2>')
|
|
elif line.startswith('### '):
|
|
html_lines.append(f'<h3>{line[4:]}</h3>')
|
|
elif line.startswith('---'):
|
|
html_lines.append('<hr style="border:none;border-top:2px dashed #ddd;margin:40px 0;">')
|
|
elif line.startswith('> '):
|
|
html_lines.append(f'<blockquote style="border-left:4px solid #4CAF50;background:#f9f9f9;padding:10px 20px;margin:20px 0;color:#666;">{line[2:]}</blockquote>')
|
|
elif re.match(r'^[-*] ', line):
|
|
html_lines.append(f'<li>{line[2:]}</li>')
|
|
elif re.match(r'^\d+\. ', line):
|
|
html_lines.append(f'<li>{line[line.find(". ")+2:]}</li>')
|
|
elif line.strip() == '':
|
|
html_lines.append('<br>')
|
|
else:
|
|
# 处理行内粗体斜体
|
|
tmp = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', line)
|
|
tmp = re.sub(r'\*(.*?)\*', r'<em>\1</em>', tmp)
|
|
html_lines.append(f'<p>{tmp}</p>')
|
|
|
|
html = f'''<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>上海阳台种菜一年</title>
|
|
<style>
|
|
body {{ font-family: "Microsoft YaHei", sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; line-height: 1.8; }}
|
|
h1 {{ font-size: 28px; border-bottom: 2px solid #4CAF50; padding-bottom: 10px; }}
|
|
h2 {{ font-size: 24px; margin-top: 40px; border-left: 4px solid #4CAF50; padding-left: 10px; }}
|
|
h3 {{ font-size: 20px; margin-top: 30px; color: #666; }}
|
|
img {{ max-width: 100%; height: auto; border-radius: 4px; margin: 20px 0; display: block; margin-left: auto; margin-right: auto; }}
|
|
blockquote {{ border-left: 4px solid #4CAF50; background: #f9f9f9; padding: 10px 20px; margin: 20px 0; color: #666; }}
|
|
li {{ margin-bottom: 8px; }}
|
|
p {{ margin-bottom: 16px; }}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
{chr(10).join(html_lines)}
|
|
</body>
|
|
</html>'''
|
|
|
|
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 文件夹)")
|