feat:基本完善了custom-post

This commit is contained in:
Azide
2022-07-05 22:11:07 +08:00
parent 6a04e9345d
commit 6da77e2404
2 changed files with 47 additions and 27 deletions
+7 -16
View File
@@ -1,4 +1,4 @@
from dataclasses import dataclass
from dataclasses import dataclass, field
from pathlib import Path
from nonebot.adapters.onebot.v11.message import Message, MessageSegment
@@ -11,8 +11,8 @@ from .abstract_post import AbstractPost, BasePost
@dataclass
class _CustomPost(BasePost):
message_segments: list[MessageSegment]
css_path: str = "" # 模板文件所用css路径
message_segments: list[MessageSegment] = field(default_factory=list)
css_path: str = None # 模板文件所用css路径
async def generate_text_messages(self) -> list[MessageSegment]:
return self.message_segments
@@ -21,9 +21,7 @@ class _CustomPost(BasePost):
require("nonebot_plugin_htmlrender")
from nonebot_plugin_htmlrender import md_to_pic
pic_bytes = await md_to_pic(
md=self._generate_md(), css_path=self._get_css_path()
)
pic_bytes = await md_to_pic(md=self._generate_md(), css_path=self.css_path)
return [MessageSegment.image(pic_bytes)]
def _generate_md(self) -> str:
@@ -31,7 +29,7 @@ class _CustomPost(BasePost):
for message_segment in self.message_segments:
if message_segment.type == "text":
md += "{}\n".format(message_segment.data.get("text", ""))
md += "{}<br>".format(message_segment.data.get("text", ""))
elif message_segment.type == "image":
try:
# 先尝试获取file的值,没有在尝试获取url的值,都没有则为空
@@ -49,15 +47,6 @@ class _CustomPost(BasePost):
return md
def _get_css_path(self):
"""返回css路径"""
css_path = (
str(Path(__file__).parent / "templates" / "custom_post.css")
if not self.css_path
else self.css_path
)
return css_path
@dataclass
class CustomPost(_CustomPost, AbstractPost):
@@ -68,6 +57,8 @@ class CustomPost(_CustomPost, AbstractPost):
理论上text部分可以直接使用markdown语法, 例如 ###123
注意:list中的每一个text都会被解释为独立的一行文字
最后使用htmlrender渲染为图片
"""