Skip to content

Commit

Permalink
🐛 修复 File name too long 错误
Browse files Browse the repository at this point in the history
  • Loading branch information
A-kirami committed Sep 10, 2023
1 parent 4243ac7 commit 4ff8a9a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
21 changes: 8 additions & 13 deletions kirami/utils/renderer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,14 @@ class Renderer:
.use(dollarmath_plugin)
)

@classmethod
def _get_string(cls, fs: str | Path) -> str:
file = Path(fs) if isinstance(fs, str) else fs
if file.is_file() or isinstance(fs, Path):
return file.read_text(encoding="utf-8")
return fs

@classmethod
async def template(
cls, tpl: str | Path, *, env: Environment | None = None, **kwargs
) -> str:
"""将 jinja2 模板渲染为 html。
### 参数
tpl: 模板文件路径或字符串
tpl: 模板字符串或文件路径
env: jinja2 环境,默认为类属性 env
Expand All @@ -64,9 +57,10 @@ async def template(
### 返回
html 字符串
"""
string = cls._get_string(tpl)
if isinstance(tpl, Path):
tpl = tpl.read_text(encoding="utf-8")
env = env or cls.env
template = env.from_string(string)
template = env.from_string(tpl)
return await template.render_async(**kwargs)

@classmethod
Expand All @@ -82,7 +76,7 @@ async def markdown(
"""将 markdown 渲染为 html。
### 参数
md: markdown 文件路径或字符串
md: markdown 字符串或文件路径
theme: 主题,可选值为 "light" 或 "dark",默认为 "light"
Expand All @@ -97,11 +91,12 @@ async def markdown(
### 返回
html 字符串
"""
string = cls._get_string(md)
if isinstance(md, Path):
md = md.read_text(encoding="utf-8")
env = {}
if highlight == "auto":
env["theme"] = "xcode" if theme == "light" else "lightbulb"
html = cls.md.render(string, env=env)
html = cls.md.render(md, env=env)
if only_md:
return await cls.template(html, **kwargs)
base_path = Path(__file__).parent / "template"
Expand Down
18 changes: 4 additions & 14 deletions kirami/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,6 @@ def new_dir(path: str | Path, root: str | Path = BOT_DIR) -> Path:
return dir_.resolve()


def is_path(fs: str | Path) -> bool:
"""判断是否是一个文件路径。
### 参数
fs: 文件路径
"""
file = Path(fs) if isinstance(fs, str) else fs
return file.is_file() or isinstance(fs, Path)


def str_of_size(size: int) -> str:
"""将字节大小转换为带单位的字符串。
Expand Down Expand Up @@ -210,7 +200,7 @@ async def tpl2img(
"""将 jinja2 模板转换为图片。
### 参数
tpl: 模板文件路径或字符串
tpl: 模板字符串或文件路径
data: 模板渲染所需的数据
Expand All @@ -226,7 +216,7 @@ async def tpl2img(
图片的二进制数据
"""
html = await Renderer.template(tpl, **(data or {}))
if not base_path and is_path(tpl):
if not base_path and isinstance(tpl, Path):
base_path = Path(tpl).parent
return await html2pic(
html, base_path, width=width, device_scale_factor=device_scale_factor, **kwargs
Expand All @@ -247,7 +237,7 @@ async def md2img(
"""将 markdown 转换为图片。
### 参数
md: markdown 文件路径或字符串
md: markdown 字符串或文件路径
theme: 主题,可选值为 "light" 或 "dark",默认为 "light"
Expand All @@ -267,7 +257,7 @@ async def md2img(
图片的二进制数据
"""
html = await Renderer.markdown(md, theme, highlight, extra)
if not base_path and is_path(md):
if not base_path and isinstance(md, Path):
base_path = Path(md).parent
return await html2pic(
html, base_path, width=width, device_scale_factor=device_scale_factor, **kwargs
Expand Down

0 comments on commit 4ff8a9a

Please sign in to comment.