diff --git a/nonebot_plugin_uma/__init__.py b/nonebot_plugin_uma/__init__.py index f8cc56b..2789d44 100644 --- a/nonebot_plugin_uma/__init__.py +++ b/nonebot_plugin_uma/__init__.py @@ -1,6 +1,7 @@ from nonebot.plugin import PluginMetadata, require require("nonebot_plugin_apscheduler") +require("nonebot_plugin_htmlrender") from . import __main__ as __main__ # noqa: E402 from .config import ConfigModel # noqa: E402 diff --git a/nonebot_plugin_uma/uma_skills/generate.py b/nonebot_plugin_uma/uma_skills/generate.py index adb98c1..a902120 100644 --- a/nonebot_plugin_uma/uma_skills/generate.py +++ b/nonebot_plugin_uma/uma_skills/generate.py @@ -1,11 +1,10 @@ -from io import BytesIO from typing import List, Tuple, Union from fuzzywuzzy import process from nonebot.adapters.onebot.v11 import MessageSegment -from pil_utils import BuildImage, Text2Image -from prettytable import PrettyTable +from nonebot_plugin_htmlrender import md_to_pic +from ..utils import md_escape from .data_source import SkillConfigDict, SkillInfoDict @@ -67,41 +66,39 @@ async def get_skill_info(skill_name: str, f_data: SkillConfigDict) -> str: # 生成图片 -def create_img(title: str, info_data: SkillInfoType) -> BytesIO: +async def create_img(title: str, info_data: SkillInfoType) -> bytes: if not info_data: raise ValueError("info_data is empty") - field_names = info_data[0][1].keys() - table = PrettyTable(field_names=field_names, title=title) - - for name, trans in info_data: - table.add_row( - [ - name, - trans["中文名"], - trans["稀有度"].replace("·", ""), - trans["颜色"], - trans["繁中译名"], - trans["条件限制"], - trans["技能数值"], - trans["持续时间"], - trans["评价分"], - trans["需要PT"], - trans["PT评价比"], - trans["触发条件"], - trans["技能类型"], - ], - ) - - space = 5 - table_text = Text2Image.from_text(table.get_string(), 20) - bg = BuildImage.new( - "RGB", - (table_text.width + space * 2, table_text.height + space * 2), - (255, 255, 255), + table_head = ["技能名", *list(info_data[0][1].keys())] + table_rows = [ + [ + name, + trans["中文名"], + trans["稀有度"].replace("·", ""), + trans["颜色"], + trans["繁中译名"], + trans["条件限制"], + trans["技能数值"], + trans["持续时间"], + trans["评价分"], + trans["需要PT"], + trans["PT评价比"], + trans["触发条件"], + trans["技能类型"], + ] + for name, trans in info_data + ] + + title = md_escape(title) + table_head_md = f"|{'|'.join(table_head)}|" + table_align_md = f"|{'|'.join([':-:' for _ in table_head])}|" + table_rows_md = "\n".join( + [f"|{'|'.join([md_escape(i) for i in row])}|" for row in table_rows], ) - table_text.draw_on_image(bg.image, (space, space)) - return bg.save("jpg") + extra_html = "" + md = f"# {title}\n\n{table_head_md}\n{table_align_md}\n{table_rows_md}\n\n{extra_html}" + return await md_to_pic(md, width=1800, type="jpeg") # 获取马娘技能列表 @@ -161,4 +158,4 @@ async def get_skill_list( name_list.append(name) title = f"检索:{' + '.join(name_list)} 的结果" - return MessageSegment.image(create_img(title, infos)) + return MessageSegment.image(await create_img(title, infos)) diff --git a/nonebot_plugin_uma/utils.py b/nonebot_plugin_uma/utils.py new file mode 100644 index 0000000..f75cf22 --- /dev/null +++ b/nonebot_plugin_uma/utils.py @@ -0,0 +1,6 @@ +import re + + +def md_escape(text: str) -> str: + pattern = r"([\\_*\[\](){}#+\-.!])" + return re.sub(pattern, r"\\\1", text) diff --git a/pyproject.toml b/pyproject.toml index a11dd8d..8dd0bd7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,19 +4,19 @@ authors = [ { name = "Perseus037", email = "1209228678@qq.com" }, { name = "student_2333", email = "lgc2333@126.com" }, ] -version = "0.1.0.dev1" +version = "0.1.0.dev2" description = "A ported version of azmiao/uma_plugin for NoneBot2." dependencies = [ "nonebot2>=2.1.0", "nonebot-adapter-onebot>=2.2.0", "nonebot-plugin-apscheduler>=0.3.0", + "nonebot-plugin-htmlrender>=0.2.2", "pydantic>=1.10.4,<2", "httpx>=0.25.0", "lxml>=4.9.3", "beautifulsoup4>=4.12.2", "pillow<10", "fuzzywuzzy>=0.18.0", - "prettytable>=3.9.0", "pil-utils>=0.1.8", ] requires-python = ">=3.8,<4.0"