From bae42c9a49ca572bb9068dc985e2111593b24771 Mon Sep 17 00:00:00 2001 From: Dmitry Kokorin Date: Fri, 11 Jun 2021 18:50:40 +0300 Subject: [PATCH] Python: markdown, iteration 2 --- python/markdown/markdown.py | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/python/markdown/markdown.py b/python/markdown/markdown.py index 6f83971..f9824e9 100644 --- a/python/markdown/markdown.py +++ b/python/markdown/markdown.py @@ -2,34 +2,29 @@ import re def parse(markdown): - res = '' - in_list = False + result = '' for line in markdown.splitlines(): - match = re.match(r'^(#{1,6}) (.*)', line) - if match: - level = len(match.group(1)) - res += f'{match.group(2)}' - continue - line = re.sub(r'__(.*)__', r'\1', line) line = re.sub(r'_(.*)_', r'\1', line) - match = re.match(r'\* (.*)', line) + match = re.match(r'^(#{1,6}) (.*)', line) if match: - if not in_list: - in_list = True - res += '' + match = re.match(r'\* (.*)', line) + if match: - res += '

' + line + '

' + if result.endswith(''): + result = result[:-5] + else: + result += '' + result += '
  • ' + match.group(1) + '
  • ' + continue - return res + result += '

    ' + line + '

    ' + + return result