78 lines
2.2 KiB
Python
78 lines
2.2 KiB
Python
"""
|
|
Module: markdown_parser.py
|
|
----------------------------
|
|
Rozšířený Markdown parser pro curses.
|
|
...
|
|
"""
|
|
|
|
import re
|
|
import curses
|
|
|
|
def parse_markdown(text):
|
|
segments = []
|
|
lines = text.splitlines()
|
|
in_code_block = False
|
|
|
|
for line in lines:
|
|
stripped = line.strip()
|
|
|
|
if stripped.startswith("```"):
|
|
in_code_block = not in_code_block
|
|
continue
|
|
|
|
if in_code_block:
|
|
# celé řádky ve vícerádkovém kódu
|
|
segments.append((line + "\n", curses.A_REVERSE))
|
|
continue
|
|
|
|
# Nadpisy
|
|
if stripped.startswith("#"):
|
|
hash_count = 0
|
|
for ch in line:
|
|
if ch == '#':
|
|
hash_count += 1
|
|
else:
|
|
break
|
|
content = line[hash_count:].strip()
|
|
segments.append((content + "\n", curses.A_BOLD | curses.A_UNDERLINE))
|
|
# Odrážky
|
|
elif stripped.startswith("-") or stripped.startswith("*"):
|
|
content = line[1:].strip()
|
|
segments.append((" • " + content + "\n", curses.A_BOLD))
|
|
else:
|
|
# Běžný řádek
|
|
inline_segments = parse_inline(line)
|
|
for seg_text, seg_attr in inline_segments:
|
|
segments.append((seg_text, seg_attr))
|
|
segments.append(("\n", 0))
|
|
|
|
return segments
|
|
|
|
|
|
def parse_inline(text):
|
|
segments = []
|
|
pos = 0
|
|
pattern = re.compile(r'(\*\*.*?\*\*|\*.*?\*|`.*?`)')
|
|
|
|
for match in pattern.finditer(text):
|
|
start, end = match.span()
|
|
if start > pos:
|
|
segments.append((text[pos:start], 0))
|
|
token = match.group(0)
|
|
if token.startswith("**") and token.endswith("**"):
|
|
content = token[2:-2]
|
|
segments.append((content, curses.A_BOLD))
|
|
elif token.startswith("*") and token.endswith("*"):
|
|
content = token[1:-1]
|
|
segments.append((content, curses.A_UNDERLINE))
|
|
elif token.startswith("`") and token.endswith("`"):
|
|
content = token[1:-1]
|
|
segments.append((content, curses.A_REVERSE))
|
|
else:
|
|
segments.append((token, 0))
|
|
pos = end
|
|
|
|
if pos < len(text):
|
|
segments.append((text[pos:], 0))
|
|
return segments
|