feat: 新增 API 获取内容方式并优化微信发布流程
This commit is contained in:
24
step2.py
24
step2.py
@@ -37,16 +37,18 @@ class Step2Converter:
|
||||
参考: docs/step2.md
|
||||
"""
|
||||
|
||||
def __init__(self, log_callback: Optional[Callable[[str], None]] = None, css_scheme_id: Optional[str] = None):
|
||||
def __init__(self, log_callback: Optional[Callable[[str], None]] = None, css_scheme_id: Optional[str] = None, project_name: Optional[str] = None):
|
||||
"""
|
||||
初始化 Step2Converter
|
||||
|
||||
|
||||
Args:
|
||||
log_callback: 日志回调函数,用于将日志输出到 GUI
|
||||
css_scheme_id: CSS 样式方案 ID,可选
|
||||
project_name: 项目名称,可选,用于生成文章标题
|
||||
"""
|
||||
self.log_callback = log_callback
|
||||
self.css_scheme_id = css_scheme_id
|
||||
self.project_name = project_name
|
||||
self.theme_manager = ThemeManager()
|
||||
|
||||
def _log(self, message: str):
|
||||
@@ -84,12 +86,22 @@ class Step2Converter:
|
||||
# 创建编辑器实例,它会自动加载当前配置(包括已应用的 CSS 方案)
|
||||
editor = WechatMarkdownEditor()
|
||||
html_content = editor.render(md_content)
|
||||
|
||||
|
||||
# 提取标题(从 HTML 内容中提取 h1 标签)
|
||||
import re
|
||||
title_match = re.search(r'<h1[^>]*>(.*?)</h1>', html_content)
|
||||
title = title_match.group(1) if title_match else "无标题"
|
||||
|
||||
# 如果提供了项目名称,使用项目名称去掉编号作为标题,并添加作者
|
||||
if self.project_name:
|
||||
# 去掉编号,如"【A167】xxx" -> "xxx"
|
||||
match = re.search(r'】(.+)', self.project_name)
|
||||
if match:
|
||||
title = match.group(1)
|
||||
else:
|
||||
title = self.project_name
|
||||
# 添加作者 - 通过 publish_to_wechat 传入
|
||||
|
||||
html_content = re.sub(r'\n\s*\n+', '\n', html_content)
|
||||
html_content = re.sub(r'>\s*\n\s*<', '><', html_content)
|
||||
|
||||
@@ -134,7 +146,8 @@ class Step2Converter:
|
||||
content=html_content,
|
||||
appid=wechat_appid,
|
||||
appsecret=wechat_appsecret,
|
||||
thumb_image_url=cover_image_url
|
||||
thumb_image_url=cover_image_url,
|
||||
author="南音"
|
||||
)
|
||||
else:
|
||||
# 使用默认封面图片
|
||||
@@ -145,7 +158,8 @@ class Step2Converter:
|
||||
content=html_content,
|
||||
appid=wechat_appid,
|
||||
appsecret=wechat_appsecret,
|
||||
thumb_image_path=thumb_path
|
||||
thumb_image_path=thumb_path,
|
||||
author="南音"
|
||||
)
|
||||
if success:
|
||||
self._log("[Step2] 公众号草稿推送成功!")
|
||||
|
||||
Reference in New Issue
Block a user