Compare commits

..

10 Commits

Author SHA1 Message Date
StarHeartHunt
f76546a232 🚨 re-enable noqa PYI061 2025-10-08 17:40:43 +08:00
pre-commit-ci[bot]
419190f23f 🚨 auto fix by pre-commit hooks 2025-10-08 09:30:51 +00:00
StarHeartHunt
815267c4fc 🔥 remove unused directives 2025-10-08 17:30:35 +08:00
StarHeartHunt
55bbabb0d5 strictly limit xfail range 2025-10-08 17:26:30 +08:00
StarHeartHunt
9cf6e770cf remove xfail for 313 2025-10-08 17:15:57 +08:00
dependabot[bot]
67e5126f6a ⬆️ Bump astral-sh/setup-uv from 6 to 7 in /.github/actions/setup-python in the actions group (#3710)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
2025-10-08 14:14:36 +08:00
noneflow[bot]
0212247694 📝 Update changelog 2025-10-08 03:41:31 +00:00
Nyuan Zhang
34aef36bac 📝 Docs: 添加 htmlkit 文档至最佳实践 (#3682)
Co-authored-by: StarHeart <starheart233@gmail.com>
2025-10-08 11:40:22 +08:00
pre-commit-ci[bot]
8ea2d4b81f ⬆️ auto update by pre-commit hooks (#3709)
Some checks failed
Code Coverage / Test Coverage (pydantic-v1, macos-latest, 3.10) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v1, windows-latest, 3.10) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v1, windows-latest, 3.11) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v1, windows-latest, 3.12) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v1, windows-latest, 3.13) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v1, windows-latest, 3.9) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v2, macos-latest, 3.10) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v2, macos-latest, 3.11) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v2, macos-latest, 3.12) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v2, macos-latest, 3.13) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v2, macos-latest, 3.9) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v2, windows-latest, 3.10) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v2, windows-latest, 3.11) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v2, windows-latest, 3.12) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v2, windows-latest, 3.13) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v2, windows-latest, 3.9) (push) Waiting to run
Code Coverage / Test Coverage (pydantic-v1, ubuntu-latest, 3.10) (push) Failing after 3s
Code Coverage / Test Coverage (pydantic-v1, ubuntu-latest, 3.11) (push) Failing after 3s
Code Coverage / Test Coverage (pydantic-v1, ubuntu-latest, 3.12) (push) Failing after 2s
Code Coverage / Test Coverage (pydantic-v1, ubuntu-latest, 3.13) (push) Failing after 2s
Code Coverage / Test Coverage (pydantic-v1, ubuntu-latest, 3.9) (push) Failing after 2s
Code Coverage / Test Coverage (pydantic-v2, ubuntu-latest, 3.10) (push) Failing after 2s
Code Coverage / Test Coverage (pydantic-v2, ubuntu-latest, 3.11) (push) Failing after 2s
Code Coverage / Test Coverage (pydantic-v2, ubuntu-latest, 3.12) (push) Failing after 2s
Code Coverage / Test Coverage (pydantic-v2, ubuntu-latest, 3.13) (push) Failing after 2s
Code Coverage / Test Coverage (pydantic-v2, ubuntu-latest, 3.9) (push) Failing after 2s
Pyright Lint / Pyright Lint (pydantic-v1) (push) Failing after 2s
Pyright Lint / Pyright Lint (pydantic-v2) (push) Failing after 2s
Ruff Lint / Ruff Lint (push) Failing after 2s
Site Deploy / publish (push) Failing after 2s
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-10-08 11:02:18 +08:00
noneflow[bot]
c9c50fa0eb 📝 Update changelog
Some checks failed
Code Coverage / Test Coverage (pydantic-v1, ubuntu-latest, 3.9) (push) Failing after 3s
Code Coverage / Test Coverage (pydantic-v2, ubuntu-latest, 3.10) (push) Failing after 3s
Code Coverage / Test Coverage (pydantic-v2, ubuntu-latest, 3.11) (push) Failing after 2s
Code Coverage / Test Coverage (pydantic-v2, ubuntu-latest, 3.12) (push) Failing after 2s
Code Coverage / Test Coverage (pydantic-v2, ubuntu-latest, 3.13) (push) Failing after 2s
Code Coverage / Test Coverage (pydantic-v2, ubuntu-latest, 3.9) (push) Failing after 2s
Pyright Lint / Pyright Lint (pydantic-v1) (push) Failing after 2s
Pyright Lint / Pyright Lint (pydantic-v2) (push) Failing after 2s
Ruff Lint / Ruff Lint (push) Failing after 2s
Site Deploy / publish (push) Failing after 2s
Code Coverage / Test Coverage (pydantic-v1, windows-latest, 3.13) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v1, windows-latest, 3.9) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v2, macos-latest, 3.10) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v2, macos-latest, 3.11) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v2, macos-latest, 3.12) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v2, macos-latest, 3.13) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v2, macos-latest, 3.9) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v1, macos-latest, 3.9) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v1, windows-latest, 3.10) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v1, windows-latest, 3.11) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v1, windows-latest, 3.12) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v1, macos-latest, 3.10) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v1, macos-latest, 3.11) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v1, macos-latest, 3.12) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v1, macos-latest, 3.13) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v2, windows-latest, 3.10) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v2, windows-latest, 3.11) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v2, windows-latest, 3.12) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v2, windows-latest, 3.13) (push) Has been cancelled
Code Coverage / Test Coverage (pydantic-v2, windows-latest, 3.9) (push) Has been cancelled
2025-10-06 09:32:21 +00:00
12 changed files with 954 additions and 50 deletions

View File

@@ -14,7 +14,7 @@ inputs:
runs:
using: "composite"
steps:
- uses: astral-sh/setup-uv@v6
- uses: astral-sh/setup-uv@v7
with:
python-version: ${{ inputs.python-version }}
cache-suffix: ${{ inputs.env-group }}

View File

@@ -7,7 +7,7 @@ ci:
autoupdate_commit_msg: ":arrow_up: auto update by pre-commit hooks"
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.11
rev: v0.14.0
hooks:
- id: ruff-check
args: [--fix]

View File

@@ -9750,7 +9750,7 @@
"color": "#199579"
}
],
"is_official": false
"is_official": true
},
{
"module_name": "nonebot_plugin_mhcodes",
@@ -9764,20 +9764,4 @@
],
"is_official": false
},
{
"module_name": "nonebot_plugin_comment_analysis",
"project_link": "nonebot-plugin-comment-analysis",
"author_id": 113231410,
"tags": [
{
"label": "ai分析",
"color": "#52c5ea"
},
{
"label": "弹幕评论",
"color": "#eb0e31"
}
],
"is_official": false
},
]

View File

@@ -89,7 +89,7 @@ def origin_is_annotated(origin: t.Optional[type[t.Any]]) -> bool:
return origin is t_ext.Annotated
NONE_TYPES = {None, type(None), t.Literal[None], t_ext.Literal[None]}
NONE_TYPES = {None, type(None), t.Literal[None], t_ext.Literal[None]} # noqa: PYI061
if sys.version_info >= (3, 10):
NONE_TYPES.add(types.NoneType)
@@ -104,6 +104,7 @@ if sys.version_info < (3, 12):
def is_type_alias_type(type_: type[t.Any]) -> bool:
"""判断是否是 TypeAliasType 类型"""
return isinstance(type_, t_ext.TypeAliasType)
else:
def is_type_alias_type(type_: type[t.Any]) -> bool:

View File

@@ -42,7 +42,7 @@ all = [
[dependency-groups]
dev = [
"ruff >=0.12.0, <0.13.0",
"ruff >=0.14.0, <0.15.0",
"nonemoji >=0.1.2, <0.2.0",
"pre-commit >=4.0.0, <5.0.0",
]

View File

@@ -43,7 +43,7 @@ UNKNOWN_PARAM = "Unknown parameter"
@pytest.mark.anyio
@pytest.mark.xfail(
(3, 13) <= sys.version_info < (3, 14),
((3, 13) <= sys.version_info < (3, 13, 8)) or (3, 14) <= sys.version_info,
reason="CPython Bug, see python/cpython#137317, python/cpython#137862",
)
async def test_depend(app: App):

57
uv.lock generated
View File

@@ -1,5 +1,5 @@
version = 1
revision = 2
revision = 3
requires-python = ">=3.9, <4.0"
resolution-markers = [
"python_full_version >= '3.10'",
@@ -1005,7 +1005,7 @@ name = "importlib-metadata"
version = "8.7.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "zipp" },
{ name = "zipp", marker = "python_full_version < '3.10' or (extra == 'group-8-nonebot2-pydantic-v1' and extra == 'group-8-nonebot2-pydantic-v2')" },
]
sdist = { url = "https://files.pythonhosted.org/packages/76/66/650a33bd90f786193e4de4b3ad86ea60b53c89b669a5c7be931fac31cdb0/importlib_metadata-8.7.0.tar.gz", hash = "sha256:d13b81ad223b890aa16c5471f2ac3056cf76c5f10f82d6f9292f0b415f389000", size = 56641, upload-time = "2025-04-27T15:29:01.736Z" }
wheels = [
@@ -1364,7 +1364,7 @@ provides-extras = ["websockets", "httpx", "aiohttp", "quart", "fastapi", "all"]
dev = [
{ name = "nonemoji", specifier = ">=0.1.2,<0.2.0" },
{ name = "pre-commit", specifier = ">=4.0.0,<5.0.0" },
{ name = "ruff", specifier = ">=0.12.0,<0.13.0" },
{ name = "ruff", specifier = ">=0.14.0,<0.15.0" },
]
docs = [{ name = "nb-autodoc", specifier = ">=1.0.0a5,<2.0.0" }]
pydantic-v1 = [{ name = "pydantic", specifier = ">=1.10.0,<2.0.0" }]
@@ -1745,10 +1745,10 @@ resolution-markers = [
"python_full_version < '3.10'",
]
dependencies = [
{ name = "annotated-types" },
{ name = "pydantic-core" },
{ name = "typing-extensions" },
{ name = "typing-inspection" },
{ name = "annotated-types", marker = "extra == 'group-8-nonebot2-pydantic-v2' or extra != 'group-8-nonebot2-pydantic-v1'" },
{ name = "pydantic-core", marker = "extra == 'group-8-nonebot2-pydantic-v2' or extra != 'group-8-nonebot2-pydantic-v1'" },
{ name = "typing-extensions", marker = "extra == 'group-8-nonebot2-pydantic-v2' or extra != 'group-8-nonebot2-pydantic-v1'" },
{ name = "typing-inspection", marker = "extra == 'group-8-nonebot2-pydantic-v2' or extra != 'group-8-nonebot2-pydantic-v1'" },
]
sdist = { url = "https://files.pythonhosted.org/packages/00/dd/4325abf92c39ba8623b5af936ddb36ffcfe0beae70405d456ab1fb2f5b8c/pydantic-2.11.7.tar.gz", hash = "sha256:d989c3c6cb79469287b1569f7447a17848c998458d49ebe294e975b9baf0f0db", size = 788350, upload-time = "2025-06-14T08:33:17.137Z" }
wheels = [
@@ -1760,7 +1760,7 @@ name = "pydantic-core"
version = "2.33.2"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "typing-extensions" },
{ name = "typing-extensions", marker = "extra == 'group-8-nonebot2-pydantic-v2' or extra != 'group-8-nonebot2-pydantic-v1'" },
]
sdist = { url = "https://files.pythonhosted.org/packages/ad/88/5f2260bdfae97aabf98f1778d43f69574390ad787afb646292a638c923d4/pydantic_core-2.33.2.tar.gz", hash = "sha256:7cb8bc3605c29176e1b105350d2e6474142d7c1bd1d9327c4a9bdb46bf827acc", size = 435195, upload-time = "2025-04-23T18:33:52.104Z" }
wheels = [
@@ -2029,27 +2029,28 @@ wheels = [
[[package]]
name = "ruff"
version = "0.12.7"
version = "0.14.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/a1/81/0bd3594fa0f690466e41bd033bdcdf86cba8288345ac77ad4afbe5ec743a/ruff-0.12.7.tar.gz", hash = "sha256:1fc3193f238bc2d7968772c82831a4ff69252f673be371fb49663f0068b7ec71", size = 5197814, upload-time = "2025-07-29T22:32:35.877Z" }
sdist = { url = "https://files.pythonhosted.org/packages/41/b9/9bd84453ed6dd04688de9b3f3a4146a1698e8faae2ceeccce4e14c67ae17/ruff-0.14.0.tar.gz", hash = "sha256:62ec8969b7510f77945df916de15da55311fade8d6050995ff7f680afe582c57", size = 5452071, upload-time = "2025-10-07T18:21:55.763Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/e1/d2/6cb35e9c85e7a91e8d22ab32ae07ac39cc34a71f1009a6f9e4a2a019e602/ruff-0.12.7-py3-none-linux_armv6l.whl", hash = "sha256:76e4f31529899b8c434c3c1dede98c4483b89590e15fb49f2d46183801565303", size = 11852189, upload-time = "2025-07-29T22:31:41.281Z" },
{ url = "https://files.pythonhosted.org/packages/63/5b/a4136b9921aa84638f1a6be7fb086f8cad0fde538ba76bda3682f2599a2f/ruff-0.12.7-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:789b7a03e72507c54fb3ba6209e4bb36517b90f1a3569ea17084e3fd295500fb", size = 12519389, upload-time = "2025-07-29T22:31:54.265Z" },
{ url = "https://files.pythonhosted.org/packages/a8/c9/3e24a8472484269b6b1821794141f879c54645a111ded4b6f58f9ab0705f/ruff-0.12.7-py3-none-macosx_11_0_arm64.whl", hash = "sha256:2e1c2a3b8626339bb6369116e7030a4cf194ea48f49b64bb505732a7fce4f4e3", size = 11743384, upload-time = "2025-07-29T22:31:59.575Z" },
{ url = "https://files.pythonhosted.org/packages/26/7c/458dd25deeb3452c43eaee853c0b17a1e84169f8021a26d500ead77964fd/ruff-0.12.7-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:32dec41817623d388e645612ec70d5757a6d9c035f3744a52c7b195a57e03860", size = 11943759, upload-time = "2025-07-29T22:32:01.95Z" },
{ url = "https://files.pythonhosted.org/packages/7f/8b/658798472ef260ca050e400ab96ef7e85c366c39cf3dfbef4d0a46a528b6/ruff-0.12.7-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:47ef751f722053a5df5fa48d412dbb54d41ab9b17875c6840a58ec63ff0c247c", size = 11654028, upload-time = "2025-07-29T22:32:04.367Z" },
{ url = "https://files.pythonhosted.org/packages/a8/86/9c2336f13b2a3326d06d39178fd3448dcc7025f82514d1b15816fe42bfe8/ruff-0.12.7-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:a828a5fc25a3efd3e1ff7b241fd392686c9386f20e5ac90aa9234a5faa12c423", size = 13225209, upload-time = "2025-07-29T22:32:06.952Z" },
{ url = "https://files.pythonhosted.org/packages/76/69/df73f65f53d6c463b19b6b312fd2391dc36425d926ec237a7ed028a90fc1/ruff-0.12.7-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:5726f59b171111fa6a69d82aef48f00b56598b03a22f0f4170664ff4d8298efb", size = 14182353, upload-time = "2025-07-29T22:32:10.053Z" },
{ url = "https://files.pythonhosted.org/packages/58/1e/de6cda406d99fea84b66811c189b5ea139814b98125b052424b55d28a41c/ruff-0.12.7-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:74e6f5c04c4dd4aba223f4fe6e7104f79e0eebf7d307e4f9b18c18362124bccd", size = 13631555, upload-time = "2025-07-29T22:32:12.644Z" },
{ url = "https://files.pythonhosted.org/packages/6f/ae/625d46d5164a6cc9261945a5e89df24457dc8262539ace3ac36c40f0b51e/ruff-0.12.7-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:5d0bfe4e77fba61bf2ccadf8cf005d6133e3ce08793bbe870dd1c734f2699a3e", size = 12667556, upload-time = "2025-07-29T22:32:15.312Z" },
{ url = "https://files.pythonhosted.org/packages/55/bf/9cb1ea5e3066779e42ade8d0cd3d3b0582a5720a814ae1586f85014656b6/ruff-0.12.7-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:06bfb01e1623bf7f59ea749a841da56f8f653d641bfd046edee32ede7ff6c606", size = 12939784, upload-time = "2025-07-29T22:32:17.69Z" },
{ url = "https://files.pythonhosted.org/packages/55/7f/7ead2663be5627c04be83754c4f3096603bf5e99ed856c7cd29618c691bd/ruff-0.12.7-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:e41df94a957d50083fd09b916d6e89e497246698c3f3d5c681c8b3e7b9bb4ac8", size = 11771356, upload-time = "2025-07-29T22:32:20.134Z" },
{ url = "https://files.pythonhosted.org/packages/17/40/a95352ea16edf78cd3a938085dccc55df692a4d8ba1b3af7accbe2c806b0/ruff-0.12.7-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:4000623300563c709458d0ce170c3d0d788c23a058912f28bbadc6f905d67afa", size = 11612124, upload-time = "2025-07-29T22:32:22.645Z" },
{ url = "https://files.pythonhosted.org/packages/4d/74/633b04871c669e23b8917877e812376827c06df866e1677f15abfadc95cb/ruff-0.12.7-py3-none-musllinux_1_2_i686.whl", hash = "sha256:69ffe0e5f9b2cf2b8e289a3f8945b402a1b19eff24ec389f45f23c42a3dd6fb5", size = 12479945, upload-time = "2025-07-29T22:32:24.765Z" },
{ url = "https://files.pythonhosted.org/packages/be/34/c3ef2d7799c9778b835a76189c6f53c179d3bdebc8c65288c29032e03613/ruff-0.12.7-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:a07a5c8ffa2611a52732bdc67bf88e243abd84fe2d7f6daef3826b59abbfeda4", size = 12998677, upload-time = "2025-07-29T22:32:27.022Z" },
{ url = "https://files.pythonhosted.org/packages/77/ab/aca2e756ad7b09b3d662a41773f3edcbd262872a4fc81f920dc1ffa44541/ruff-0.12.7-py3-none-win32.whl", hash = "sha256:c928f1b2ec59fb77dfdf70e0419408898b63998789cc98197e15f560b9e77f77", size = 11756687, upload-time = "2025-07-29T22:32:29.381Z" },
{ url = "https://files.pythonhosted.org/packages/b4/71/26d45a5042bc71db22ddd8252ca9d01e9ca454f230e2996bb04f16d72799/ruff-0.12.7-py3-none-win_amd64.whl", hash = "sha256:9c18f3d707ee9edf89da76131956aba1270c6348bfee8f6c647de841eac7194f", size = 12912365, upload-time = "2025-07-29T22:32:31.517Z" },
{ url = "https://files.pythonhosted.org/packages/4c/9b/0b8aa09817b63e78d94b4977f18b1fcaead3165a5ee49251c5d5c245bb2d/ruff-0.12.7-py3-none-win_arm64.whl", hash = "sha256:dfce05101dbd11833a0776716d5d1578641b7fddb537fe7fa956ab85d1769b69", size = 11982083, upload-time = "2025-07-29T22:32:33.881Z" },
{ url = "https://files.pythonhosted.org/packages/3a/4e/79d463a5f80654e93fa653ebfb98e0becc3f0e7cf6219c9ddedf1e197072/ruff-0.14.0-py3-none-linux_armv6l.whl", hash = "sha256:58e15bffa7054299becf4bab8a1187062c6f8cafbe9f6e39e0d5aface455d6b3", size = 12494532, upload-time = "2025-10-07T18:21:00.373Z" },
{ url = "https://files.pythonhosted.org/packages/ee/40/e2392f445ed8e02aa6105d49db4bfff01957379064c30f4811c3bf38aece/ruff-0.14.0-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:838d1b065f4df676b7c9957992f2304e41ead7a50a568185efd404297d5701e8", size = 13160768, upload-time = "2025-10-07T18:21:04.73Z" },
{ url = "https://files.pythonhosted.org/packages/75/da/2a656ea7c6b9bd14c7209918268dd40e1e6cea65f4bb9880eaaa43b055cd/ruff-0.14.0-py3-none-macosx_11_0_arm64.whl", hash = "sha256:703799d059ba50f745605b04638fa7e9682cc3da084b2092feee63500ff3d9b8", size = 12363376, upload-time = "2025-10-07T18:21:07.833Z" },
{ url = "https://files.pythonhosted.org/packages/42/e2/1ffef5a1875add82416ff388fcb7ea8b22a53be67a638487937aea81af27/ruff-0.14.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3ba9a8925e90f861502f7d974cc60e18ca29c72bb0ee8bfeabb6ade35a3abde7", size = 12608055, upload-time = "2025-10-07T18:21:10.72Z" },
{ url = "https://files.pythonhosted.org/packages/4a/32/986725199d7cee510d9f1dfdf95bf1efc5fa9dd714d0d85c1fb1f6be3bc3/ruff-0.14.0-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:e41f785498bd200ffc276eb9e1570c019c1d907b07cfb081092c8ad51975bbe7", size = 12318544, upload-time = "2025-10-07T18:21:13.741Z" },
{ url = "https://files.pythonhosted.org/packages/9a/ed/4969cefd53315164c94eaf4da7cfba1f267dc275b0abdd593d11c90829a3/ruff-0.14.0-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:30a58c087aef4584c193aebf2700f0fbcfc1e77b89c7385e3139956fa90434e2", size = 14001280, upload-time = "2025-10-07T18:21:16.411Z" },
{ url = "https://files.pythonhosted.org/packages/ab/ad/96c1fc9f8854c37681c9613d825925c7f24ca1acfc62a4eb3896b50bacd2/ruff-0.14.0-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:f8d07350bc7af0a5ce8812b7d5c1a7293cf02476752f23fdfc500d24b79b783c", size = 15027286, upload-time = "2025-10-07T18:21:19.577Z" },
{ url = "https://files.pythonhosted.org/packages/b3/00/1426978f97df4fe331074baf69615f579dc4e7c37bb4c6f57c2aad80c87f/ruff-0.14.0-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:eec3bbbf3a7d5482b5c1f42d5fc972774d71d107d447919fca620b0be3e3b75e", size = 14451506, upload-time = "2025-10-07T18:21:22.779Z" },
{ url = "https://files.pythonhosted.org/packages/58/d5/9c1cea6e493c0cf0647674cca26b579ea9d2a213b74b5c195fbeb9678e15/ruff-0.14.0-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:16b68e183a0e28e5c176d51004aaa40559e8f90065a10a559176713fcf435206", size = 13437384, upload-time = "2025-10-07T18:21:25.758Z" },
{ url = "https://files.pythonhosted.org/packages/29/b4/4cd6a4331e999fc05d9d77729c95503f99eae3ba1160469f2b64866964e3/ruff-0.14.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:eb732d17db2e945cfcbbc52af0143eda1da36ca8ae25083dd4f66f1542fdf82e", size = 13447976, upload-time = "2025-10-07T18:21:28.83Z" },
{ url = "https://files.pythonhosted.org/packages/3b/c0/ac42f546d07e4f49f62332576cb845d45c67cf5610d1851254e341d563b6/ruff-0.14.0-py3-none-manylinux_2_31_riscv64.whl", hash = "sha256:c958f66ab884b7873e72df38dcabee03d556a8f2ee1b8538ee1c2bbd619883dd", size = 13682850, upload-time = "2025-10-07T18:21:31.842Z" },
{ url = "https://files.pythonhosted.org/packages/5f/c4/4b0c9bcadd45b4c29fe1af9c5d1dc0ca87b4021665dfbe1c4688d407aa20/ruff-0.14.0-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:7eb0499a2e01f6e0c285afc5bac43ab380cbfc17cd43a2e1dd10ec97d6f2c42d", size = 12449825, upload-time = "2025-10-07T18:21:35.074Z" },
{ url = "https://files.pythonhosted.org/packages/4b/a8/e2e76288e6c16540fa820d148d83e55f15e994d852485f221b9524514730/ruff-0.14.0-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:4c63b2d99fafa05efca0ab198fd48fa6030d57e4423df3f18e03aa62518c565f", size = 12272599, upload-time = "2025-10-07T18:21:38.08Z" },
{ url = "https://files.pythonhosted.org/packages/18/14/e2815d8eff847391af632b22422b8207704222ff575dec8d044f9ab779b2/ruff-0.14.0-py3-none-musllinux_1_2_i686.whl", hash = "sha256:668fce701b7a222f3f5327f86909db2bbe99c30877c8001ff934c5413812ac02", size = 13193828, upload-time = "2025-10-07T18:21:41.216Z" },
{ url = "https://files.pythonhosted.org/packages/44/c6/61ccc2987cf0aecc588ff8f3212dea64840770e60d78f5606cd7dc34de32/ruff-0.14.0-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:a86bf575e05cb68dcb34e4c7dfe1064d44d3f0c04bbc0491949092192b515296", size = 13628617, upload-time = "2025-10-07T18:21:44.04Z" },
{ url = "https://files.pythonhosted.org/packages/73/e6/03b882225a1b0627e75339b420883dc3c90707a8917d2284abef7a58d317/ruff-0.14.0-py3-none-win32.whl", hash = "sha256:7450a243d7125d1c032cb4b93d9625dea46c8c42b4f06c6b709baac168e10543", size = 12367872, upload-time = "2025-10-07T18:21:46.67Z" },
{ url = "https://files.pythonhosted.org/packages/41/77/56cf9cf01ea0bfcc662de72540812e5ba8e9563f33ef3d37ab2174892c47/ruff-0.14.0-py3-none-win_amd64.whl", hash = "sha256:ea95da28cd874c4d9c922b39381cbd69cb7e7b49c21b8152b014bd4f52acddc2", size = 13464628, upload-time = "2025-10-07T18:21:50.318Z" },
{ url = "https://files.pythonhosted.org/packages/c6/2a/65880dfd0e13f7f13a775998f34703674a4554906167dce02daf7865b954/ruff-0.14.0-py3-none-win_arm64.whl", hash = "sha256:f42c9495f5c13ff841b1da4cb3c2a42075409592825dada7c5885c2c844ac730", size = 12565142, upload-time = "2025-10-07T18:21:53.577Z" },
]
[[package]]
@@ -2167,7 +2168,7 @@ name = "typing-inspection"
version = "0.4.1"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "typing-extensions" },
{ name = "typing-extensions", marker = "extra == 'group-8-nonebot2-pydantic-v2' or extra != 'group-8-nonebot2-pydantic-v1'" },
]
sdist = { url = "https://files.pythonhosted.org/packages/f8/b1/0c11f5058406b3af7609f121aaa6b609744687f1d158b3c3a5bf4cc94238/typing_inspection-0.4.1.tar.gz", hash = "sha256:6ae134cc0203c33377d43188d4064e9b357dba58cff3185f22924610e70a9d28", size = 75726, upload-time = "2025-05-21T18:55:23.885Z" }
wheels = [

View File

@@ -0,0 +1,229 @@
---
sidebar_position: 8
description: 轻量化 HTML 绘图
---
# 轻量化 HTML 绘图
图片是机器人交互中不可或缺的一部分,对于信息展示的直观性、美观性有很大的作用。
基于 PIL 直接绘制图片具有良好的性能和存储开销,但是难以调试、维护过程式的绘图代码。
使用浏览器渲染类插件可以方便地绘制网页,且能够直接通过 JS 对网页效果进行编程,但是它占用的存储和内存空间相对可观。
NoneBot 提供的 `nonebot-plugin-htmlkit` 提供了另一种基于 HTML 和 CSS 语法的轻量化绘图选择:它基于 `litehtml` 解析库,无须安装额外的依赖即可使用,没有进程间通信带来的额外开销,且在支持 `webp` `avif` 等丰富图片格式的前提下,安装用的 wheel 文件大小仅有约 10 MB。
作为粗略的性能参考,在一台 Ryzen 7 9700X 的 Windows 电脑上,渲染 [PEP 7](https://peps.python.org/pep-0007/) 的 HTML 页面(分辨率为 800x5788大小约 1.4MB,从本地文件系统读取 CSS大约需要 100ms每个渲染任务内存最高占用约为 40MB.
## 安装插件
在使用前请先安装 `nonebot-plugin-htmlkit` 插件至项目环境中,可参考[获取商店插件](../tutorial/store.mdx#安装插件)来了解并选择安装插件的方式。如:
在**项目目录**下执行以下命令:
```bash
nb plugin install nonebot-plugin-htmlkit
```
`nonebot-plugin-htmlkit` 插件目前兼容以下系统架构:
- Windows x64
- macOS arm64M-系列芯片)
- Linux x64 (非 Alpine 等 musl 系发行版)
- Linux arm64 (非 Alpine 等 musl 系发行版)
:::caution 访问网络内容
如果需要访问网络资源(如 http(s) 网页内容NoneBot 需要客户端型驱动器Forward。内置的驱动器有 `~httpx``~aiohttp`
详见[选择驱动器](../advanced/driver.md)。
:::
## 使用插件
### 加载插件
在使用本插件前同样需要使用 `require` 方法进行**加载**并**导入**需要使用的方法,可参考 [跨插件访问](../advanced/requiring.md) 一节进行了解,如:
```python
from nonebot import require
require("nonebot_plugin_htmlkit")
from nonebot_plugin_htmlkit import html_to_pic, md_to_pic, template_to_pic, text_to_pic
```
插件会自动使用[配置中的参数](#配置-fontconfig)初始化 `fontconfig` 以提供字体查找功能。
### 渲染 API
`nonebot-plugin-htmlkit` 主要提供以下**异步**渲染函数:
#### html_to_pic
```python
async def html_to_pic(
html: str,
*,
base_url: str = "",
dpi: float = 144.0,
max_width: float = 800.0,
device_height: float = 600.0,
default_font_size: float = 12.0,
font_name: str = "sans-serif",
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
lang: str = "zh",
culture: str = "CN",
img_fetch_fn: ImgFetchFn = combined_img_fetcher,
css_fetch_fn: CSSFetchFn = combined_css_fetcher,
urljoin_fn: Callable[[str, str], str] = urllib3.parse.urljoin,
) -> bytes:
...
```
最核心的渲染函数。
`base_url``urljoin_fn` 控制着传入 `image_fetch_fn``css_fetch_fn` 回调的 url 内容。
`allow_refit` 如果为真,渲染时会自动缩小产出图片的宽度到最适合的宽度,否则必定产出 `max_width` 宽度的图片。
`max_width``device_height` 会在 `@media` 判断中被使用。
`img_fetch_fn` 预期为一个异步可调用对象(函数),接收图片 url 并返回对应 url 的 jpeg 或 png 二进制数据(`bytes`),可在拒绝加载时返回 `None`.
`css_fetch_fn` 预期为一个异步可调用对象(函数),接收目标 CSS url 并返回对应 url 的 CSS 文本(`str`),可在拒绝加载时返回 `None`.
以下为辅助的封装函数,关键字参数若未特殊说明均与 `html_to_pic` 含义相同。
#### text_to_pic
```python
async def text_to_pic(
text: str,
css_path: str = "",
*,
max_width: int = 500,
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
) -> bytes:
...
```
可用于渲染多行文本。
`text` 会被放置于 `<div id="main" class="main-box"> <div class="text">` 中,可据此编写 CSS 来改变文本表现。
#### md_to_pic
```python
async def md_to_pic(
md: str = "",
md_path: str = "",
css_path: str = "",
*,
max_width: int = 500,
img_fetch_fn: ImgFetchFn = combined_img_fetcher,
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
) -> bytes:
...
```
可用于渲染 Markdown 文本。默认为 GitHub Markdown Light 风格,支持基于 `pygments` 的代码高亮。
`md``md_path` 二选一,前者设置时应为 Markdown 的文本,后者设置时应为指向 Markdown 文本文件的路径。
#### template_to_pic
```python
async def template_to_pic(
template_path: str | PathLike[str] | Sequence[str | PathLike[str]],
template_name: str,
templates: Mapping[Any, Any],
filters: None | Mapping[str, Any] = None,
*,
max_width: int = 500,
device_height: int = 600,
base_url: str | None = None,
img_fetch_fn: ImgFetchFn = combined_img_fetcher,
css_fetch_fn: CSSFetchFn = combined_css_fetcher,
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
) -> bytes:
...
```
渲染 jinja2 模板。
`template_path` 为 jinja2 环境的路径,`template_name` 是环境中要加载模板的名字,`templates` 为传入模板的参数,`filters` 为过滤器名 -> 自定义过滤器的映射。
### 控制外部资源获取
通过传入 `img_fetch_fn``css_fetch_fn`,我们可以在实际访问资源前进行审查,修改资源的来源,或是对 IO 操作进行缓存。
`img_fetch_fn` 预期为一个异步可调用对象(函数),接收图片 url 并返回对应 url 的 jpeg 或 png 二进制数据(`bytes`),可在拒绝加载时返回 `None`.
`css_fetch_fn` 预期为一个异步可调用对象(函数),接收目标 CSS url 并返回对应 url 的 CSS 文本(`str`),可在拒绝加载时返回 `None`.
如果你想要禁用外部资源加载/只从文件系统加载/只从网络加载,可以使用 `none_fetcher` `filesystem_***_fetcher` `network_***_fetcher`
默认的 fetcher 行为(对于 `file://` 从文件系统加载,其余从网络加载)位于 `combined_***_fetcher`,可以通过对其封装实现缓存等操作。
## 配置项
### 配置 fontconfig
`htmlkit` 使用 `fontconfig` 查找字体,请参阅 [`fontconfig 用户手册`](https://fontconfig.pages.freedesktop.org/fontconfig/fontconfig-user) 了解环境变量的具体含义、如何通过编写配置文件修改字体配置等。
#### fontconfig_file
- **类型**: `str | None`
- **默认值**: `None`
覆盖默认的配置文件路径。
#### fontconfig_path
- **类型**: `str | None`
- **默认值**: `None`
覆盖默认的配置目录。
#### fontconfig_sysroot
- **类型**: `str | None`
- **默认值**: `None`
覆盖默认的 sysroot。
#### fc_debug
- **类型**: `str | None`
- **默认值**: `None`
设置 Fontconfig 的 debug 级别。
#### fc_dbg_match_filter
- **类型**: `str | None`
- **默认值**: `None`
`FC_DEBUG` 设置为 `MATCH2` 时,过滤 debug 输出。
#### fc_lang
- **类型**: `str | None`
- **默认值**: `None`
设置默认语言,否则从 `LOCALE` 环境变量获取。
#### fontconfig_use_mmap
- **类型**: `str | None`
- **默认值**: `None`
是否使用 `mmap(2)` 读取字体缓存。

View File

@@ -9,6 +9,7 @@ toc_max_heading_level: 2
### 📝 文档
- Docs: 添加 htmlkit 文档至最佳实践 [@BlueGlassBlock](https://github.com/BlueGlassBlock) ([#3682](https://github.com/nonebot/nonebot2/pull/3682))
- Docs: 修复 userinfo 插件链接 [@XieXiLin2](https://github.com/XieXiLin2) ([#3660](https://github.com/nonebot/nonebot2/pull/3660))
- Docs: 升级 docusaurus 3.8.1 [@StarHeartHunt](https://github.com/StarHeartHunt) ([#3649](https://github.com/nonebot/nonebot2/pull/3649))
- Docs: 更新文档《手动创建项目》 [@Chen-Luan](https://github.com/Chen-Luan) ([#3623](https://github.com/nonebot/nonebot2/pull/3623))
@@ -22,6 +23,7 @@ toc_max_heading_level: 2
### 🍻 插件发布
- Plugin: 怪物猎人集会码插件 [@noneflow](https://github.com/noneflow) ([#3684](https://github.com/nonebot/nonebot2/pull/3684))
- Plugin: nonebot-plugin-htmlkit [@noneflow](https://github.com/noneflow) ([#3695](https://github.com/nonebot/nonebot2/pull/3695))
- Plugin: 言令 [@noneflow](https://github.com/noneflow) ([#3675](https://github.com/nonebot/nonebot2/pull/3675))
- Plugin: 算法比赛助手 [@noneflow](https://github.com/noneflow) ([#3672](https://github.com/nonebot/nonebot2/pull/3672))

View File

@@ -0,0 +1,229 @@
---
sidebar_position: 8
description: 轻量化 HTML 绘图
---
# 轻量化 HTML 绘图
图片是机器人交互中不可或缺的一部分,对于信息展示的直观性、美观性有很大的作用。
基于 PIL 直接绘制图片具有良好的性能和存储开销,但是难以调试、维护过程式的绘图代码。
使用浏览器渲染类插件可以方便地绘制网页,且能够直接通过 JS 对网页效果进行编程,但是它占用的存储和内存空间相对可观。
NoneBot 提供的 `nonebot-plugin-htmlkit` 提供了另一种基于 HTML 和 CSS 语法的轻量化绘图选择:它基于 `litehtml` 解析库,无须安装额外的依赖即可使用,没有进程间通信带来的额外开销,且在支持 `webp` `avif` 等丰富图片格式的前提下,安装用的 wheel 文件大小仅有约 10 MB。
作为粗略的性能参考,在一台 Ryzen 7 9700X 的 Windows 电脑上,渲染 [PEP 7](https://peps.python.org/pep-0007/) 的 HTML 页面(分辨率为 800x5788大小约 1.4MB,从本地文件系统读取 CSS大约需要 100ms每个渲染任务内存最高占用约为 40MB.
## 安装插件
在使用前请先安装 `nonebot-plugin-htmlkit` 插件至项目环境中,可参考[获取商店插件](../tutorial/store.mdx#安装插件)来了解并选择安装插件的方式。如:
在**项目目录**下执行以下命令:
```bash
nb plugin install nonebot-plugin-htmlkit
```
`nonebot-plugin-htmlkit` 插件目前兼容以下系统架构:
- Windows x64
- macOS arm64M-系列芯片)
- Linux x64 (非 Alpine 等 musl 系发行版)
- Linux arm64 (非 Alpine 等 musl 系发行版)
:::caution 访问网络内容
如果需要访问网络资源(如 http(s) 网页内容NoneBot 需要客户端型驱动器Forward。内置的驱动器有 `~httpx``~aiohttp`
详见[选择驱动器](../advanced/driver.md)。
:::
## 使用插件
### 加载插件
在使用本插件前同样需要使用 `require` 方法进行**加载**并**导入**需要使用的方法,可参考 [跨插件访问](../advanced/requiring.md) 一节进行了解,如:
```python
from nonebot import require
require("nonebot_plugin_htmlkit")
from nonebot_plugin_htmlkit import html_to_pic, md_to_pic, template_to_pic, text_to_pic
```
插件会自动使用[配置中的参数](#配置-fontconfig)初始化 `fontconfig` 以提供字体查找功能。
### 渲染 API
`nonebot-plugin-htmlkit` 主要提供以下**异步**渲染函数:
#### html_to_pic
```python
async def html_to_pic(
html: str,
*,
base_url: str = "",
dpi: float = 144.0,
max_width: float = 800.0,
device_height: float = 600.0,
default_font_size: float = 12.0,
font_name: str = "sans-serif",
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
lang: str = "zh",
culture: str = "CN",
img_fetch_fn: ImgFetchFn = combined_img_fetcher,
css_fetch_fn: CSSFetchFn = combined_css_fetcher,
urljoin_fn: Callable[[str, str], str] = urllib3.parse.urljoin,
) -> bytes:
...
```
最核心的渲染函数。
`base_url``urljoin_fn` 控制着传入 `image_fetch_fn``css_fetch_fn` 回调的 url 内容。
`allow_refit` 如果为真,渲染时会自动缩小产出图片的宽度到最适合的宽度,否则必定产出 `max_width` 宽度的图片。
`max_width``device_height` 会在 `@media` 判断中被使用。
`img_fetch_fn` 预期为一个异步可调用对象(函数),接收图片 url 并返回对应 url 的 jpeg 或 png 二进制数据(`bytes`),可在拒绝加载时返回 `None`.
`css_fetch_fn` 预期为一个异步可调用对象(函数),接收目标 CSS url 并返回对应 url 的 CSS 文本(`str`),可在拒绝加载时返回 `None`.
以下为辅助的封装函数,关键字参数若未特殊说明均与 `html_to_pic` 含义相同。
#### text_to_pic
```python
async def text_to_pic(
text: str,
css_path: str = "",
*,
max_width: int = 500,
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
) -> bytes:
...
```
可用于渲染多行文本。
`text` 会被放置于 `<div id="main" class="main-box"> <div class="text">` 中,可据此编写 CSS 来改变文本表现。
#### md_to_pic
```python
async def md_to_pic(
md: str = "",
md_path: str = "",
css_path: str = "",
*,
max_width: int = 500,
img_fetch_fn: ImgFetchFn = combined_img_fetcher,
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
) -> bytes:
...
```
可用于渲染 Markdown 文本。默认为 GitHub Markdown Light 风格,支持基于 `pygments` 的代码高亮。
`md``md_path` 二选一,前者设置时应为 Markdown 的文本,后者设置时应为指向 Markdown 文本文件的路径。
#### template_to_pic
```python
async def template_to_pic(
template_path: str | PathLike[str] | Sequence[str | PathLike[str]],
template_name: str,
templates: Mapping[Any, Any],
filters: None | Mapping[str, Any] = None,
*,
max_width: int = 500,
device_height: int = 600,
base_url: str | None = None,
img_fetch_fn: ImgFetchFn = combined_img_fetcher,
css_fetch_fn: CSSFetchFn = combined_css_fetcher,
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
) -> bytes:
...
```
渲染 jinja2 模板。
`template_path` 为 jinja2 环境的路径,`template_name` 是环境中要加载模板的名字,`templates` 为传入模板的参数,`filters` 为过滤器名 -> 自定义过滤器的映射。
### 控制外部资源获取
通过传入 `img_fetch_fn``css_fetch_fn`,我们可以在实际访问资源前进行审查,修改资源的来源,或是对 IO 操作进行缓存。
`img_fetch_fn` 预期为一个异步可调用对象(函数),接收图片 url 并返回对应 url 的 jpeg 或 png 二进制数据(`bytes`),可在拒绝加载时返回 `None`.
`css_fetch_fn` 预期为一个异步可调用对象(函数),接收目标 CSS url 并返回对应 url 的 CSS 文本(`str`),可在拒绝加载时返回 `None`.
如果你想要禁用外部资源加载/只从文件系统加载/只从网络加载,可以使用 `none_fetcher` `filesystem_***_fetcher` `network_***_fetcher`
默认的 fetcher 行为(对于 `file://` 从文件系统加载,其余从网络加载)位于 `combined_***_fetcher`,可以通过对其封装实现缓存等操作。
## 配置项
### 配置 fontconfig
`htmlkit` 使用 `fontconfig` 查找字体,请参阅 [`fontconfig 用户手册`](https://fontconfig.pages.freedesktop.org/fontconfig/fontconfig-user) 了解环境变量的具体含义、如何通过编写配置文件修改字体配置等。
#### fontconfig_file
- **类型**: `str | None`
- **默认值**: `None`
覆盖默认的配置文件路径。
#### fontconfig_path
- **类型**: `str | None`
- **默认值**: `None`
覆盖默认的配置目录。
#### fontconfig_sysroot
- **类型**: `str | None`
- **默认值**: `None`
覆盖默认的 sysroot。
#### fc_debug
- **类型**: `str | None`
- **默认值**: `None`
设置 Fontconfig 的 debug 级别。
#### fc_dbg_match_filter
- **类型**: `str | None`
- **默认值**: `None`
`FC_DEBUG` 设置为 `MATCH2` 时,过滤 debug 输出。
#### fc_lang
- **类型**: `str | None`
- **默认值**: `None`
设置默认语言,否则从 `LOCALE` 环境变量获取。
#### fontconfig_use_mmap
- **类型**: `str | None`
- **默认值**: `None`
是否使用 `mmap(2)` 读取字体缓存。

View File

@@ -0,0 +1,229 @@
---
sidebar_position: 8
description: 轻量化 HTML 绘图
---
# 轻量化 HTML 绘图
图片是机器人交互中不可或缺的一部分,对于信息展示的直观性、美观性有很大的作用。
基于 PIL 直接绘制图片具有良好的性能和存储开销,但是难以调试、维护过程式的绘图代码。
使用浏览器渲染类插件可以方便地绘制网页,且能够直接通过 JS 对网页效果进行编程,但是它占用的存储和内存空间相对可观。
NoneBot 提供的 `nonebot-plugin-htmlkit` 提供了另一种基于 HTML 和 CSS 语法的轻量化绘图选择:它基于 `litehtml` 解析库,无须安装额外的依赖即可使用,没有进程间通信带来的额外开销,且在支持 `webp` `avif` 等丰富图片格式的前提下,安装用的 wheel 文件大小仅有约 10 MB。
作为粗略的性能参考,在一台 Ryzen 7 9700X 的 Windows 电脑上,渲染 [PEP 7](https://peps.python.org/pep-0007/) 的 HTML 页面(分辨率为 800x5788大小约 1.4MB,从本地文件系统读取 CSS大约需要 100ms每个渲染任务内存最高占用约为 40MB.
## 安装插件
在使用前请先安装 `nonebot-plugin-htmlkit` 插件至项目环境中,可参考[获取商店插件](../tutorial/store.mdx#安装插件)来了解并选择安装插件的方式。如:
在**项目目录**下执行以下命令:
```bash
nb plugin install nonebot-plugin-htmlkit
```
`nonebot-plugin-htmlkit` 插件目前兼容以下系统架构:
- Windows x64
- macOS arm64M-系列芯片)
- Linux x64 (非 Alpine 等 musl 系发行版)
- Linux arm64 (非 Alpine 等 musl 系发行版)
:::caution 访问网络内容
如果需要访问网络资源(如 http(s) 网页内容NoneBot 需要客户端型驱动器Forward。内置的驱动器有 `~httpx``~aiohttp`
详见[选择驱动器](../advanced/driver.md)。
:::
## 使用插件
### 加载插件
在使用本插件前同样需要使用 `require` 方法进行**加载**并**导入**需要使用的方法,可参考 [跨插件访问](../advanced/requiring.md) 一节进行了解,如:
```python
from nonebot import require
require("nonebot_plugin_htmlkit")
from nonebot_plugin_htmlkit import html_to_pic, md_to_pic, template_to_pic, text_to_pic
```
插件会自动使用[配置中的参数](#配置-fontconfig)初始化 `fontconfig` 以提供字体查找功能。
### 渲染 API
`nonebot-plugin-htmlkit` 主要提供以下**异步**渲染函数:
#### html_to_pic
```python
async def html_to_pic(
html: str,
*,
base_url: str = "",
dpi: float = 144.0,
max_width: float = 800.0,
device_height: float = 600.0,
default_font_size: float = 12.0,
font_name: str = "sans-serif",
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
lang: str = "zh",
culture: str = "CN",
img_fetch_fn: ImgFetchFn = combined_img_fetcher,
css_fetch_fn: CSSFetchFn = combined_css_fetcher,
urljoin_fn: Callable[[str, str], str] = urllib3.parse.urljoin,
) -> bytes:
...
```
最核心的渲染函数。
`base_url``urljoin_fn` 控制着传入 `image_fetch_fn``css_fetch_fn` 回调的 url 内容。
`allow_refit` 如果为真,渲染时会自动缩小产出图片的宽度到最适合的宽度,否则必定产出 `max_width` 宽度的图片。
`max_width``device_height` 会在 `@media` 判断中被使用。
`img_fetch_fn` 预期为一个异步可调用对象(函数),接收图片 url 并返回对应 url 的 jpeg 或 png 二进制数据(`bytes`),可在拒绝加载时返回 `None`.
`css_fetch_fn` 预期为一个异步可调用对象(函数),接收目标 CSS url 并返回对应 url 的 CSS 文本(`str`),可在拒绝加载时返回 `None`.
以下为辅助的封装函数,关键字参数若未特殊说明均与 `html_to_pic` 含义相同。
#### text_to_pic
```python
async def text_to_pic(
text: str,
css_path: str = "",
*,
max_width: int = 500,
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
) -> bytes:
...
```
可用于渲染多行文本。
`text` 会被放置于 `<div id="main" class="main-box"> <div class="text">` 中,可据此编写 CSS 来改变文本表现。
#### md_to_pic
```python
async def md_to_pic(
md: str = "",
md_path: str = "",
css_path: str = "",
*,
max_width: int = 500,
img_fetch_fn: ImgFetchFn = combined_img_fetcher,
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
) -> bytes:
...
```
可用于渲染 Markdown 文本。默认为 GitHub Markdown Light 风格,支持基于 `pygments` 的代码高亮。
`md``md_path` 二选一,前者设置时应为 Markdown 的文本,后者设置时应为指向 Markdown 文本文件的路径。
#### template_to_pic
```python
async def template_to_pic(
template_path: str | PathLike[str] | Sequence[str | PathLike[str]],
template_name: str,
templates: Mapping[Any, Any],
filters: None | Mapping[str, Any] = None,
*,
max_width: int = 500,
device_height: int = 600,
base_url: str | None = None,
img_fetch_fn: ImgFetchFn = combined_img_fetcher,
css_fetch_fn: CSSFetchFn = combined_css_fetcher,
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
) -> bytes:
...
```
渲染 jinja2 模板。
`template_path` 为 jinja2 环境的路径,`template_name` 是环境中要加载模板的名字,`templates` 为传入模板的参数,`filters` 为过滤器名 -> 自定义过滤器的映射。
### 控制外部资源获取
通过传入 `img_fetch_fn``css_fetch_fn`,我们可以在实际访问资源前进行审查,修改资源的来源,或是对 IO 操作进行缓存。
`img_fetch_fn` 预期为一个异步可调用对象(函数),接收图片 url 并返回对应 url 的 jpeg 或 png 二进制数据(`bytes`),可在拒绝加载时返回 `None`.
`css_fetch_fn` 预期为一个异步可调用对象(函数),接收目标 CSS url 并返回对应 url 的 CSS 文本(`str`),可在拒绝加载时返回 `None`.
如果你想要禁用外部资源加载/只从文件系统加载/只从网络加载,可以使用 `none_fetcher` `filesystem_***_fetcher` `network_***_fetcher`
默认的 fetcher 行为(对于 `file://` 从文件系统加载,其余从网络加载)位于 `combined_***_fetcher`,可以通过对其封装实现缓存等操作。
## 配置项
### 配置 fontconfig
`htmlkit` 使用 `fontconfig` 查找字体,请参阅 [`fontconfig 用户手册`](https://fontconfig.pages.freedesktop.org/fontconfig/fontconfig-user) 了解环境变量的具体含义、如何通过编写配置文件修改字体配置等。
#### fontconfig_file
- **类型**: `str | None`
- **默认值**: `None`
覆盖默认的配置文件路径。
#### fontconfig_path
- **类型**: `str | None`
- **默认值**: `None`
覆盖默认的配置目录。
#### fontconfig_sysroot
- **类型**: `str | None`
- **默认值**: `None`
覆盖默认的 sysroot。
#### fc_debug
- **类型**: `str | None`
- **默认值**: `None`
设置 Fontconfig 的 debug 级别。
#### fc_dbg_match_filter
- **类型**: `str | None`
- **默认值**: `None`
`FC_DEBUG` 设置为 `MATCH2` 时,过滤 debug 输出。
#### fc_lang
- **类型**: `str | None`
- **默认值**: `None`
设置默认语言,否则从 `LOCALE` 环境变量获取。
#### fontconfig_use_mmap
- **类型**: `str | None`
- **默认值**: `None`
是否使用 `mmap(2)` 读取字体缓存。

View File

@@ -0,0 +1,229 @@
---
sidebar_position: 8
description: 轻量化 HTML 绘图
---
# 轻量化 HTML 绘图
图片是机器人交互中不可或缺的一部分,对于信息展示的直观性、美观性有很大的作用。
基于 PIL 直接绘制图片具有良好的性能和存储开销,但是难以调试、维护过程式的绘图代码。
使用浏览器渲染类插件可以方便地绘制网页,且能够直接通过 JS 对网页效果进行编程,但是它占用的存储和内存空间相对可观。
NoneBot 提供的 `nonebot-plugin-htmlkit` 提供了另一种基于 HTML 和 CSS 语法的轻量化绘图选择:它基于 `litehtml` 解析库,无须安装额外的依赖即可使用,没有进程间通信带来的额外开销,且在支持 `webp` `avif` 等丰富图片格式的前提下,安装用的 wheel 文件大小仅有约 10 MB。
作为粗略的性能参考,在一台 Ryzen 7 9700X 的 Windows 电脑上,渲染 [PEP 7](https://peps.python.org/pep-0007/) 的 HTML 页面(分辨率为 800x5788大小约 1.4MB,从本地文件系统读取 CSS大约需要 100ms每个渲染任务内存最高占用约为 40MB.
## 安装插件
在使用前请先安装 `nonebot-plugin-htmlkit` 插件至项目环境中,可参考[获取商店插件](../tutorial/store.mdx#安装插件)来了解并选择安装插件的方式。如:
在**项目目录**下执行以下命令:
```bash
nb plugin install nonebot-plugin-htmlkit
```
`nonebot-plugin-htmlkit` 插件目前兼容以下系统架构:
- Windows x64
- macOS arm64M-系列芯片)
- Linux x64 (非 Alpine 等 musl 系发行版)
- Linux arm64 (非 Alpine 等 musl 系发行版)
:::caution 访问网络内容
如果需要访问网络资源(如 http(s) 网页内容NoneBot 需要客户端型驱动器Forward。内置的驱动器有 `~httpx``~aiohttp`
详见[选择驱动器](../advanced/driver.md)。
:::
## 使用插件
### 加载插件
在使用本插件前同样需要使用 `require` 方法进行**加载**并**导入**需要使用的方法,可参考 [跨插件访问](../advanced/requiring.md) 一节进行了解,如:
```python
from nonebot import require
require("nonebot_plugin_htmlkit")
from nonebot_plugin_htmlkit import html_to_pic, md_to_pic, template_to_pic, text_to_pic
```
插件会自动使用[配置中的参数](#配置-fontconfig)初始化 `fontconfig` 以提供字体查找功能。
### 渲染 API
`nonebot-plugin-htmlkit` 主要提供以下**异步**渲染函数:
#### html_to_pic
```python
async def html_to_pic(
html: str,
*,
base_url: str = "",
dpi: float = 144.0,
max_width: float = 800.0,
device_height: float = 600.0,
default_font_size: float = 12.0,
font_name: str = "sans-serif",
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
lang: str = "zh",
culture: str = "CN",
img_fetch_fn: ImgFetchFn = combined_img_fetcher,
css_fetch_fn: CSSFetchFn = combined_css_fetcher,
urljoin_fn: Callable[[str, str], str] = urllib3.parse.urljoin,
) -> bytes:
...
```
最核心的渲染函数。
`base_url``urljoin_fn` 控制着传入 `image_fetch_fn``css_fetch_fn` 回调的 url 内容。
`allow_refit` 如果为真,渲染时会自动缩小产出图片的宽度到最适合的宽度,否则必定产出 `max_width` 宽度的图片。
`max_width``device_height` 会在 `@media` 判断中被使用。
`img_fetch_fn` 预期为一个异步可调用对象(函数),接收图片 url 并返回对应 url 的 jpeg 或 png 二进制数据(`bytes`),可在拒绝加载时返回 `None`.
`css_fetch_fn` 预期为一个异步可调用对象(函数),接收目标 CSS url 并返回对应 url 的 CSS 文本(`str`),可在拒绝加载时返回 `None`.
以下为辅助的封装函数,关键字参数若未特殊说明均与 `html_to_pic` 含义相同。
#### text_to_pic
```python
async def text_to_pic(
text: str,
css_path: str = "",
*,
max_width: int = 500,
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
) -> bytes:
...
```
可用于渲染多行文本。
`text` 会被放置于 `<div id="main" class="main-box"> <div class="text">` 中,可据此编写 CSS 来改变文本表现。
#### md_to_pic
```python
async def md_to_pic(
md: str = "",
md_path: str = "",
css_path: str = "",
*,
max_width: int = 500,
img_fetch_fn: ImgFetchFn = combined_img_fetcher,
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
) -> bytes:
...
```
可用于渲染 Markdown 文本。默认为 GitHub Markdown Light 风格,支持基于 `pygments` 的代码高亮。
`md``md_path` 二选一,前者设置时应为 Markdown 的文本,后者设置时应为指向 Markdown 文本文件的路径。
#### template_to_pic
```python
async def template_to_pic(
template_path: str | PathLike[str] | Sequence[str | PathLike[str]],
template_name: str,
templates: Mapping[Any, Any],
filters: None | Mapping[str, Any] = None,
*,
max_width: int = 500,
device_height: int = 600,
base_url: str | None = None,
img_fetch_fn: ImgFetchFn = combined_img_fetcher,
css_fetch_fn: CSSFetchFn = combined_css_fetcher,
allow_refit: bool = True,
image_format: Literal["png", "jpeg"] = "png",
jpeg_quality: int = 100,
) -> bytes:
...
```
渲染 jinja2 模板。
`template_path` 为 jinja2 环境的路径,`template_name` 是环境中要加载模板的名字,`templates` 为传入模板的参数,`filters` 为过滤器名 -> 自定义过滤器的映射。
### 控制外部资源获取
通过传入 `img_fetch_fn``css_fetch_fn`,我们可以在实际访问资源前进行审查,修改资源的来源,或是对 IO 操作进行缓存。
`img_fetch_fn` 预期为一个异步可调用对象(函数),接收图片 url 并返回对应 url 的 jpeg 或 png 二进制数据(`bytes`),可在拒绝加载时返回 `None`.
`css_fetch_fn` 预期为一个异步可调用对象(函数),接收目标 CSS url 并返回对应 url 的 CSS 文本(`str`),可在拒绝加载时返回 `None`.
如果你想要禁用外部资源加载/只从文件系统加载/只从网络加载,可以使用 `none_fetcher` `filesystem_***_fetcher` `network_***_fetcher`
默认的 fetcher 行为(对于 `file://` 从文件系统加载,其余从网络加载)位于 `combined_***_fetcher`,可以通过对其封装实现缓存等操作。
## 配置项
### 配置 fontconfig
`htmlkit` 使用 `fontconfig` 查找字体,请参阅 [`fontconfig 用户手册`](https://fontconfig.pages.freedesktop.org/fontconfig/fontconfig-user) 了解环境变量的具体含义、如何通过编写配置文件修改字体配置等。
#### fontconfig_file
- **类型**: `str | None`
- **默认值**: `None`
覆盖默认的配置文件路径。
#### fontconfig_path
- **类型**: `str | None`
- **默认值**: `None`
覆盖默认的配置目录。
#### fontconfig_sysroot
- **类型**: `str | None`
- **默认值**: `None`
覆盖默认的 sysroot。
#### fc_debug
- **类型**: `str | None`
- **默认值**: `None`
设置 Fontconfig 的 debug 级别。
#### fc_dbg_match_filter
- **类型**: `str | None`
- **默认值**: `None`
`FC_DEBUG` 设置为 `MATCH2` 时,过滤 debug 输出。
#### fc_lang
- **类型**: `str | None`
- **默认值**: `None`
设置默认语言,否则从 `LOCALE` 环境变量获取。
#### fontconfig_use_mmap
- **类型**: `str | None`
- **默认值**: `None`
是否使用 `mmap(2)` 读取字体缓存。