nonebot-plugin-htmlrender 0.3.5__tar.gz → 0.5.0__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 (49) hide show
  1. nonebot_plugin_htmlrender-0.5.0/.flake8 +3 -0
  2. nonebot_plugin_htmlrender-0.5.0/.github/workflows/codecov.yml +43 -0
  3. nonebot_plugin_htmlrender-0.5.0/.github/workflows/publish.yml +55 -0
  4. nonebot_plugin_htmlrender-0.5.0/.gitignore +129 -0
  5. nonebot_plugin_htmlrender-0.5.0/.python-version +1 -0
  6. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/PKG-INFO +18 -10
  7. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/README.md +8 -0
  8. nonebot_plugin_htmlrender-0.5.0/docs/example.md +119 -0
  9. nonebot_plugin_htmlrender-0.5.0/docs/html2pic.png +0 -0
  10. nonebot_plugin_htmlrender-0.5.0/docs/md2pic.png +0 -0
  11. nonebot_plugin_htmlrender-0.5.0/docs/template2pic.png +0 -0
  12. nonebot_plugin_htmlrender-0.5.0/docs/text2pic.png +0 -0
  13. nonebot_plugin_htmlrender-0.5.0/example/.env.dev +7 -0
  14. nonebot_plugin_htmlrender-0.5.0/example/bot.py +12 -0
  15. nonebot_plugin_htmlrender-0.5.0/example/plugins/render/__init__.py +144 -0
  16. nonebot_plugin_htmlrender-0.5.0/example/plugins/render/html2pic.html +31 -0
  17. nonebot_plugin_htmlrender-0.5.0/example/plugins/render/templates/progress.html.jinja2 +27 -0
  18. nonebot_plugin_htmlrender-0.5.0/example/plugins/render/utils.py +12 -0
  19. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/nonebot_plugin_htmlrender/__init__.py +3 -3
  20. nonebot_plugin_htmlrender-0.5.0/nonebot_plugin_htmlrender/browser.py +356 -0
  21. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/nonebot_plugin_htmlrender/config.py +7 -0
  22. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/nonebot_plugin_htmlrender/data_source.py +16 -0
  23. nonebot_plugin_htmlrender-0.5.0/nonebot_plugin_htmlrender/templates/katex/mhchem.min.js +1 -0
  24. nonebot_plugin_htmlrender-0.5.0/pyproject.toml +40 -0
  25. nonebot_plugin_htmlrender-0.5.0/tests/conftest.py +15 -0
  26. nonebot_plugin_htmlrender-0.5.0/tests/templates/markdown.css +112 -0
  27. nonebot_plugin_htmlrender-0.5.0/tests/templates/mystyle.css +7 -0
  28. nonebot_plugin_htmlrender-0.5.0/tests/templates/text.html +24 -0
  29. nonebot_plugin_htmlrender-0.5.0/uv.lock +1546 -0
  30. nonebot_plugin_htmlrender-0.3.5/nonebot_plugin_htmlrender/browser.py +0 -117
  31. nonebot_plugin_htmlrender-0.3.5/pyproject.toml +0 -44
  32. nonebot_plugin_htmlrender-0.3.5/tests/conftest.py +0 -10
  33. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/LICENSE +0 -0
  34. {nonebot_plugin_htmlrender-0.3.5/tests → nonebot_plugin_htmlrender-0.5.0/example/plugins/render}/templates/markdown.css +0 -0
  35. {nonebot_plugin_htmlrender-0.3.5/tests → nonebot_plugin_htmlrender-0.5.0/example/plugins/render}/templates/mystyle.css +0 -0
  36. {nonebot_plugin_htmlrender-0.3.5/tests → nonebot_plugin_htmlrender-0.5.0/example/plugins/render}/templates/text.html +0 -0
  37. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/nonebot_plugin_htmlrender/templates/github-markdown-light.css +0 -0
  38. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/nonebot_plugin_htmlrender/templates/katex/katex.min.b64_fonts.css +0 -0
  39. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/nonebot_plugin_htmlrender/templates/katex/katex.min.js +0 -0
  40. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/nonebot_plugin_htmlrender/templates/katex/mathtex-script-type.min.js +0 -0
  41. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/nonebot_plugin_htmlrender/templates/markdown.html +0 -0
  42. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/nonebot_plugin_htmlrender/templates/pygments-default.css +0 -0
  43. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/nonebot_plugin_htmlrender/templates/text.css +0 -0
  44. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/nonebot_plugin_htmlrender/templates/text.html +0 -0
  45. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/tests/__init__.py +0 -0
  46. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/tests/templates/progress.html.jinja2 +0 -0
  47. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/tests/test_htmlrender.py +0 -0
  48. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/tests/test_template_filter.png +0 -0
  49. {nonebot_plugin_htmlrender-0.3.5 → nonebot_plugin_htmlrender-0.5.0}/tests/utils.py +0 -0
@@ -0,0 +1,3 @@
1
+ [flake8]
2
+ max-line-length = 90
3
+ ignore= F401
@@ -0,0 +1,43 @@
1
+ name: Code Coverage
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - "master"
7
+ - "dev"
8
+ pull_request:
9
+ branches:
10
+ - "master"
11
+
12
+ jobs:
13
+ test:
14
+ name: Test
15
+ runs-on: ${{ matrix.os }}
16
+ strategy:
17
+ matrix:
18
+ python-version: ["3.10", "3.11", "3.12", "3.13"]
19
+ os: [ubuntu-latest, windows-latest, macos-latest]
20
+ fail-fast: false
21
+ env:
22
+ OS: ${{ matrix.os }}
23
+ PYTHON_VERSION: ${{ matrix.python-version }}
24
+ steps:
25
+ - name: Checkout
26
+ uses: actions/checkout@v4
27
+
28
+ - name: Install the latest version of uv
29
+ uses: astral-sh/setup-uv@v3
30
+ with:
31
+ enable-cache: true
32
+ python-version: ${{ matrix.python-version }}
33
+
34
+ - run: uv sync
35
+ shell: bash
36
+
37
+ - name: Run tests
38
+ run: uv run pytest -s
39
+
40
+ - name: Upload coverage to Codecov
41
+ uses: codecov/codecov-action@v2
42
+ with:
43
+ env_vars: OS,PYTHON_VERSION
@@ -0,0 +1,55 @@
1
+ on:
2
+ push:
3
+ tags:
4
+ - "*"
5
+ workflow_dispatch:
6
+
7
+ jobs:
8
+ pypi-publish:
9
+ name: Upload release to PyPI
10
+ runs-on: ubuntu-latest
11
+ environment: release
12
+ permissions:
13
+ # IMPORTANT: this permission is mandatory for trusted publishing
14
+ id-token: write
15
+ steps:
16
+ - name: Checkout
17
+ uses: actions/checkout@v4
18
+
19
+ - name: Install the latest version of uv
20
+ uses: astral-sh/setup-uv@v3
21
+ with:
22
+ enable-cache: true
23
+
24
+ - name: "Set up Python"
25
+ uses: actions/setup-python@v5
26
+ with:
27
+ python-version-file: ".python-version"
28
+
29
+ - run: uv sync
30
+ shell: bash
31
+
32
+ # - name: Get Version
33
+ # id: version
34
+ # run: |
35
+ # echo "VERSION=$(uvx pdm show --version)" >> $GITHUB_OUTPUT
36
+ # echo "TAG_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
37
+ # echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
38
+
39
+ # - name: Check Version
40
+ # if: steps.version.outputs.VERSION != steps.version.outputs.TAG_VERSION
41
+ # run: exit 1
42
+
43
+ - name: Build Package
44
+ run: uv build
45
+
46
+ - name: pypi-publish
47
+ uses: pypa/gh-action-pypi-publish@v1.11.0
48
+
49
+ # - name: Publish Package to PyPI
50
+ # run: uv publish
51
+
52
+ # - name: Publish Package to GitHub Release
53
+ # run: gh release create ${{ steps.version.outputs.TAG_NAME }} dist/*.tar.gz dist/*.whl -t "🔖 ${{ steps.version.outputs.TAG_NAME }}" --generate-notes
54
+ # env:
55
+ # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -0,0 +1,129 @@
1
+ *DS_Store*
2
+ test
3
+ # Byte-compiled / optimized / DLL files
4
+ __pycache__/
5
+ *.py[cod]
6
+ *$py.class
7
+
8
+ # C extensions
9
+ *.so
10
+
11
+ # Distribution / packaging
12
+ .Python
13
+ build/
14
+ develop-eggs/
15
+ dist/
16
+ downloads/
17
+ eggs/
18
+ .eggs/
19
+ lib/
20
+ lib64/
21
+ parts/
22
+ sdist/
23
+ var/
24
+ wheels/
25
+ pip-wheel-metadata/
26
+ share/python-wheels/
27
+ *.egg-info/
28
+ .installed.cfg
29
+ *.egg
30
+ MANIFEST
31
+
32
+ # PyInstaller
33
+ # Usually these files are written by a python script from a template
34
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
35
+ *.manifest
36
+ *.spec
37
+
38
+ # Installer logs
39
+ pip-log.txt
40
+ pip-delete-this-directory.txt
41
+
42
+ # Unit test / coverage reports
43
+ htmlcov/
44
+ .tox/
45
+ .nox/
46
+ .coverage
47
+ .coverage.*
48
+ .cache
49
+ nosetests.xml
50
+ coverage.xml
51
+ *.cover
52
+ *.py,cover
53
+ .hypothesis/
54
+ .pytest_cache/
55
+
56
+ # Translations
57
+ *.mo
58
+ *.pot
59
+
60
+ # Django stuff:
61
+ *.log
62
+ local_settings.py
63
+ db.sqlite3
64
+ db.sqlite3-journal
65
+
66
+ # Flask stuff:
67
+ instance/
68
+ .webassets-cache
69
+
70
+ # Scrapy stuff:
71
+ .scrapy
72
+
73
+ # Sphinx documentation
74
+ docs/_build/
75
+
76
+ # PyBuilder
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+
82
+ # IPython
83
+ profile_default/
84
+ ipython_config.py
85
+
86
+ # pipenv
87
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
88
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
89
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
90
+ # install all needed dependencies.
91
+ #Pipfile.lock
92
+
93
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
94
+ __pypackages__/
95
+
96
+ # Celery stuff
97
+ celerybeat-schedule
98
+ celerybeat.pid
99
+
100
+ # SageMath parsed files
101
+ *.sage.py
102
+
103
+ # Environments
104
+ .env
105
+ .venv
106
+ env/
107
+ venv/
108
+ ENV/
109
+ env.bak/
110
+ venv.bak/
111
+
112
+ # Spyder project settings
113
+ .spyderproject
114
+ .spyproject
115
+
116
+ # Rope project settings
117
+ .ropeproject
118
+
119
+ # mkdocs documentation
120
+ /site
121
+
122
+ # mypy
123
+ .mypy_cache/
124
+ .dmypy.json
125
+ dmypy.json
126
+
127
+ # Pyre type checker
128
+ .pyre/
129
+ .pdm-python
@@ -0,0 +1 @@
1
+ 3.12
@@ -1,8 +1,10 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: nonebot-plugin-htmlrender
3
- Version: 0.3.5
3
+ Version: 0.5.0
4
4
  Summary: 通过浏览器渲染图片
5
- Author-Email: kexue <xana278@foxmail.com>
5
+ Project-URL: Homepage, https://github.com/kexue-z/nonebot-plugin-htmlrender
6
+ Project-URL: Bug Tracker, https://github.com/kexue-z/nonebot-plugin-htmlrender/issues
7
+ Author-email: kexue <x@kexue-cloud.cn>
6
8
  License: MIT License
7
9
 
8
10
  Copyright (c) 2021 kexue
@@ -24,17 +26,15 @@ License: MIT License
24
26
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
27
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
28
  SOFTWARE.
27
- Project-URL: Homepage, https://github.com/kexue-z/nonebot-plugin-htmlrender
28
- Project-URL: Bug tracker, https://github.com/kexue-z/nonebot-plugin-htmlrender/issues
29
29
  Requires-Python: <4.0,>=3.9
30
- Requires-Dist: playwright>=1.17.2
31
- Requires-Dist: nonebot2>=2.2.0
30
+ Requires-Dist: aiofiles>=0.8.0
32
31
  Requires-Dist: jinja2>=3.0.3
33
32
  Requires-Dist: markdown>=3.3.6
34
- Requires-Dist: Pygments>=2.10.0
35
- Requires-Dist: python-markdown-math>=0.8
33
+ Requires-Dist: nonebot2>=2.2.0
34
+ Requires-Dist: playwright>=1.48.0
35
+ Requires-Dist: pygments>=2.10.0
36
36
  Requires-Dist: pymdown-extensions>=9.1
37
- Requires-Dist: aiofiles>=0.8.0
37
+ Requires-Dist: python-markdown-math>=0.8
38
38
  Description-Content-Type: text/markdown
39
39
 
40
40
  # nonebot-plugin-htmlrender
@@ -90,6 +90,14 @@ htmlrender_proxy_host = "http://127.0.0.1:7890"
90
90
  # 手动编辑可以直接使用系统自带浏览器而不用重新下载 chromium
91
91
  # 可不写
92
92
  htmlrender_browser_channel = ""
93
+
94
+ # 浏览器执行文件路径 可不写
95
+ htmlrender_browser_executable_path = ""
96
+
97
+ # 使用 cdp 连接现有浏览器
98
+ # 使用方法,自行启动浏览,在启动浏览器时添加参数 --remote-debugging-port=1234
99
+ # 可不写
100
+ htmlrender_connect_over_cdp = "http://127.0.0.1:1234"
93
101
  ```
94
102
 
95
103
  ## markdown 转 图片
@@ -51,6 +51,14 @@ htmlrender_proxy_host = "http://127.0.0.1:7890"
51
51
  # 手动编辑可以直接使用系统自带浏览器而不用重新下载 chromium
52
52
  # 可不写
53
53
  htmlrender_browser_channel = ""
54
+
55
+ # 浏览器执行文件路径 可不写
56
+ htmlrender_browser_executable_path = ""
57
+
58
+ # 使用 cdp 连接现有浏览器
59
+ # 使用方法,自行启动浏览,在启动浏览器时添加参数 --remote-debugging-port=1234
60
+ # 可不写
61
+ htmlrender_connect_over_cdp = "http://127.0.0.1:1234"
54
62
  ```
55
63
 
56
64
  ## markdown 转 图片
@@ -0,0 +1,119 @@
1
+ md2pic <div align="center">
2
+ <h1>html格式支持和居中</h1>
3
+ <img width="250" src="https://v2.nonebot.dev/logo.png"/>
4
+ <div>
5
+ html格式 图片支持
6
+ </div>
7
+ </div>
8
+
9
+ # 一级标题
10
+ ## 二级标题
11
+ ### 三级标题
12
+ #### 四级标题
13
+ ##### 五级标题
14
+ ###### 六级标题
15
+
16
+ # 文本
17
+ *斜体文本*
18
+
19
+ _斜体文本_
20
+
21
+ **粗体文本**
22
+
23
+ __粗体文本__
24
+
25
+ ***粗斜体文本***
26
+
27
+ ___粗斜体文本___
28
+
29
+ <s>删除线</s>
30
+
31
+ ~~删除线~~
32
+
33
+ <u>下划线</u>
34
+
35
+ ~小号~字体
36
+
37
+ emoji 😀😃😄😁😆😅
38
+
39
+ 列表
40
+
41
+ * 第一项
42
+ * 第二项
43
+ * 第三项
44
+
45
+ 1. 第一项
46
+ 2. 第二项
47
+ 3. 第三项
48
+
49
+ 任务列表
50
+
51
+ - [X] 第一项
52
+ - [ ] 第二项
53
+
54
+ # 嵌套
55
+ 1. 第一项:
56
+ - 第一项嵌套的第一个元素
57
+ - 第一项嵌套的第二个元素
58
+ 2. 第二项:
59
+ - 第二项嵌套的第一个元素
60
+ - 第二项嵌套的第二个元素
61
+
62
+ - [X] 任务 1
63
+ * [X] 任务 A
64
+ * [ ] 任务 B
65
+ + [x] 任务 a
66
+ + [ ] 任务 b
67
+ + [x] 任务 c
68
+ * [X] 任务 C
69
+ - [ ] 任务 2
70
+ - [ ] 任务 3
71
+
72
+ 分割线
73
+ ----
74
+
75
+ # 图片
76
+
77
+ - 必须指定宽度或大小 如 `250` 或 `100%`
78
+ ```html
79
+ <img width="20%" src="https://v2.nonebot.dev/logo.png"/>
80
+ ```
81
+
82
+ # html同款标签
83
+
84
+ 如 <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd>
85
+
86
+ # 引用
87
+
88
+ > 最外层
89
+ > > 第一层嵌套
90
+ > > > 第二层嵌套
91
+
92
+ # 代码
93
+
94
+ ```python
95
+ import this
96
+ ```
97
+ 行内代码 `print("nonebot")`
98
+
99
+ # 表格
100
+ | 左对齐 | 右对齐 | 居中对齐 |
101
+ | :-----| ----: | :----: |
102
+ | 单元格 | 单元格 | 单元格 |
103
+ | 单元格 | 单元格 | 单元格 |
104
+
105
+ # 数学公式
106
+
107
+ 单行公式
108
+
109
+ $$(1+x)^\alpha =1+\alpha x +\frac{\alpha (\alpha -1}{2!} x^2+\cdots+\frac{\alpha (\alpha - 1)\cdots(\alpha - n+1)}{n!}x^n+o(x^n)$$
110
+
111
+ `$$...$$`
112
+
113
+ 行内公式 $f'(x_0)=\lim_{x\rightarrow x_0} \frac{f(x)-f(x_0)}{\Delta x}$ 行内公式
114
+
115
+ `$...$`
116
+
117
+ # 不支持
118
+ - md 格式图片插入(必须使用html格式)
119
+ - 某些符号会被自动转换
@@ -0,0 +1,7 @@
1
+ HOST=127.0.0.1
2
+ PORT=9905
3
+ DEBUG=true
4
+ SUPERUSERS=["1"]
5
+ COMMAND_START=["/", ""]
6
+ COMMAND_SEP=["."]
7
+ FASTAPI_RELOAD_DIRS=["data/"]
@@ -0,0 +1,12 @@
1
+ import nonebot
2
+ from nonebot.adapters.onebot.v11 import Adapter as OneBotV11Adapter
3
+
4
+ nonebot.init()
5
+ driver = nonebot.get_driver()
6
+ driver.register_adapter(OneBotV11Adapter)
7
+
8
+ nonebot.load_plugins("plugins")
9
+
10
+
11
+ if __name__ == "__main__":
12
+ nonebot.run()
@@ -0,0 +1,144 @@
1
+ from nonebot import require
2
+ require("nonebot_plugin_htmlrender")
3
+ # 注意顺序,先require再 from ... import ...
4
+ # 注意顺序,先require再 from ... import ...
5
+ # 注意顺序,先require再 from ... import ...
6
+ from nonebot_plugin_htmlrender import (
7
+ text_to_pic,
8
+ md_to_pic,
9
+ template_to_pic,
10
+ get_new_page,
11
+ )
12
+ # 注意顺序,先require再 from ... import ...
13
+ # 注意顺序,先require再 from ... import ...
14
+ # 注意顺序,先require再 from ... import ...
15
+
16
+ from nonebot import on_command
17
+ from nonebot.adapters.onebot.v11 import Bot, MessageEvent, MessageSegment
18
+ from PIL import Image
19
+ import io
20
+
21
+ from .utils import count_to_color
22
+
23
+ # 纯文本转图片
24
+ text2pic = on_command("text2pic")
25
+
26
+
27
+ @text2pic.handle()
28
+ async def _text2pic(bot: Bot, event: MessageEvent):
29
+ msg = str(event.get_message())
30
+
31
+ # css_path 可选
32
+ # from pathlib import Path
33
+ # pic = await text_to_pic(
34
+ # text=msg, css_path=str(Path(__file__).parent / "templates" / "markdown.css")
35
+ # )
36
+
37
+ pic = await text_to_pic(text=msg)
38
+ a = Image.open(io.BytesIO(pic))
39
+ a.save("text2pic.png", format="PNG")
40
+ await text2pic.finish(MessageSegment.image(pic))
41
+
42
+
43
+ # 加载本地 html 方法
44
+ html2pic = on_command("html2pic")
45
+
46
+
47
+ @html2pic.handle()
48
+ async def _html2pic(bot: Bot, event: MessageEvent):
49
+ from pathlib import Path
50
+
51
+ # html 可使用本地资源
52
+ async with get_new_page(viewport={"width": 300, "height": 300}) as page:
53
+ await page.goto(
54
+ "file://" + (str(Path(__file__).parent / "html2pic.html")),
55
+ wait_until="networkidle",
56
+ )
57
+ pic = await page.screenshot(full_page=True, path="./html2pic.png")
58
+
59
+ await html2pic.finish(MessageSegment.image(pic))
60
+
61
+
62
+ # 使用 template2pic 加载模板
63
+ template2pic = on_command("template2pic")
64
+
65
+
66
+ @template2pic.handle()
67
+ async def _template2pic(bot: Bot, event: MessageEvent):
68
+ from pathlib import Path
69
+
70
+ text_list = ["1", "2", "3", "4"]
71
+ template_path = str(Path(__file__).parent / "templates")
72
+ template_name = "text.html"
73
+ # 设置模板
74
+ # 模板中本地资源地址需要相对于 base_url 或使用绝对路径
75
+ pic = await template_to_pic(
76
+ template_path=template_path,
77
+ template_name=template_name,
78
+ templates={"text_list": text_list},
79
+ pages={
80
+ "viewport": {"width": 600, "height": 300},
81
+ "base_url": f"file://{template_path}",
82
+ },
83
+ wait=2,
84
+ )
85
+
86
+ a = Image.open(io.BytesIO(pic))
87
+ a.save("template2pic.png", format="PNG")
88
+
89
+ await template2pic.finish(MessageSegment.image(pic))
90
+
91
+
92
+ # 使用自定义过滤器
93
+ template_filter = on_command("template_filter")
94
+
95
+
96
+ @template_filter.handle()
97
+ async def _():
98
+ from pathlib import Path
99
+
100
+ count_list = ["1", "2", "3", "4"]
101
+ template_path = str(Path(__file__).parent / "templates")
102
+ template_name = "progress.html.jinja2"
103
+
104
+ pic = await template_to_pic(
105
+ template_path=template_path,
106
+ template_name=template_name,
107
+ templates={"counts": count_list},
108
+ filters={"count_to_color": count_to_color},
109
+ pages={
110
+ "viewport": {"width": 600, "height": 300},
111
+ "base_url": f"file://{template_path}",
112
+ }
113
+ )
114
+
115
+ a = Image.open(io.BytesIO(pic))
116
+ a.save("template_filter.png", format="PNG")
117
+
118
+ await template_filter.finish(MessageSegment.image(pic))
119
+
120
+
121
+ # 使用 md2pic
122
+ md2pic = on_command("md2pic")
123
+
124
+
125
+ @md2pic.handle()
126
+ async def _md2pic(bot: Bot, event: MessageEvent):
127
+ from pathlib import Path
128
+
129
+ # 如果是直接获取消息内容 需要 unescape
130
+ from nonebot.adapters.onebot.v11 import unescape
131
+
132
+ msg = unescape(str(event.get_message()))
133
+
134
+ # css_path 可选
135
+ # pic = await md_to_pic(
136
+ # md=msg, css_path=str(Path(__file__).parent / "templates" / "markdown.css")
137
+ # )
138
+
139
+ pic = await md_to_pic(md=msg)
140
+
141
+ a = Image.open(io.BytesIO(pic))
142
+ a.save("md2pic.png", format="PNG")
143
+
144
+ await md2pic.finish(MessageSegment.image(pic))
@@ -0,0 +1,31 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+
4
+ <head>
5
+ <meta charset="utf-8">
6
+ <title>菜鸟教程(runoob.com)</title>
7
+ </head>
8
+
9
+ <body>
10
+
11
+ <div style="opacity:0.5;position:absolute;left:50px;width:300px;height:150px;background-color:#40B3DF"></div>
12
+
13
+ <div style="font-family:verdana;padding:20px;border-radius:10px;border:10px solid #EE872A;">
14
+
15
+ <div style="opacity:0.3;position:absolute;left:120px;width:100px;height:200px;background-color:#8AC007"></div>
16
+
17
+ <h3>Look! Styles and colors</h3>
18
+
19
+ <div style="letter-spacing:12px;">Manipulate Text</div>
20
+
21
+ <div style="color:#40B3DF;">Colors
22
+ <span style="background-color:#B4009E;color:#ffffff;">Boxes</span>
23
+ </div>
24
+
25
+ <div style="color:#000000;">and more...</div>
26
+
27
+ </div>
28
+
29
+ </body>
30
+
31
+ </html>
@@ -0,0 +1,27 @@
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>Example</title>
7
+
8
+ {# 使用 tailwindclss CDN(仅用于举例,不建议在生产环境中使用) #}
9
+ <script src="https://cdn.tailwindcss.com"></script>
10
+ </head>
11
+ <body>
12
+ {% for count in counts %}
13
+ <div>
14
+ <div class="mb-2 flex justify-between items-center">
15
+ <h3 class="text-sm font-semibold text-black">Count: {{ count }}</h3>
16
+ <span class="text-sm text-black">{{ count | count_to_color }}</span>
17
+ </div>
18
+ <div class="flex w-full h-2 bg-gray-200 rounded-full overflow-hidden" role="progressbar" aria-valuenow="{{ count }}" aria-valuemin="0" aria-valuemax="100">
19
+ <div
20
+ class="flex flex-col justify-center rounded-full overflow-hidden text-xs text-white text-center whitespace-nowrap transition duration-500"
21
+ style="width: 50%; background-color: {{ count | count_to_color }};"
22
+ >
23
+ </div>
24
+ </div>
25
+ {% endfor %}
26
+ </body>
27
+ </html>
@@ -0,0 +1,12 @@
1
+ from nonebot.log import logger
2
+
3
+ def count_to_color(count: str) -> str:
4
+ logger.debug(f"Filtering count: {count}")
5
+ if count == '1':
6
+ return "#facc15"
7
+ elif count == '2':
8
+ return "#f87171"
9
+ elif count == '3':
10
+ return "#c084fc"
11
+ else:
12
+ return "#60a5fa"
@@ -6,6 +6,7 @@ from .browser import (
6
6
  get_browser as get_browser,
7
7
  get_new_page as get_new_page,
8
8
  shutdown_browser as shutdown_browser,
9
+ init_browser as init_browser,
9
10
  )
10
11
  from .data_source import (
11
12
  capture_element as capture_element,
@@ -35,9 +36,8 @@ async def init(**kwargs):
35
36
  Returns:
36
37
  Browser: Browser
37
38
  """
38
- browser = await get_browser(**kwargs)
39
- logger.info("Browser Started.")
40
- return browser
39
+ logger.info("Browser Starting.")
40
+ return await init_browser(**kwargs)
41
41
 
42
42
 
43
43
  @driver.on_shutdown