From 38abd208ffd6b44346a1bf9adf18a186d9d46b34 Mon Sep 17 00:00:00 2001 From: jeffser Date: Wed, 9 Oct 2024 20:48:04 -0600 Subject: [PATCH] Much better message block rendering --- src/custom_widgets/message_widget.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/custom_widgets/message_widget.py b/src/custom_widgets/message_widget.py index 816780f..90ab7ee 100644 --- a/src/custom_widgets/message_widget.py +++ b/src/custom_widgets/message_widget.py @@ -534,7 +534,8 @@ class message(Gtk.Overlay): self.content_children = [] if text: self.content_children = [] - code_block_pattern = re.compile(r'[```|`](\w*)\n(.*?)\n\s*[```|`]', re.DOTALL) + code_block_pattern = re.compile(r'```(\w*)\n(.*?)\n\s*```', re.DOTALL) + no_language_code_block_pattern = re.compile(r'`(\w*)\n(.*?)\n\s*`', re.DOTALL) table_pattern = re.compile(r'((\r?\n){2}|^)([^\r\n]*\|[^\r\n]*(\r?\n)?)+(?=(\r?\n){2}|$)', re.MULTILINE) bold_pattern = re.compile(r'\*\*(.*?)\*\*') #"**text**" code_pattern = re.compile(r'`([^`\n]*?)`') #"`text`" @@ -544,7 +545,8 @@ class message(Gtk.Overlay): parts = [] pos = 0 # Code blocks - for match in code_block_pattern.finditer(self.text): + for match in code_block_pattern.finditer(self.text[pos:]): + print(match) start, end = match.span() if pos < start: normal_text = self.text[pos:start] @@ -553,8 +555,17 @@ class message(Gtk.Overlay): code_text = match.group(2) parts.append({"type": "code", "text": code_text, "language": 'python3' if language == 'python' else language}) pos = end + for match in no_language_code_block_pattern.finditer(self.text[pos:]): + start, end = match.span() + if pos < start: + normal_text = self.text[pos:start] + parts.append({"type": "normal", "text": normal_text.strip()}) + language = match.group(1) + code_text = match.group(2) + parts.append({"type": "code", "text": code_text, "language": None}) + pos = end # Tables - for match in table_pattern.finditer(self.text): + for match in table_pattern.finditer(self.text[pos:]): start, end = match.span() if pos < start: normal_text = self.text[pos:start] @@ -563,8 +574,8 @@ class message(Gtk.Overlay): parts.append({"type": "table", "text": table_text}) pos = end # Text blocks - if pos < len(text): - normal_text = text[pos:] + if pos < len(self.text[pos:]): + normal_text = self.text[pos:] if normal_text.strip(): parts.append({"type": "normal", "text": normal_text.strip()})