Created
February 13, 2022 08:33
-
-
Save browneye1826/bc87f240cbd65866cd1e0072d7ec0006 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
BOLD = r"__(.*?)__", r"<strong>\1</strong>" | |
ITALIC = r"_(.*?)_", r"<em>\1</em>" | |
LIST = re.compile(r"^\* (.*)", re.M), r"<li>\1</li>" | |
END_LIST = re.compile(r"(<li>.*</li>)", re.S), r"<ul>\1</ul>" | |
HEADER = ( | |
(re.compile(r"^%s (.+)" % ("#" * i)), r"<h{0}>\1</h{0}>".format(i)) | |
for i in range(6, 0, -1) | |
) | |
PARAGRAPH = re.compile(r"^(?!<h|<li|<ul)(.*)", re.M), r"<p>\1</p>" | |
FLAT_TEXT = r"\n", r"" | |
OP = BOLD, ITALIC, LIST, END_LIST, *HEADER, PARAGRAPH, FLAT_TEXT | |
def parse(md: str) -> str: | |
"""Parses Markdown to HTML | |
Args: | |
md (str): the markdown string to be parsed | |
Returns: | |
Final HTML | |
""" | |
return [md := re.sub(*op, md) for op in OP][-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment