Enhance Markdown parsing: support escaped asterisks and improve segment handling
This commit is contained in:
parent
ddf318f7c4
commit
b900bada0a
16
app.py
16
app.py
@ -286,6 +286,10 @@ def parse_line(line):
|
||||
return ('text', parse_segments(line))
|
||||
|
||||
def parse_segments(line):
|
||||
# Handle escaped asterisks: replace them with a placeholder
|
||||
line = line.replace(r'\*', '\x07')
|
||||
|
||||
# Apply formatting
|
||||
bi = re.findall(r"\*\*\*(.+?)\*\*\*", line)
|
||||
for x in bi:
|
||||
line = line.replace(f"***{x}***", f"\x01{x}\x02")
|
||||
@ -295,39 +299,37 @@ def parse_segments(line):
|
||||
i = re.findall(r"\*(.+?)\*", line)
|
||||
for x in i:
|
||||
line = line.replace(f"*{x}*", f"\x05{x}\x06")
|
||||
|
||||
# Split into styled segments
|
||||
segments = []
|
||||
i = 0
|
||||
style = 'text'
|
||||
while i < len(line):
|
||||
if line[i] == '\x01':
|
||||
style = 'bolditalic'
|
||||
i += 1
|
||||
start = i
|
||||
while i < len(line) and line[i] != '\x02': i += 1
|
||||
segments.append(('bolditalic', line[start:i]))
|
||||
style = 'text'
|
||||
i += 1
|
||||
elif line[i] == '\x03':
|
||||
style = 'bold'
|
||||
i += 1
|
||||
start = i
|
||||
while i < len(line) and line[i] != '\x04': i += 1
|
||||
segments.append(('bold', line[start:i]))
|
||||
style = 'text'
|
||||
i += 1
|
||||
elif line[i] == '\x05':
|
||||
style = 'italic'
|
||||
i += 1
|
||||
start = i
|
||||
while i < len(line) and line[i] != '\x06': i += 1
|
||||
segments.append(('italic', line[start:i]))
|
||||
style = 'text'
|
||||
i += 1
|
||||
else:
|
||||
start = i
|
||||
while i < len(line) and line[i] not in '\x01\x03\x05': i += 1
|
||||
if i > start:
|
||||
segments.append(('text', line[start:i]))
|
||||
|
||||
# Restore literal asterisks
|
||||
segments = [(style, text.replace('\x07', '*')) for style, text in segments]
|
||||
return segments
|
||||
|
||||
def font_for_style(style, font, font_bold, font_italic, font_bolditalic):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user