Я работаю над приложением Tkinter, где я отображаю текст в текстовом виджете. Входящий текст иногда содержит ссылки в стиле Markdown в следующем формате: < /p>
Здесь, имя источника ("CNN Travel") сразу же следует URL в скобках. Я хочу сделать имя источника, чтобы щелкнуть его открывало соответствующую ссылку в веб -браузере по умолчанию. Ниже приведена упрощенная версия моего текущего кода, которая обрабатывает текстовый поток и форматирует жирный текст: < /p>
stream = some_function()
full_response = ""
buffer = ""
bold = False
for chunk in stream:
if chunk != "None":
full_response += chunk
buffer += chunk
if "**" in buffer:
while "**" in buffer:
print("exist bold")
pre, _, post = buffer.partition("**")
append_to_chat_log(message=pre, bold=bold)
bold = not bold
buffer = post
else:
append_to_chat_log(message=buffer, bold=bold)
buffer = ""
def insert_with_formatting(message):
chat_log.tag_configure("bold", font=('Helvetica', font_size, 'bold'))
bold_pattern = re.compile(r'\*\*(.*?)\*\*')
cursor = 0
for match in bold_pattern.finditer(message):
start, end = match.span()
# Insert text before bold part
chat_log.insert("end", message[cursor:start])
# Insert bold text
chat_log.insert("end", match.group(1), "bold")
cursor = end
# Insert any remaining text after the last bold part
chat_log.insert("end", message[cursor:])
def append_to_chat_log(sender=None, message=None, bold=False):
chat_log.config(state=tk.NORMAL)
if sender:
chat_log.insert("end", f"{sender}\n\n", "sender")
if message and bold:
print("append bold")
chat_log.tag_configure("bold", font=('Helvetica', font_size, 'bold'))
chat_log.insert("end", message, "bold")
if not bold and message:
if "**" in message:
print("if message and not bold")
insert_with_formatting(message)
else:
chat_log.insert("end", message)
chat_log.tag_config("sender", font=('Helvetica', font_size, 'bold'), foreground=TEXT_COLOR)
chat_log.config(state=tk.DISABLED)
chat_log.see("end")
chat_log.update()
Я работаю над приложением Tkinter, где я отображаю текст в текстовом виджете. Входящий текст иногда содержит ссылки в стиле Markdown в следующем формате: < /p> [code][CNN Travel](https://www.cnn.com/travel/best-destinations-to-visit-2025/index.html)[/code] Здесь, имя источника ("CNN Travel") сразу же следует URL в скобках. Я хочу сделать имя источника, чтобы щелкнуть его открывало соответствующую ссылку в веб -браузере по умолчанию. Ниже приведена упрощенная версия моего текущего кода, которая обрабатывает текстовый поток и форматирует жирный текст: < /p> [code]stream = some_function()
full_response = "" buffer = "" bold = False for chunk in stream: if chunk != "None": full_response += chunk buffer += chunk if "**" in buffer: while "**" in buffer: print("exist bold") pre, _, post = buffer.partition("**") append_to_chat_log(message=pre, bold=bold) bold = not bold buffer = post else: append_to_chat_log(message=buffer, bold=bold) buffer = ""
for match in bold_pattern.finditer(message): start, end = match.span() # Insert text before bold part chat_log.insert("end", message[cursor:start]) # Insert bold text chat_log.insert("end", match.group(1), "bold") cursor = end
# Insert any remaining text after the last bold part chat_log.insert("end", message[cursor:])
def append_to_chat_log(sender=None, message=None, bold=False): chat_log.config(state=tk.NORMAL) if sender: chat_log.insert("end", f"{sender}\n\n", "sender") if message and bold: print("append bold") chat_log.tag_configure("bold", font=('Helvetica', font_size, 'bold')) chat_log.insert("end", message, "bold")
if not bold and message: if "**" in message: print("if message and not bold") insert_with_formatting(message) else: chat_log.insert("end", message)
chat_log.tag_config("sender", font=('Helvetica', font_size, 'bold'), foreground=TEXT_COLOR) chat_log.config(state=tk.DISABLED) chat_log.see("end") chat_log.update() [/code] [b] Мой вопрос: [/b] Как я могу изменить это (или добавить дополнительный код), чтобы обнаружить ссылки в стиле разметки (например, [CNN Travel] (https://www.cnn.com/travel/best-destinations-to-visit-2025/index.html)
Я работаю над приложением Tkinter, где я отображаю текст в текстовом виджете. Входящий текст иногда содержит ссылки в стиле Markdown в следующем формате:
(
Здесь, имя источника ( CNN Travel ) сразу же следует URL в скобках. Я хочу сделать имя...
Как мне включить слова, начинающиеся с заглавных и строчных букв? Потому что сейчас это только строчные буквы.
$string = you would love this @matt ;
$pattern = '/(^|\W)(@( +))/';
$replacement = '$1 $2 ';
echo preg_replace($pattern, $replacement,...