Files
yu-zhi-ran/automation/make_final_html.sh
T

88 lines
3.4 KiB
Bash

#!/bin/bash
# 生成最终 HTML(内联图片,跳过广告相关图片)
python3 - << 'PYEOF'
import os, re, base64
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-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'<img src="data:image/png;base64,{image_cache[key]}" alt="{alt}" style="max-width:100%; margin:20px 0; display:block;">'
else:
return f'<p>[图片已省略: {alt}]</p>'
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'<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"🖼️ 内嵌图片: {len(image_cache)} 张(跳过 {len(SKIP_IMAGES)} 张)")
print(f"💾 文件大小: {os.path.getsize(OUT_HTML)/1024:.1f} KB")
PYEOF