Cateon Huo 9e5dcb3912
实现 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>
2024-08-04 18:39:12 +08:00

65 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from io import BytesIO
from pathlib import Path
from typing import TYPE_CHECKING, Literal
from nonebot_plugin_saa import Text, Image, MessageSegmentFactory
from nonebot_bison.theme import Theme
from nonebot_bison.utils import pic_merge, is_pics_mergable
if TYPE_CHECKING:
from nonebot_bison.post import Post
class BasicTheme(Theme):
"""最基本的主题
纯文本应为每个Post必定支持的Theme
"""
name: Literal["basic"] = "basic"
async def render(self, post: "Post") -> list[MessageSegmentFactory]:
text = ""
text += f"{post.title}\n\n" if post.title else ""
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 ""
rp_content = await rp.get_plain_content()
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:
urls.append(f"详情: {post.url}")
if urls:
text += "\n".join(urls)
client = await post.platform.ctx.get_client_for_static()
msgs: list[MessageSegmentFactory] = [Text(text)]
pics_group: list[list[str | bytes | Path | BytesIO]] = []
if post.images:
pics_group.append(post.images)
if rp and rp.images:
pics_group.append(rp.images)
for pics in pics_group:
if is_pics_mergable(pics):
pics = await pic_merge(list(pics), client)
msgs.extend(map(Image, pics))
return msgs