nonebot-plugin-htmlrender 0.3.3__tar.gz → 0.3.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/PKG-INFO +1 -1
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/data_source.py +16 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/pyproject.toml +2 -1
- nonebot_plugin_htmlrender-0.3.5/tests/conftest.py +10 -0
- nonebot_plugin_htmlrender-0.3.5/tests/templates/progress.html.jinja2 +13 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/test_htmlrender.py +33 -0
- nonebot_plugin_htmlrender-0.3.5/tests/test_template_filter.png +0 -0
- nonebot_plugin_htmlrender-0.3.5/tests/utils.py +10 -0
- nonebot_plugin_htmlrender-0.3.3/tests/conftest.py +0 -10
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/LICENSE +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/README.md +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/__init__.py +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/browser.py +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/config.py +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/github-markdown-light.css +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/katex/katex.min.b64_fonts.css +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/katex/katex.min.js +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/katex/mathtex-script-type.min.js +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/markdown.html +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/pygments-default.css +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/text.css +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/text.html +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/__init__.py +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/templates/markdown.css +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/templates/mystyle.css +0 -0
- {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/templates/text.html +0 -0
|
@@ -144,6 +144,7 @@ async def read_tpl(path: str) -> str:
|
|
|
144
144
|
async def template_to_html(
|
|
145
145
|
template_path: str,
|
|
146
146
|
template_name: str,
|
|
147
|
+
filters: Optional[Dict[str, Any]] = None,
|
|
147
148
|
**kwargs,
|
|
148
149
|
) -> str:
|
|
149
150
|
"""使用jinja2模板引擎通过html生成图片
|
|
@@ -151,6 +152,7 @@ async def template_to_html(
|
|
|
151
152
|
Args:
|
|
152
153
|
template_path (str): 模板路径
|
|
153
154
|
template_name (str): 模板名
|
|
155
|
+
filters (Optional[Dict[str, Any]]): 自定义过滤器
|
|
154
156
|
**kwargs: 模板内容
|
|
155
157
|
Returns:
|
|
156
158
|
str: html
|
|
@@ -160,6 +162,12 @@ async def template_to_html(
|
|
|
160
162
|
loader=jinja2.FileSystemLoader(template_path),
|
|
161
163
|
enable_async=True,
|
|
162
164
|
)
|
|
165
|
+
|
|
166
|
+
if filters:
|
|
167
|
+
for filter_name, filter_func in filters.items():
|
|
168
|
+
template_env.filters[filter_name] = filter_func
|
|
169
|
+
logger.debug(f"加载自定义过滤器 {filter_name}")
|
|
170
|
+
|
|
163
171
|
template = template_env.get_template(template_name)
|
|
164
172
|
|
|
165
173
|
return await template.render_async(**kwargs)
|
|
@@ -207,6 +215,7 @@ async def template_to_pic(
|
|
|
207
215
|
template_path: str,
|
|
208
216
|
template_name: str,
|
|
209
217
|
templates: Dict[Any, Any],
|
|
218
|
+
filters: Optional[Dict[str, Any]] = None,
|
|
210
219
|
pages: Optional[Dict[Any, Any]] = None,
|
|
211
220
|
wait: int = 0,
|
|
212
221
|
type: Literal["jpeg", "png"] = "png", # noqa: A002
|
|
@@ -219,6 +228,7 @@ async def template_to_pic(
|
|
|
219
228
|
template_path (str): 模板路径
|
|
220
229
|
template_name (str): 模板名
|
|
221
230
|
templates (Dict[Any, Any]): 模板内参数 如: {"name": "abc"}
|
|
231
|
+
filters (Optional[Dict[str, Any]]): 自定义过滤器
|
|
222
232
|
pages (Optional[Dict[Any, Any]]): 网页参数 Defaults to
|
|
223
233
|
{"base_url": f"file://{getcwd()}", "viewport": {"width": 500, "height": 10}}
|
|
224
234
|
wait (int, optional): 网页载入等待时间. Defaults to 0.
|
|
@@ -238,6 +248,12 @@ async def template_to_pic(
|
|
|
238
248
|
loader=jinja2.FileSystemLoader(template_path),
|
|
239
249
|
enable_async=True,
|
|
240
250
|
)
|
|
251
|
+
|
|
252
|
+
if filters:
|
|
253
|
+
for filter_name, filter_func in filters.items():
|
|
254
|
+
template_env.filters[filter_name] = filter_func
|
|
255
|
+
logger.debug(f"加载自定义过滤器 {filter_name}")
|
|
256
|
+
|
|
241
257
|
template = template_env.get_template(template_name)
|
|
242
258
|
|
|
243
259
|
return await html_to_pic(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "nonebot-plugin-htmlrender"
|
|
3
|
-
version = "0.3.
|
|
3
|
+
version = "0.3.5"
|
|
4
4
|
description = "通过浏览器渲染图片"
|
|
5
5
|
authors = [
|
|
6
6
|
{ name = "kexue", email = "xana278@foxmail.com" },
|
|
@@ -31,6 +31,7 @@ dev = [
|
|
|
31
31
|
"nonebug @ git+https://github.com/nonebot/nonebug.git",
|
|
32
32
|
"nonebot-adapter-onebot>=2.2.3",
|
|
33
33
|
"fastapi>=0.111.0",
|
|
34
|
+
"pillow>=10.4.0",
|
|
34
35
|
]
|
|
35
36
|
|
|
36
37
|
[tool.pdm.build]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>Test</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
{% for count in counts %}
|
|
10
|
+
<p style="width: 10px; height: 10px; background-color: {{ count | count_to_color}};"></p>
|
|
11
|
+
{% endfor %}
|
|
12
|
+
</body>
|
|
13
|
+
</html>
|
{nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/test_htmlrender.py
RENAMED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import pytest
|
|
2
|
+
|
|
3
|
+
from io import BytesIO
|
|
2
4
|
from nonebug import App
|
|
5
|
+
from PIL import Image, ImageChops
|
|
6
|
+
|
|
7
|
+
from .utils import count_to_color
|
|
3
8
|
|
|
4
9
|
|
|
5
10
|
@pytest.mark.asyncio
|
|
@@ -51,3 +56,31 @@ async def test_template_to_pic(app: App):
|
|
|
51
56
|
wait=2,
|
|
52
57
|
)
|
|
53
58
|
assert isinstance(img, bytes)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@pytest.mark.asyncio
|
|
62
|
+
async def test_template_filter(app: App):
|
|
63
|
+
from pathlib import Path
|
|
64
|
+
from nonebot_plugin_htmlrender import template_to_pic
|
|
65
|
+
|
|
66
|
+
count_list = ["1", "2", "3", "4"]
|
|
67
|
+
template_path = str(Path(__file__).parent / "templates")
|
|
68
|
+
template_name = "progress.html.jinja2"
|
|
69
|
+
|
|
70
|
+
image_byte = await template_to_pic(
|
|
71
|
+
template_path=template_path,
|
|
72
|
+
template_name=template_name,
|
|
73
|
+
templates={"counts": count_list},
|
|
74
|
+
filters={"count_to_color": count_to_color},
|
|
75
|
+
pages={
|
|
76
|
+
"viewport": {"width": 600, "height": 300},
|
|
77
|
+
"base_url": f"file://{template_path}",
|
|
78
|
+
}
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
test_image_path = Path(__file__).parent / "test_template_filter.png"
|
|
82
|
+
test_image = Image.open(test_image_path)
|
|
83
|
+
image = Image.open(BytesIO(initial_bytes=image_byte))
|
|
84
|
+
diff = ImageChops.difference(image, test_image)
|
|
85
|
+
assert diff.getbbox() is None
|
|
86
|
+
|
|
Binary file
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/templates/markdown.css
RENAMED
|
File without changes
|
{nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/templates/mystyle.css
RENAMED
|
File without changes
|
{nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/templates/text.html
RENAMED
|
File without changes
|