🎨 模板渲染自动转义设置为打开并提高代码质量

This commit is contained in:
洛水居室 2022-09-02 16:27:54 +08:00
parent 3eeb8db869
commit 48562a6a8e
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
5 changed files with 9 additions and 10 deletions

View File

@ -21,26 +21,25 @@ class TemplateService:
self._jinja2_env = {}
self._jinja2_template = {}
def get_template(self, package_path: str, template_name: str, auto_escape: bool = True) -> Template:
def get_template(self, package_path: str, template_name: str) -> Template:
if config.debug:
# DEBUG下 禁止复用 方便查看和修改模板
loader = PackageLoader(self._template_package_name, package_path)
jinja2_env = Environment(loader=loader, enable_async=True, autoescape=auto_escape)
jinja2_env = Environment(loader=loader, enable_async=True, autoescape=True)
jinja2_template = jinja2_env.get_template(template_name)
else:
jinja2_env: Environment = self._jinja2_env.get(package_path)
jinja2_template: Template = self._jinja2_template.get(package_path + template_name)
if jinja2_env is None:
loader = PackageLoader(self._template_package_name, package_path)
jinja2_env = Environment(loader=loader, enable_async=True, autoescape=auto_escape)
jinja2_env = Environment(loader=loader, enable_async=True, autoescape=True)
jinja2_template = jinja2_env.get_template(template_name)
self._jinja2_env[package_path] = jinja2_env
self._jinja2_template[package_path + template_name] = jinja2_template
return jinja2_template
async def render(self, template_path: str, template_name: str, template_data: dict,
viewport: ViewportSize, full_page: bool = True, auto_escape: bool = True,
evaluate: Optional[str] = None) -> bytes:
viewport: ViewportSize, full_page: bool = True, evaluate: Optional[str] = None) -> bytes:
"""
模板渲染成图片
:param template_path: 模板目录
@ -53,7 +52,7 @@ class TemplateService:
:return:
"""
start_time = time.time()
template = self.get_template(template_path, template_name, auto_escape)
template = self.get_template(template_path, template_name)
template_data["res_path"] = f"file://{self._current_dir}"
html = await template.render_async(**template_data)
Log.debug(f"{template_name} 模板渲染使用了 {str(time.time() - start_time)}")

View File

@ -130,7 +130,7 @@ class Gacha(BasePlugins):
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
# 因为 gacha_info["title"] 返回的是 HTML 标签 尝试关闭自动转义
png_data = await self.template_service.render('genshin/gacha', "gacha.html", data,
{"width": 1157, "height": 603}, False, auto_escape=True)
{"width": 1157, "height": 603}, False)
reply_message = await message.reply_photo(png_data)
if filters.ChatType.GROUPS.filter(message):

View File

@ -17,7 +17,7 @@ class Help:
"""帮助菜单"""
@inject
def __init__(self, template_service: TemplateService):
def __init__(self, template_service: TemplateService = None):
self.template_service = template_service
self.help_png = None
self.file_id = None

View File

@ -14,7 +14,7 @@ class Wiki(BasePlugins):
"""有关WIKI操作"""
@inject
def __init__(self, wiki_service: WikiService):
def __init__(self, wiki_service: WikiService = None):
self.wiki_service = wiki_service
@classmethod

View File

@ -14,7 +14,7 @@ class Admin:
"""有关BOT ADMIN处理"""
@inject
def __init__(self, bot_admin_service: BotAdminService):
def __init__(self, bot_admin_service: BotAdminService = None):
self.bot_admin_service = bot_admin_service
@classmethod