#!/usr/bin/env python3 import re import markdown2 # https://stackoverflow.com/a/6041965 RE_URL = re.compile( r"(https?://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-]))" ) RE_EM = re.compile(r"\*(.*?)\*") RE_LIST = re.compile(r"(-|[0-9]\.) .*") RE_PLAINURL = re.compile( r"(?P
^|\s|\n)(?Phttps?://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-]))(?P\s|\n|$)"
)

def html(text):
    text = RE_PLAINURL.sub(r'\g
[\g](\g)\g', text)
    return markdown2.markdown(text)


def html_old(text):
    # Replace angle brackets to prevent XSS
    # Also replace ampersands to prevent surprises.
    text = text.replace("&", "&").replace("<", "<").replace(">", ">")

    html = ["

"] lines = text.split("\n") in_code = False in_list = False for l in lines: if l == "": in_list = False if in_code: html.append("

") in_code = False html.append("

") continue if l.startswith(" "): in_list = False l = l[2:] if not in_code: html.append("

")
                in_code = True
            html.append(l)
            continue
        if in_code:
            html.append("
") in_code = False l = RE_EM.sub(r"\1", l) l = RE_URL.sub(r'\1', l) if RE_LIST.match(l): if in_list: html.append("
") in_list = True else: in_list = False html.append(l) if in_code: html.append("
") html.append("

") return "\n".join(html) if __name__ == "__main__": import sys print(html_old(sys.stdin.read()))