实现 Post.content 相关扩展协议 (#553)

*  `post` 新增 `get_content()` 方法

* 🚨 make linter happy

* 💄 auto fix by pre-commit hooks

* 🐛 fix: 调整函数使用

* 💄 auto fix by pre-commit hooks

*  转用函数处理文本

* 💄 auto fix by pre-commit hooks

* 🔨 使用`Dict`存储`content_handlers`

* 💄 auto fix by pre-commit hooks

* 🎨 fix

* :arts: 简化函数使用

* 🐛 移除`Theme`的过时参数

* 🗑️ 复用 `self.plain_content`

* 💄 auto fix by pre-commit hooks

*  注册式装饰器写法

* 💄 auto fix by pre-commit hooks

* 🐛 fix

* 💄 auto fix by pre-commit hooks

* :feat: 通用纯文本处理函数

* 💄 auto fix by pre-commit hooks

* :downgrade: 复用`==`处理标题

* 🎨 简化(?)写法

*  测试修复

* ♻️ via ContentSupport extensions

* 🐛 fix test

* 💄 auto fix by pre-commit hooks

* 🐛 for clean text

* 🐛 fix

* 💄 auto fix by pre-commit hooks

* fix: for xml

* 💄 auto fix by pre-commit hooks

* chore: art

* 💄 auto fix by pre-commit hooks

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Cateon Huo
2024-08-04 18:39:12 +08:00
committed by GitHub
parent 0c1012b0f4
commit 9e5dcb3912
18 changed files with 363 additions and 46 deletions
@@ -10,7 +10,7 @@ from nonebot_bison.theme.utils import web_embed_image
from nonebot_bison.theme import Theme, ThemeRenderError, ThemeRenderUnsupportError
if TYPE_CHECKING:
from nonebot_bison.post import Post
from nonebot_bison.platform.arknights import ArknightsPost
@dataclass
@@ -32,7 +32,7 @@ class ArknightsTheme(Theme):
template_path: Path = Path(__file__).parent / "templates"
template_name: str = "announce.html.jinja"
async def render(self, post: "Post"):
async def render(self, post: "ArknightsPost"):
from nonebot_plugin_htmlrender import template_to_pic
if not post.title:
@@ -49,10 +49,9 @@ class ArknightsTheme(Theme):
raise ThemeRenderUnsupportError(
f"图片类型错误, 期望 str | Path | bytes | BytesIO | None, 实际为 {type(banner)}"
)
ark_data = ArkData(
announce_title=text_fletten(post.title),
content=post.content,
content=await post.get_content(),
banner_image_url=banner,
)
+5 -2
View File
@@ -24,12 +24,15 @@ class BasicTheme(Theme):
text += f"{post.title}\n\n" if post.title else ""
text += post.content if len(post.content) < 500 else f"{post.content[:500]}..."
content = await post.get_plain_content()
text += content if len(content) < 500 else f"{content[:500]}..."
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]}..."
rp_content = await rp.get_plain_content()
text += rp_content if len(rp_content) < 500 else f"{rp_content[:500]}..."
text += "\n--------------\n"
@@ -95,7 +95,7 @@ class CeobeCanteenTheme(Theme):
if post.images:
images = await self.merge_pics(post.images, http_client)
content = CeoboContent(text=post.content)
content = CeoboContent(text=await post.get_content())
retweet: CeoboRetweet | None = None
if post.repost:
@@ -106,7 +106,9 @@ class CeobeCanteenTheme(Theme):
images.extend(repost_images)
repost_nickname = f"@{post.repost.nickname}:" if post.repost.nickname else ""
retweet = CeoboRetweet(image=repost_head_pic, content=post.repost.content, author=repost_nickname)
retweet = CeoboRetweet(
image=repost_head_pic, content=await post.repost.get_content(), author=repost_nickname
)
return (
CeobeCard(
+15 -2
View File
@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING, Literal
from nonebot_plugin_saa import Text, Image, MessageSegmentFactory
from nonebot_bison.theme import Theme, ThemeRenderError
from nonebot_bison.post.protocol import HTMLContentSupport
from nonebot_bison.utils import pic_merge, is_pics_mergable
if TYPE_CHECKING:
@@ -30,16 +31,28 @@ class Ht2iTheme(Theme):
raise ThemeRenderError(f"渲染文本失败: {e}")
async def render(self, post: "Post"):
md_text = ""
md_text += f"## {post.title}\n\n" if post.title else ""
md_text += post.content if len(post.content) < 500 else f"{post.content[:500]}..."
if isinstance(post, HTMLContentSupport):
content = await post.get_html_content()
else:
content = await post.get_content()
md_text += content if len(content) < 500 else f"{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"
if isinstance(rp, HTMLContentSupport):
rp_content = await rp.get_html_content()
else:
rp_content = await rp.get_content()
md_text += (
"> \n> " + rp_content if len(rp_content) < 500 else f"{rp_content[:500]}..." + " \n" # noqa: E501
) # noqa: E501
md_text += "\n\n"
md_text += f"###### 来源: {post.platform.name} {post.nickname or ''}\n"