fix render encoding

This commit is contained in:
felinae98 2021-07-08 17:47:01 +08:00
parent 272ce58b09
commit 2d44bc5dd1
No known key found for this signature in database
GPG Key ID: 00C8B010587FF610
2 changed files with 11 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import asyncio
import base64
from html import escape
from typing import Awaitable, Callable, Optional
from urllib.parse import quote
@ -91,7 +92,8 @@ class Render(metaclass=Singleton):
lines = text.split('\n')
parsed_lines = list(map(lambda x: '<p>{}</p>'.format(escape(x)), lines))
html_text = '<div style="width:17em;padding:1em">{}</div>'.format(''.join(parsed_lines))
url = 'data:text/html,{}'.format(quote(html_text))
url = 'data:text/html;charset=UTF-8;base64,{}'.format(base64.b64encode(html_text.encode()).decode())
logger.info(url)
data = await self.render(url, target='div')
return data

View File

@ -10,5 +10,12 @@ if typing.TYPE_CHECKING:
@pytest.mark.render
async def test_render(plugin_module: 'nonebot_hk_reporter'):
render = plugin_module.utils.Render()
res = await render.text_to_pic('a\nbbbbbbbbbbbbbbbbbbbbbb\ncd')
res = await render.text_to_pic('''a\nbbbbbbbbbbbbbbbbbbbbbb\ncd
<h1>中文</h1>
VuePress 由两部分组成第一部分是一个极简静态网站生成器
(opens new window)它包含由 Vue 驱动的主题系统和插件 API另一个部分是为书写技术文档而优化的默认主题它的诞生初衷是为了支持 Vue 及其子项目的文档需求
每一个由 VuePress 生成的页面都带有预渲染好的 HTML也因此具有非常好的加载性能和搜索引擎优化SEO同时一旦页面被加载Vue 将接管这些静态内容并将其转换成一个完整的单页应用SPA其他的页面则会只在用户浏览到的时候才按需加载
''')
print(res)