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.
Files changed (26) hide show
  1. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/PKG-INFO +1 -1
  2. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/data_source.py +16 -0
  3. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/pyproject.toml +2 -1
  4. nonebot_plugin_htmlrender-0.3.5/tests/conftest.py +10 -0
  5. nonebot_plugin_htmlrender-0.3.5/tests/templates/progress.html.jinja2 +13 -0
  6. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/test_htmlrender.py +33 -0
  7. nonebot_plugin_htmlrender-0.3.5/tests/test_template_filter.png +0 -0
  8. nonebot_plugin_htmlrender-0.3.5/tests/utils.py +10 -0
  9. nonebot_plugin_htmlrender-0.3.3/tests/conftest.py +0 -10
  10. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/LICENSE +0 -0
  11. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/README.md +0 -0
  12. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/__init__.py +0 -0
  13. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/browser.py +0 -0
  14. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/config.py +0 -0
  15. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/github-markdown-light.css +0 -0
  16. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/katex/katex.min.b64_fonts.css +0 -0
  17. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/katex/katex.min.js +0 -0
  18. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/katex/mathtex-script-type.min.js +0 -0
  19. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/markdown.html +0 -0
  20. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/pygments-default.css +0 -0
  21. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/text.css +0 -0
  22. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/nonebot_plugin_htmlrender/templates/text.html +0 -0
  23. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/__init__.py +0 -0
  24. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/templates/markdown.css +0 -0
  25. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/templates/mystyle.css +0 -0
  26. {nonebot_plugin_htmlrender-0.3.3 → nonebot_plugin_htmlrender-0.3.5}/tests/templates/text.html +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: nonebot-plugin-htmlrender
3
- Version: 0.3.3
3
+ Version: 0.3.5
4
4
  Summary: 通过浏览器渲染图片
5
5
  Author-Email: kexue <xana278@foxmail.com>
6
6
  License: MIT License
@@ -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"
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,10 @@
1
+ import nonebot
2
+ import pytest
3
+ from nonebug import App
4
+
5
+
6
+ @pytest.fixture
7
+ def app():
8
+ nonebot.require("nonebot_plugin_htmlrender")
9
+
10
+ yield App()
@@ -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>
@@ -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
+
@@ -0,0 +1,10 @@
1
+ def count_to_color(count: str) -> str:
2
+ if count == '1':
3
+ return "#facc15"
4
+ elif count == '2':
5
+ return "#f87171"
6
+ elif count == '3':
7
+ return "#c084fc"
8
+ else:
9
+ return "#60a5fa"
10
+
@@ -1,10 +0,0 @@
1
- import pytest
2
- import nonebot
3
-
4
-
5
- @pytest.fixture(scope="session", autouse=True)
6
- def load_bot():
7
- # 加载插件
8
- import nonebot_plugin_htmlrender
9
-
10
- nonebot.load_plugin("nonebot_plugin_htmlrender")