实现 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
+14 -6
View File
@@ -10,6 +10,7 @@ from nonebot_plugin_saa import MessageSegmentFactory
from ..theme import theme_manager
from .abstract_post import AbstractPost
from ..plugin_config import plugin_config
from .protocol import PlainContentSupport
from ..theme.types import ThemeRenderError, ThemeRenderUnsupportError
if TYPE_CHECKING:
@@ -17,7 +18,7 @@ if TYPE_CHECKING:
@dataclass
class Post(AbstractPost):
class Post(AbstractPost, PlainContentSupport):
"""最通用的Post,理论上包含所有常用的数据
对于更特殊的需要,可以考虑另外实现一个Post
@@ -62,6 +63,12 @@ class Post(AbstractPost):
themes_by_priority.append("basic")
return themes_by_priority
async def get_content(self):
return self.content
async def get_plain_content(self):
return self.content
async def generate(self) -> list[MessageSegmentFactory]:
"""生成消息"""
themes = self.get_priority_themes()
@@ -95,12 +102,13 @@ class Post(AbstractPost):
来源: <Platform {self.platform.platform_name}>
"""
post_format += "附加信息:\n"
for field in fields(self):
if field.name in ("content", "platform", "repost"):
for cls_field in fields(self):
if cls_field.name in ("content", "platform", "repost"):
continue
value = getattr(self, field.name)
if value is not None:
post_format += f"- {field.name}: {aRepr.repr(value)}\n"
else:
value = getattr(self, cls_field.name)
if value is not None:
post_format += f"- {cls_field.name}: {aRepr.repr(value)}\n"
if self.repost:
post_format += "\n转发:\n"
+16
View File
@@ -0,0 +1,16 @@
from typing import Protocol, runtime_checkable
@runtime_checkable
class PlainContentSupport(Protocol):
async def get_plain_content(self) -> str: ...
@runtime_checkable
class HTMLContentSupport(Protocol):
async def get_html_content(self) -> str: ...
@runtime_checkable
class MarkdownContentSupport(Protocol):
async def get_markdown_content(self) -> str: ...