Much better message block rendering
This commit is contained in:
parent
c34713eff5
commit
38abd208ff
@ -534,7 +534,8 @@ class message(Gtk.Overlay):
|
|||||||
self.content_children = []
|
self.content_children = []
|
||||||
if text:
|
if text:
|
||||||
self.content_children = []
|
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)
|
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**"
|
bold_pattern = re.compile(r'\*\*(.*?)\*\*') #"**text**"
|
||||||
code_pattern = re.compile(r'`([^`\n]*?)`') #"`text`"
|
code_pattern = re.compile(r'`([^`\n]*?)`') #"`text`"
|
||||||
@ -544,7 +545,8 @@ class message(Gtk.Overlay):
|
|||||||
parts = []
|
parts = []
|
||||||
pos = 0
|
pos = 0
|
||||||
# Code blocks
|
# 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()
|
start, end = match.span()
|
||||||
if pos < start:
|
if pos < start:
|
||||||
normal_text = self.text[pos:start]
|
normal_text = self.text[pos:start]
|
||||||
@ -553,8 +555,17 @@ class message(Gtk.Overlay):
|
|||||||
code_text = match.group(2)
|
code_text = match.group(2)
|
||||||
parts.append({"type": "code", "text": code_text, "language": 'python3' if language == 'python' else language})
|
parts.append({"type": "code", "text": code_text, "language": 'python3' if language == 'python' else language})
|
||||||
pos = end
|
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
|
# Tables
|
||||||
for match in table_pattern.finditer(self.text):
|
for match in table_pattern.finditer(self.text[pos:]):
|
||||||
start, end = match.span()
|
start, end = match.span()
|
||||||
if pos < start:
|
if pos < start:
|
||||||
normal_text = self.text[pos:start]
|
normal_text = self.text[pos:start]
|
||||||
@ -563,8 +574,8 @@ class message(Gtk.Overlay):
|
|||||||
parts.append({"type": "table", "text": table_text})
|
parts.append({"type": "table", "text": table_text})
|
||||||
pos = end
|
pos = end
|
||||||
# Text blocks
|
# Text blocks
|
||||||
if pos < len(text):
|
if pos < len(self.text[pos:]):
|
||||||
normal_text = text[pos:]
|
normal_text = self.text[pos:]
|
||||||
if normal_text.strip():
|
if normal_text.strip():
|
||||||
parts.append({"type": "normal", "text": normal_text.strip()})
|
parts.append({"type": "normal", "text": normal_text.strip()})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user