🐛 为各主题添加 repost 的渲染 (#505)

* 🐛 为各主题添加 repost 的渲染

*  调整渲染结果

*  更新测试
This commit is contained in:
Azide
2024-03-21 14:17:25 +08:00
committed by GitHub
parent c915b36499
commit 3d4dca3b21
5 changed files with 126 additions and 20 deletions
+16 -4
View File
@@ -20,15 +20,27 @@ class BasicTheme(Theme):
async def render(self, post: "Post") -> list[MessageSegmentFactory]:
text = ""
if post.title:
text += f"{post.title}\n\n"
text += f"{post.title}\n\n" if post.title else ""
text += post.content if len(post.content) < 500 else f"{post.content[:500]}..."
text += f"\n来源: {post.platform.name} {post.nickname or ''}\n"
if rp := post.repost:
text += f"\n--------------\n转发自 {rp.nickname or ''}:\n"
text += f"{rp.title}\n\n" if rp.title else ""
text += rp.content if len(rp.content) < 500 else f"{rp.content[:500]}..."
text += "\n--------------\n"
text += f"来源: {post.platform.name} {post.nickname or ''}\n"
urls: list[str] = []
if rp and rp.url:
urls.append(f"转发详情:{rp.url}")
if post.url:
text += f"详情: {post.url}"
urls.append(f"详情: {post.url}")
if urls:
text += "\n".join(urls)
msgs: list[MessageSegmentFactory] = [Text(text)]
if post.images:
+9 -2
View File
@@ -18,9 +18,16 @@ class BriefTheme(Theme):
if not post.title:
raise ThemeRenderUnsupportError("Post has no title")
text = f"{post.title}\n\n"
text += f"来源: {post.platform.name} {post.nickname or ''}\n"
text += f"来源: {post.platform.name} {post.nickname or ''}{' 的转发' if post.repost else ''}\n"
urls: list[str] = []
if (rp := post.repost) and rp.url:
urls.append(f"转发详情: {rp.url}")
if post.url:
text += f"详情: {post.url}"
urls.append(f"详情: {post.url}")
if urls:
text += "\n".join(urls)
msgs: list[MessageSegmentFactory] = [Text(text)]
if post.images:
+21 -9
View File
@@ -20,27 +20,39 @@ class Ht2iTheme(Theme):
need_browser: bool = True
async def _text_render(self, text: str):
from nonebot_plugin_htmlrender import text_to_pic
from nonebot_plugin_htmlrender import md_to_pic
try:
return Image(await text_to_pic(text))
return Image(await md_to_pic(text, width=400))
except Exception as e:
raise ThemeRenderError(f"渲染文本失败: {e}")
async def render(self, post: "Post"):
_text = ""
md_text = ""
if post.title:
_text += f"{post.title}\n\n"
md_text += f"## {post.title}\n\n" if post.title else ""
_text += post.content if len(post.content) < 500 else f"{post.content[:500]}..."
md_text += post.content if len(post.content) < 500 else f"{post.content[:500]}..."
md_text += "\n\n"
if rp := post.repost:
md_text += f"> 转发自 {f'**{rp.nickname}**' if rp.nickname else ''}: \n"
md_text += f"> {rp.title} \n" if rp.title else ""
md_text += "> \n> " + rp.content if len(rp.content) < 500 else f"{rp.content[:500]}..." + " \n"
md_text += "\n\n"
_text += f"\n来源: {post.platform.name} {post.nickname or ''}\n"
md_text += f"###### 来源: {post.platform.name} {post.nickname or ''}\n"
msgs: list[MessageSegmentFactory] = [await self._text_render(_text)]
msgs: list[MessageSegmentFactory] = [await self._text_render(md_text)]
urls: list[str] = []
if rp and rp.url:
urls.append(f"转发详情: {rp.url}")
if post.url:
msgs.append(Text(f"详情: {post.url}"))
urls.append(f"详情: {post.url}")
if urls:
msgs.append(Text("\n".join(urls)))
if post.images:
pics = post.images
if is_pics_mergable(pics):