-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
46 lines (35 loc) · 1.45 KB
/
Copy pathutils.py
File metadata and controls
46 lines (35 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import re
import emoji
from typing import Match
def strip_inline_formatting(text: str) -> str:
"""Strips all inline Markdown syntax, formatting labels, and symbols to make the
text completely clean and optimized for Text-to-Speech (TTS) engines.
"""
if not text:
return ""
# Strip image:  -> Image: alt (if alt exists), else empty
def replace_image(match: Match[str]) -> str:
alt = match.group(1).strip()
return f"Image: {alt}" if alt else ""
text = re.sub(r"!\[([^\]]*)\]\(([^)]+)\)", replace_image, text)
# Strip link: [text](url) -> text (completely omitting URL to keep TTS clean)
text = re.sub(r"\[([^\]]+)\]\(([^)]+)\)", r"\1", text)
# Strip footnote references completely from inline text (e.g. [^1] -> "")
text = re.sub(r"\[\^([^\]]+)\](?!:)", "", text)
# Strip bold/italic/strikethrough/highlight/sub/super
text = re.sub(r"(\*\*|__)(.*?)\1", r"\2", text)
text = re.sub(r"(\*|_)(.*?)\1", r"\2", text)
text = re.sub(r"~~(.*?)~~", r"\1", text)
text = re.sub(r"==(.*?)==", r"\1", text)
text = re.sub(r"~(.*?)~", r"\1", text)
text = re.sub(r"\^(.*?)\^", r"\1", text)
# Clean spoilers: ||spoiler|| -> Spoiler: spoiler
text = re.sub(r"\|\|(.*?)\|\|", r"Spoiler: \1", text)
# Inline code
text = re.sub(r"`([^`]+)`", r"\1", text)
# Emojis shortcodes
try:
text = emoji.emojize(text)
except Exception:
pass
return text