这是一个批量整理提交,包含以下主要改动: 1. 删除多处冗余的空行和未使用的导入 2. 修复文件末尾缺少换行符的问题 3. 调整部分模块的导入顺序与代码排版 4. 修复部分配置默认值与策略逻辑 5. 新增多个功能模块与辅助工具 6. 完善异常处理与日志记录 7. 修复速率限制、消息缓存、权限校验等逻辑bug 8. 废弃部分旧有API与配置项并添加警告提示
100 lines
2.8 KiB
Python
100 lines
2.8 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
_ALLOWED_TAGS = {"b", "i", "u", "s", "a", "br", "code", "pre", "blockquote", "img"}
|
|
|
|
_UNSUPPORTED_REPLACEMENTS: dict[str, tuple[str, str]] = {
|
|
"strong": ("<b>", "</b>"),
|
|
"em": ("<i>", "</i>"),
|
|
"strike": ("<s>", "</s>"),
|
|
"del": ("<s>", "</s>"),
|
|
"ins": ("<u>", "</u>"),
|
|
"h1": ("<b>", "</b>\n"),
|
|
"h2": ("<b>", "</b>\n"),
|
|
"h3": ("<b>", "</b>\n"),
|
|
"h4": ("<b>", "</b>\n"),
|
|
"h5": ("<b>", "</b>\n"),
|
|
"h6": ("<b>", "</b>\n"),
|
|
"p": ("\n", ""),
|
|
"ul": ("", ""),
|
|
"ol": ("", ""),
|
|
"li": ("• ", "\n"),
|
|
"span": ("", ""),
|
|
"div": ("\n", ""),
|
|
"hr": ("\n---\n", ""),
|
|
}
|
|
|
|
_STRIP_TAGS = {"script", "style", "head", "meta", "link", "iframe", "object", "embed"}
|
|
|
|
|
|
def render_html_to_imessage(html: str) -> str:
|
|
if not html:
|
|
return ""
|
|
|
|
for tag in _STRIP_TAGS:
|
|
html = re.sub(rf"<{tag}[^>]*>.*?</{tag}>", "", html, flags=re.DOTALL | re.IGNORECASE)
|
|
html = re.sub(rf"<{tag}[^>]*/>", "", html, flags=re.IGNORECASE)
|
|
|
|
for tag, (open_tag, close_tag) in _UNSUPPORTED_REPLACEMENTS.items():
|
|
html = re.sub(rf"<{tag}(\s[^>]*)?>", open_tag, html, flags=re.IGNORECASE)
|
|
html = re.sub(rf"</{tag}>", close_tag, html, flags=re.IGNORECASE)
|
|
|
|
html = _strip_unsafe_attributes(html)
|
|
|
|
html = _normalize_links(html)
|
|
|
|
html = html.replace("\r\n", "\n").replace("\r", "\n")
|
|
|
|
return html.strip()
|
|
|
|
|
|
def _strip_unsafe_attributes(html: str) -> str:
|
|
def _clean_tag(tag_match: re.Match) -> str:
|
|
full_tag = tag_match.group(0)
|
|
tag_name_match = re.match(r"</?(\w+)", full_tag)
|
|
if not tag_name_match:
|
|
return full_tag
|
|
tag_name = tag_name_match.group(1).lower()
|
|
if tag_name not in _ALLOWED_TAGS:
|
|
return full_tag
|
|
safe_attrs = ["href", "src", "alt"]
|
|
result = full_tag
|
|
for attr in re.finditer(r'(\w+)="([^"]*)"', full_tag):
|
|
if attr.group(1).lower() not in safe_attrs:
|
|
result = result.replace(attr.group(0), "")
|
|
return result
|
|
|
|
return re.sub(r"<[^>]+>", _clean_tag, html)
|
|
|
|
|
|
def _normalize_links(text: str) -> str:
|
|
url_pattern = re.compile(r"(?<!href=\")(https?://[^\s<>\"\)]+)")
|
|
return url_pattern.sub(r'<a href="\1">\1</a>', text)
|
|
|
|
|
|
def render_markdown_to_imessage(text: str) -> str:
|
|
if not text:
|
|
return ""
|
|
|
|
import re
|
|
|
|
codes: dict[str, str] = {}
|
|
|
|
def _save_code(m: re.Match) -> str:
|
|
key = f"\x00CODE{len(codes)}\x00"
|
|
codes[key] = m.group(0)
|
|
return key
|
|
|
|
text = re.sub(r"`[^`]+`", _save_code, text)
|
|
|
|
text = re.sub(r"\*\*(.+?)\*\*", r"<b>\1</b>", text)
|
|
text = re.sub(r"__(.+?)__", r"<b>\1</b>", text)
|
|
|
|
text = re.sub(r"(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)", r"<i>\1</i>", text)
|
|
|
|
for key, val in codes.items():
|
|
text = text.replace(key, val)
|
|
|
|
return text
|