新增Twitch IRC协议相关的全套实现,包括: 1. 基础工具类:令牌处理、消息格式化、速率限制、消息去重 2. 核心适配器组件:IRC解析器、消息归一化、外发消息处理 3. API客户端:Helix API封装、认证提供者 4. 配置与部署:配置校验、设置向导 5. 辅助功能:配对管理、健康检查、目标解析等
56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
_LINK_PATTERN = re.compile(r"\[([^\]]*)\]\([^)]*\)")
|
|
_IMG_PATTERN = re.compile(r"!\[([^\]]*)\]\([^)]*\)")
|
|
_BOLD_PATTERN = re.compile(r"\*\*(.+?)\*\*")
|
|
_BOLD_ALT_PATTERN = re.compile(r"__(.+?)__")
|
|
_ITALIC_PATTERN = re.compile(r"\*(.+?)\*")
|
|
_ITALIC_ALT_PATTERN = re.compile(r"_(.+?)_")
|
|
_STRIKETHROUGH_PATTERN = re.compile(r"~~(.+?)~~")
|
|
_FENCED_CODE_PATTERN = re.compile(r"```[^\n]*\n(.*?)```", re.DOTALL)
|
|
_INLINE_CODE_PATTERN = re.compile(r"`([^`]+)`")
|
|
_HEADING_PATTERN = re.compile(r"^#{1,6}\s+", re.MULTILINE)
|
|
_UNORDERED_LIST_PATTERN = re.compile(r"^[\-\*]\s+", re.MULTILINE)
|
|
_ORDERED_LIST_PATTERN = re.compile(r"^\d+\.\s+", re.MULTILINE)
|
|
_BLOCKQUOTE_PATTERN = re.compile(r"^>\s?", re.MULTILINE)
|
|
_HR_PATTERN = re.compile(r"^[\-\*_]{3,}\s*$", re.MULTILINE)
|
|
_MULTISPACE_PATTERN = re.compile(r"[ \t]+")
|
|
_MULTINEWLINE_PATTERN = re.compile(r"\n{3,}")
|
|
|
|
|
|
def strip_twitch_markdown(text: str) -> str:
|
|
if not text:
|
|
return text
|
|
|
|
text = _IMG_PATTERN.sub("", text)
|
|
|
|
text = _LINK_PATTERN.sub(r"\1", text)
|
|
|
|
text = _FENCED_CODE_PATTERN.sub(r"\1", text)
|
|
|
|
text = _HR_PATTERN.sub("", text)
|
|
|
|
text = _BOLD_PATTERN.sub(r"\1", text)
|
|
text = _BOLD_ALT_PATTERN.sub(r"\1", text)
|
|
|
|
text = _ITALIC_PATTERN.sub(r"\1", text)
|
|
text = _ITALIC_ALT_PATTERN.sub(r"\1", text)
|
|
|
|
text = _STRIKETHROUGH_PATTERN.sub(r"\1", text)
|
|
|
|
text = _INLINE_CODE_PATTERN.sub(r"\1", text)
|
|
|
|
text = _HEADING_PATTERN.sub("", text)
|
|
|
|
text = _ORDERED_LIST_PATTERN.sub("", text)
|
|
text = _UNORDERED_LIST_PATTERN.sub("", text)
|
|
|
|
text = _BLOCKQUOTE_PATTERN.sub("", text)
|
|
|
|
text = _MULTISPACE_PATTERN.sub(" ", text)
|
|
text = _MULTINEWLINE_PATTERN.sub("\n\n", text)
|
|
|
|
return text.strip()
|