这是一个批量整理提交,包含以下主要改动: 1. 删除多处冗余的空行和未使用的导入 2. 修复文件末尾缺少换行符的问题 3. 调整部分模块的导入顺序与代码排版 4. 修复部分配置默认值与策略逻辑 5. 新增多个功能模块与辅助工具 6. 完善异常处理与日志记录 7. 修复速率限制、消息缓存、权限校验等逻辑bug 8. 废弃部分旧有API与配置项并添加警告提示
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
SLACK_MENTION_STRIP_PATTERN = re.compile(r"<@[^>\s]+>")
|
|
SLACK_CHANNEL_PATTERN = re.compile(r"<#([A-Z0-9]+)(?:\|([^>]+))?>")
|
|
SLACK_URL_PATTERN = re.compile(r"<(https?://[^|>]+)(?:\|([^>]+))?>")
|
|
SLACK_SUBTEAM_PATTERN = re.compile(r"<!subteam\^([A-Z0-9]+)(?:\|([^>]+))?>")
|
|
SLACK_SPECIAL_MENTION_PATTERN = re.compile(r"<!([^>]+)>")
|
|
SLACK_HTML_ENTITY_PATTERN = re.compile(r"&(amp|lt|gt|quot);")
|
|
|
|
_HTML_ENTITIES = {"amp": "&", "lt": "<", "gt": ">", "quot": '"'}
|
|
|
|
|
|
def strip_mentions(text: str) -> str:
|
|
return SLACK_MENTION_STRIP_PATTERN.sub("", text).strip()
|
|
|
|
|
|
def extract_mentions(text: str) -> list[str]:
|
|
return re.findall(r"<@([A-Z0-9]+)>", text)
|
|
|
|
|
|
def normalize_channel_refs(text: str) -> str:
|
|
def repl(m: re.Match) -> str:
|
|
name = m.group(2)
|
|
return f"#{name}" if name else f"#{m.group(1)}"
|
|
|
|
return SLACK_CHANNEL_PATTERN.sub(repl, text)
|
|
|
|
|
|
def normalize_urls(text: str) -> str:
|
|
def repl(m: re.Match) -> str:
|
|
url = m.group(1)
|
|
label = m.group(2)
|
|
if label and label != url:
|
|
return f"{label} ({url})"
|
|
return url
|
|
|
|
return SLACK_URL_PATTERN.sub(repl, text)
|
|
|
|
|
|
def normalize_special_mentions(text: str) -> str:
|
|
return SLACK_SPECIAL_MENTION_PATTERN.sub(r"@\1", text)
|
|
|
|
|
|
def unescape_html_entities(text: str) -> str:
|
|
def repl(m: re.Match) -> str:
|
|
return _HTML_ENTITIES.get(m.group(1), m.group(0))
|
|
|
|
return SLACK_HTML_ENTITY_PATTERN.sub(repl, text)
|
|
|
|
|
|
def normalize_slack_text(text: str) -> str:
|
|
text = normalize_channel_refs(text)
|
|
text = normalize_urls(text)
|
|
text = normalize_special_mentions(text)
|
|
text = unescape_html_entities(text)
|
|
return text.strip() |