Skip to content

Commit da05f60

Browse files
✨ OSPP 2025: NoneBot HTML 图片渲染器 (#1)
1 parent 9194119 commit da05f60

File tree

116 files changed

+9833
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+9833
-1
lines changed

.clang-format

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
BasedOnStyle: LLVM
2+
IndentWidth: 4
3+
DerivePointerAlignment: false
4+
PointerAlignment: Left
5+
ColumnLimit: 88

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tests/** linguist-vendored

.github/workflows/build.yml

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
name: Build Wheels
2+
on:
3+
push:
4+
workflow_dispatch:
5+
pull_request:
6+
branches:
7+
- master
8+
9+
jobs:
10+
build:
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
name: [manylinux-x64, manylinux-arm64, windows-x64, macos-arm64]
15+
include:
16+
- before-build: ""
17+
container: null
18+
repair: ""
19+
activate: "source .venv/bin/activate"
20+
replace-name: ""
21+
pick-wheel: "$(ls ../plugin-htmlkit/wheelhouse/*.whl | head -n1)"
22+
- name: manylinux-x64
23+
os: ubuntu-latest
24+
container:
25+
image: quay.io/pypa/manylinux_2_34_x86_64
26+
repair: "auditwheel repair dist/*.whl -w wheelhouse"
27+
replace-name: "sed -i -e 's/name = \"nonebot-plugin-htmlkit\"/name = \"test-env\"/' pyproject.toml"
28+
- name: manylinux-arm64
29+
os: ubuntu-24.04-arm
30+
container:
31+
image: quay.io/pypa/manylinux_2_34_aarch64
32+
repair: "auditwheel repair dist/*.whl -w wheelhouse"
33+
replace-name: "sed -i -e 's/name = \"nonebot-plugin-htmlkit\"/name = \"test-env\"/' pyproject.toml"
34+
- name: windows-x64
35+
os: windows-2025
36+
activate: ".venv/Scripts/activate"
37+
before-build: "cp .venv/Scripts/python.exe .venv/Scripts/python3.exe" # workaround xmake check for python3 executable
38+
replace-name: (Get-Content pyproject.toml) -replace 'name = "nonebot-plugin-htmlkit"', 'name = "test-env"' | Set-Content pyproject.toml
39+
repair: "delvewheel repair -w wheelhouse dist/*.whl"
40+
pick-wheel: "(Get-ChildItem ..\\plugin-htmlkit\\wheelhouse\\*.whl | Select-Object -First 1).FullName"
41+
- name: macos-arm64
42+
os: macos-latest
43+
repair: "delocate-wheel -w wheelhouse dist/*.whl"
44+
replace-name: "sed -i '' -e 's/name = \"nonebot-plugin-htmlkit\"/name = \"test-env\"/' pyproject.toml"
45+
env:
46+
XMAKE_ROOT: 'y'
47+
MACOSX_DEPLOYMENT_TARGET: '11.0'
48+
XMAKE_CONFIG_MODE: release
49+
50+
name: Build on ${{ matrix.name }}
51+
runs-on: ${{ matrix.os }}
52+
container: ${{ matrix.container }}
53+
54+
steps:
55+
- name: Checkout code
56+
uses: actions/checkout@v5
57+
with:
58+
submodules: recursive
59+
- name: Install UV
60+
uses: astral-sh/setup-uv@v6
61+
- name: Install XMake
62+
uses: xmake-io/github-action-setup-xmake@v1
63+
with:
64+
xmake-version: latest
65+
package-cache: true
66+
package-cache-key: packages-${{ matrix.os }}-${{ hashFiles('xmake.lua', 'litehtml/**', '!litehtml/CMakeLists.txt') }}
67+
project-path: .
68+
actions-cache-folder: '.xmake-cache'
69+
actions-cache-key: xmake-${{ matrix.os }}-${{ hashFiles('xmake.lua') }}
70+
- name: Pin Python Version
71+
run: uv python pin 3.10.11
72+
- name: Build wheels
73+
run: |
74+
git config --global --add safe.directory '*'
75+
uv venv
76+
${{ matrix.before-build }}
77+
${{ matrix.activate }}
78+
uv sync --only-group build --no-install-workspace
79+
uv build
80+
${{ matrix.repair }}
81+
- name: Upload wheels
82+
uses: actions/upload-artifact@v4
83+
with:
84+
name: wheels-${{ matrix.os }}
85+
path: wheelhouse/*.whl
86+
- name: Build Unstripped Binaries
87+
run: |
88+
git config --global --add safe.directory '*'
89+
${{ matrix.activate }}
90+
xmake config -m releasedbg -y
91+
xmake build -vD core
92+
xmake install -o bindist
93+
- name: Upload Binaries
94+
uses: actions/upload-artifact@v4
95+
with:
96+
name: binaries-${{ matrix.os }}
97+
path: bindist/*
98+
- name: Run Test
99+
run: |
100+
cd ../
101+
mkdir test-env
102+
cd test-env
103+
uv python pin 3.13
104+
uv venv
105+
${{ matrix.activate }}
106+
cp -r ../plugin-htmlkit/tests/ ./tests
107+
cp ../plugin-htmlkit/pyproject.toml ./pyproject.toml
108+
${{ matrix.replace-name }}
109+
uv sync --group test --no-install-workspace
110+
uv add ${{ matrix.pick-wheel }} --no-install-workspace
111+
uv run pytest --output-img-dir ../plugin-htmlkit/tests_output
112+
- name: Upload Test Results
113+
if: always()
114+
uses: actions/upload-artifact@v4
115+
with:
116+
name: test-results-${{ matrix.os }}
117+
path: tests_output
118+
119+
sdist:
120+
name: Build Source Distribution
121+
runs-on: ubuntu-latest
122+
steps:
123+
- name: Checkout code
124+
uses: actions/checkout@v5
125+
with:
126+
submodules: recursive
127+
- name: Install UV
128+
uses: astral-sh/setup-uv@v6
129+
- name: Pin Python Version
130+
run: uv python pin 3.10.11
131+
- name: Build sdist
132+
run: |
133+
uv venv
134+
source .venv/bin/activate
135+
uv sync --only-group build --no-install-workspace
136+
uv build --sdist
137+
mkdir sdist-out
138+
cp dist/*.tar.gz sdist-out/
139+
- name: Upload sdist
140+
uses: actions/upload-artifact@v4
141+
with:
142+
name: sdist
143+
path: sdist-out/*
144+
145+
package:
146+
name: Package + Publish to PyPI
147+
runs-on: ubuntu-latest
148+
needs: [build, sdist]
149+
permissions:
150+
id-token: write # required for OIDC trusted publisher
151+
contents: read
152+
steps:
153+
- name: Download all wheels
154+
uses: actions/download-artifact@v4
155+
with:
156+
path: dist
157+
pattern: wheels-*
158+
merge-multiple: true
159+
- name: Download sdist
160+
uses: actions/download-artifact@v4
161+
with:
162+
name: sdist
163+
path: dist
164+
- name: Upload Artifacts
165+
uses: actions/upload-artifact@v4
166+
with:
167+
name: dist
168+
path: dist/*
169+
- name: Install UV
170+
uses: astral-sh/setup-uv@v6
171+
- name: Publish to PyPI
172+
if: startsWith(github.ref, 'refs/tags/')
173+
run: |
174+
uv publish --trusted-publishing always

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
.idea/**/usage.statistics.xml
1212
.idea/**/dictionaries
1313
.idea/**/shelf
14+
.idea/*
1415

1516
# AWS User-specific
1617
.idea/**/aws.xml
@@ -516,3 +517,15 @@ $RECYCLE.BIN/
516517
*.lnk
517518

518519
# End of https://www.toptal.com/developers/gitignore/api/node,linux,macos,python,windows,jetbrains,visualstudiocode
520+
521+
.xmake/
522+
523+
# Auto generated by xmake for better dev experience
524+
525+
compile_commands.json
526+
CMakeLists.txt
527+
528+
output/
529+
bindist/
530+
gitmodules.lock
531+
.fontconfig_cache/

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "litehtml"]
2+
path = litehtml
3+
url = https://github.com/BlueGlassBlock/litehtml

.pre-commit-config.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@ repos:
1515
- id: ruff-format
1616
stages: [pre-commit]
1717

18+
- repo: https://github.com/pre-commit/mirrors-clang-format
19+
rev: v21.1.1
20+
hooks:
21+
- id: clang-format
22+
args: [--style=file, -i, core/*.cpp, -i, core/*.h]
23+
stages: [pre-commit]
24+
1825
- repo: https://github.com/nonebot/nonemoji
1926
rev: v0.1.4
2027
hooks:
2128
- id: nonemoji
22-
stages: [prepare-commit-msg]
29+
stages: [prepare-commit-msg]

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.10.11

README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# nonebot-plugin-htmlkit
2+
3+
一个基于 [litehtml](https://github.com/litehtml/litehtml) 的轻量级 HTML 渲染插件。
4+
5+
## 特性
6+
7+
- 基于 [fontconfig](https://www.freedesktop.org/wiki/Software/fontconfig/) 的字体管理, 支持系统字体和自定义字体
8+
- 提供了 HTML,纯文本,markdown,和 Jinja2 模板渲染的快捷函数
9+
- 支持自定义图片和 CSS 的加载策略
10+
- 支持通过 CSS 控制样式
11+
- 支持自适应控制渲染宽度
12+
13+
## 安装
14+
15+
使用 [`nb-cli`](https://cli.nonebot.dev/) 安装:
16+
17+
```bash
18+
nb plugin install nonebot-plugin-htmlkit
19+
```
20+
21+
或者,使用你选择的 Python 包管理器工具安装 `nonebot-plugin-htmlkit` 即可。
22+
23+
## 使用
24+
25+
### API
26+
27+
```python
28+
from nonebot import require
29+
30+
require("nonebot_plugin_htmlkit")
31+
from nonebot_plugin_htmlkit import (
32+
text_to_pic,
33+
md_to_pic,
34+
template_to_pic,
35+
html_to_pic,
36+
)
37+
```
38+
39+
> [!CAUTION]
40+
> 注意:请先 `require("nonebot_plugin_htmlkit")` 后再 `import` 插件!!!
41+
42+
### 配置项
43+
44+
`plugin-htmlkit` 的配置项主要为 [fontconfig](https://www.freedesktop.org/wiki/Software/fontconfig/) 的相关配置。
45+
46+
对于 `FC/FONTCONFIG` 开头的配置项,请参考 [fontconfig 文档](https://www.freedesktop.org/software/fontconfig/fontconfig-user.html) 以了解更多。
47+
48+
```python
49+
# ===============================
50+
# Fontconfig 配置
51+
# ===============================
52+
53+
# FONTCONFIG_FILE
54+
# 用于覆盖默认的配置文件路径。
55+
FONTCONFIG_FILE: str
56+
57+
# FONTCONFIG_PATH
58+
# 用于覆盖默认的配置目录。
59+
FONTCONFIG_PATH: str
60+
61+
# FONTCONFIG_SYSROOT
62+
# 用于设置默认的 sysroot 目录。
63+
FONTCONFIG_SYSROOT: str
64+
65+
# FC_DEBUG
66+
# 用于输出详细的调试信息。
67+
# 详细见 fontconfig 文档。
68+
FC_DEBUG: str
69+
70+
# FC_DBG_MATCH_FILTER
71+
# 用于在调试时过滤特定模式。
72+
# 仅当 FC_DEBUG 设置为 MATCH2 时生效。
73+
FC_DBG_MATCH_FILTER: str
74+
75+
# FC_LANG
76+
# 用于指定查询时的默认语言(弱绑定)。
77+
# 如果未设置,则从当前 locale 推导。
78+
FC_LANG: str
79+
80+
# FONTCONFIG_USE_MMAP
81+
# 控制是否使用 mmap(2) 来处理缓存文件(如果可用)。
82+
# 值为布尔类型(yes/no, 1/0)。
83+
# 如果显式设置该变量,将跳过系统检查并强制启用或禁用。
84+
FONTCONFIG_USE_MMAP: str
85+
```
86+
87+
### 构建说明
88+
89+
受限于 XMake, 构建时须使用 Python 3.10.11,并且在 `uv sync` 时需一同安装 `build` 组的依赖(`pip` `setuptools` `wheel` 等)。
90+
91+
以下说明假定你已经按照 [XMake 官方文档](https://xmake.io/#/zh-cn/guide/installation) 安装好了 XMake 并在 PATH 上可用。
92+
93+
```bash
94+
# 拉取子模块
95+
git submodule update --init --recursive
96+
# 创建虚拟环境
97+
uv venv
98+
# 激活虚拟环境,在不同操作系统下的命令不同
99+
source .venv/bin/activate
100+
# 安装开发依赖,同时避免直接构建原生包
101+
uv sync --no-install-workspace
102+
# 配置依赖
103+
xmake f -m releasedbg
104+
# 构建
105+
xmake build
106+
# 安装到 bindist,加速 editable 安装
107+
xmake install -o bindist
108+
# 使用 uv 安装到当前虚拟环境
109+
uv sync --reinstall-package nonebot-plugin-htmlkit
110+
```
111+
112+
如果对 litehtml 做了修改,需重新构建:
113+
114+
```bash
115+
xmake require --force litehtml
116+
# 或者用以下更 dirty 但是快速的方法
117+
rm -r build
118+
xmake clean --all
119+
# 重新构建与安装
120+
xmake build
121+
xmake install -o bindist
122+
uv sync --reinstall-package nonebot-plugin-htmlkit
123+
```
124+
125+
#### 许可证
126+
127+
本插件的 C++ 部分静态链接了 `litehtml` `cairo` `pango` 等第三方库,它们在 LGPL 许可证下发布。
128+
129+
本插件的 Python 部分(所有 .py 文件)在 MIT 许可证下发布,[C++ 部分](./core)在 LGPL-3.0-or-later 许可证下发布。

0 commit comments

Comments
 (0)