Update app.py
Browse files
app.py
CHANGED
@@ -82,48 +82,23 @@ async def generate_audio(text, voice, filename):
|
|
82 |
def detect_and_convert_links(text):
|
83 |
# Convert Markdown links [text](url) to HTML <a> tags
|
84 |
md_link_pattern = re.compile(r'\[(.*?)\]\((https?://[^\s\[\]()<>{}]+)\)')
|
85 |
-
|
86 |
-
text = match.group(1)
|
87 |
-
url = match.group(2)
|
88 |
-
return f'<a href="{url}" color="blue">{text}</a>'
|
89 |
-
text = md_link_pattern.sub(replace_md_link, text)
|
90 |
|
91 |
# Convert plain URLs to HTML <a> tags, avoiding already tagged links
|
92 |
url_pattern = re.compile(
|
93 |
-
r'(?<!href=")(https?://[^\s
|
94 |
re.IGNORECASE
|
95 |
)
|
96 |
-
|
97 |
-
url = match.group(1)
|
98 |
-
return f'<a href="{url}" color="blue">{url}</a>'
|
99 |
-
|
100 |
-
text = url_pattern.sub(replace_url, text)
|
101 |
return text
|
102 |
|
103 |
def apply_emoji_font(text, emoji_font):
|
104 |
-
#
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
# Split text around links and bold tags
|
109 |
-
segments = []
|
110 |
-
last_pos = 0
|
111 |
-
for match in link_pattern.finditer(text):
|
112 |
-
start, end = match.span()
|
113 |
-
if last_pos < start:
|
114 |
-
segments.append(('text', text[last_pos:start]))
|
115 |
-
segments.append(('link', match.group(0)))
|
116 |
-
last_pos = end
|
117 |
-
for match in bold_pattern.finditer(text[last_pos:]):
|
118 |
-
start, end = match.span()
|
119 |
-
if last_pos < start + last_pos:
|
120 |
-
segments.append(('text', text[last_pos:start + last_pos]))
|
121 |
-
segments.append(('bold', match.group(0)))
|
122 |
-
last_pos = start + end
|
123 |
-
if last_pos < len(text):
|
124 |
-
segments.append(('text', text[last_pos:]))
|
125 |
|
126 |
-
# Apply emoji font to text
|
127 |
emoji_pattern = re.compile(
|
128 |
r"([\U0001F300-\U0001F5FF"
|
129 |
r"\U0001F600-\U0001F64F"
|
@@ -144,24 +119,23 @@ def apply_emoji_font(text, emoji_font):
|
|
144 |
emoji = unicodedata.normalize('NFC', emoji)
|
145 |
return f'<font face="{emoji_font}">{emoji}</font>'
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
150 |
# Apply font to non-emoji text and emoji separately
|
151 |
parts = []
|
152 |
last_pos = 0
|
153 |
-
for match in emoji_pattern.finditer(
|
154 |
start, end = match.span()
|
155 |
if last_pos < start:
|
156 |
-
parts.append(f'<font face="DejaVuSans">{
|
157 |
parts.append(replace_emoji(match))
|
158 |
last_pos = end
|
159 |
-
if last_pos < len(
|
160 |
-
parts.append(f'<font face="DejaVuSans">{
|
161 |
result.append(''.join(parts))
|
162 |
-
else:
|
163 |
-
# Keep links and bold tags unchanged
|
164 |
-
result.append(content)
|
165 |
|
166 |
return ''.join(result)
|
167 |
|
|
|
82 |
def detect_and_convert_links(text):
|
83 |
# Convert Markdown links [text](url) to HTML <a> tags
|
84 |
md_link_pattern = re.compile(r'\[(.*?)\]\((https?://[^\s\[\]()<>{}]+)\)')
|
85 |
+
text = md_link_pattern.sub(r'<a href="\2" color="blue">\1</a>', text)
|
|
|
|
|
|
|
|
|
86 |
|
87 |
# Convert plain URLs to HTML <a> tags, avoiding already tagged links
|
88 |
url_pattern = re.compile(
|
89 |
+
r'(?<!href=")(https?://[^\s<>{}]+)',
|
90 |
re.IGNORECASE
|
91 |
)
|
92 |
+
text = url_pattern.sub(r'<a href="\1" color="blue">\1</a>', text)
|
|
|
|
|
|
|
|
|
93 |
return text
|
94 |
|
95 |
def apply_emoji_font(text, emoji_font):
|
96 |
+
# Protect existing tags
|
97 |
+
tag_pattern = re.compile(r'(<[^>]+>)')
|
98 |
+
segments = tag_pattern.split(text)
|
99 |
+
result = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
+
# Apply emoji font only to non-tag text
|
102 |
emoji_pattern = re.compile(
|
103 |
r"([\U0001F300-\U0001F5FF"
|
104 |
r"\U0001F600-\U0001F64F"
|
|
|
119 |
emoji = unicodedata.normalize('NFC', emoji)
|
120 |
return f'<font face="{emoji_font}">{emoji}</font>'
|
121 |
|
122 |
+
for segment in segments:
|
123 |
+
if tag_pattern.match(segment):
|
124 |
+
# Keep tags unchanged
|
125 |
+
result.append(segment)
|
126 |
+
else:
|
127 |
# Apply font to non-emoji text and emoji separately
|
128 |
parts = []
|
129 |
last_pos = 0
|
130 |
+
for match in emoji_pattern.finditer(segment):
|
131 |
start, end = match.span()
|
132 |
if last_pos < start:
|
133 |
+
parts.append(f'<font face="DejaVuSans">{segment[last_pos:start]}</font>')
|
134 |
parts.append(replace_emoji(match))
|
135 |
last_pos = end
|
136 |
+
if last_pos < len(segment):
|
137 |
+
parts.append(f'<font face="DejaVuSans">{segment[last_pos:]}</font>')
|
138 |
result.append(''.join(parts))
|
|
|
|
|
|
|
139 |
|
140 |
return ''.join(result)
|
141 |
|