|
@@ -110,12 +110,14 @@ def render_markdown(md_content: str) -> str:
|
|
|
return md.convert(md_content)
|
|
return md.convert(md_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
-def generate_static_page(post_id: str, title: str, html_body: str):
|
|
|
|
|
|
|
+def generate_static_page(post_id: str, title: str, html_body: str, date: str, thumbnail: str):
|
|
|
"""生成独立的静态 HTML 文件"""
|
|
"""生成独立的静态 HTML 文件"""
|
|
|
rendered = render_template(
|
|
rendered = render_template(
|
|
|
"post_template.html",
|
|
"post_template.html",
|
|
|
title=title,
|
|
title=title,
|
|
|
content=html_body,
|
|
content=html_body,
|
|
|
|
|
+ date=date,
|
|
|
|
|
+ thumbnail=thumbnail,
|
|
|
)
|
|
)
|
|
|
output_path = config.GENERATED_FOLDER / f"{post_id}.html"
|
|
output_path = config.GENERATED_FOLDER / f"{post_id}.html"
|
|
|
with open(output_path, "w", encoding="utf-8") as f:
|
|
with open(output_path, "w", encoding="utf-8") as f:
|
|
@@ -138,15 +140,13 @@ def index():
|
|
|
generated_path = config.GENERATED_FOLDER / f"{post_id}.html"
|
|
generated_path = config.GENERATED_FOLDER / f"{post_id}.html"
|
|
|
if generated_path.exists():
|
|
if generated_path.exists():
|
|
|
with open(generated_path, "r", encoding="utf-8") as f:
|
|
with open(generated_path, "r", encoding="utf-8") as f:
|
|
|
- # 提取 <article class="post-article"> 内的内容
|
|
|
|
|
html_content = f.read()
|
|
html_content = f.read()
|
|
|
- # 简单提取 body 内的内容(实际项目中可用 BeautifulSoup)
|
|
|
|
|
- start = html_content.find('<article class="post-article">')
|
|
|
|
|
- end = html_content.find("</article>")
|
|
|
|
|
- if start != -1 and end != -1:
|
|
|
|
|
- post["content"] = html_content[start + len('<article class="post-article">'):end]
|
|
|
|
|
- else:
|
|
|
|
|
- post["content"] = ""
|
|
|
|
|
|
|
+ soup = BeautifulSoup(html_content, "html.parser")
|
|
|
|
|
+ card_summary = soup.find("div", class_="card-summary")
|
|
|
|
|
+ if card_summary:
|
|
|
|
|
+ post["content"] = card_summary.decode_contents()
|
|
|
|
|
+ else:
|
|
|
|
|
+ post["content"] = ""
|
|
|
else:
|
|
else:
|
|
|
post["content"] = ""
|
|
post["content"] = ""
|
|
|
|
|
|
|
@@ -209,8 +209,11 @@ def upload():
|
|
|
# 提取缩略图
|
|
# 提取缩略图
|
|
|
thumbnail = extract_thumbnail(html_body)
|
|
thumbnail = extract_thumbnail(html_body)
|
|
|
|
|
|
|
|
|
|
+ # 生成日期
|
|
|
|
|
+ date_iso = datetime.now(timezone.utc).isoformat()
|
|
|
|
|
+
|
|
|
# 生成静态页面
|
|
# 生成静态页面
|
|
|
- generate_static_page(post_id, title, html_body)
|
|
|
|
|
|
|
+ generate_static_page(post_id, title, html_body, date_iso, thumbnail)
|
|
|
|
|
|
|
|
# 更新索引
|
|
# 更新索引
|
|
|
index = load_index()
|
|
index = load_index()
|
|
@@ -218,7 +221,7 @@ def upload():
|
|
|
{
|
|
{
|
|
|
"id": post_id,
|
|
"id": post_id,
|
|
|
"title": title,
|
|
"title": title,
|
|
|
- "date": datetime.now(timezone.utc).isoformat(),
|
|
|
|
|
|
|
+ "date": date_iso,
|
|
|
"summary": summary,
|
|
"summary": summary,
|
|
|
"thumbnail": thumbnail,
|
|
"thumbnail": thumbnail,
|
|
|
}
|
|
}
|