use markdown2, add forced login. Added breadcrumbs

This commit is contained in:
Ville Rantanen
2023-07-23 20:23:48 +03:00
parent 09f56bd1fe
commit 9437e64936
13 changed files with 771 additions and 527 deletions

View File

@@ -1,58 +1,70 @@
#!/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_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"([ |\n])(https?://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-]))[^\)]"
)
def html(text):
text = RE_PLAINURL.sub(r'\1[\2](\2)', text)
return markdown2.markdown(text)
def html_old(text):
# Replace angle brackets to prevent XSS
# Also replace ampersands to prevent surprises.
text = text.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
text = text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
html = ['<p>']
lines = text.split('\n')
html = ["<p>"]
lines = text.split("\n")
in_code = False
in_list = False
for l in lines:
if l == '':
if l == "":
in_list = False
if in_code:
html.append('</pre>')
html.append("</pre>")
in_code = False
html.append('</p><p>')
html.append("</p><p>")
continue
if l.startswith(' '):
if l.startswith(" "):
in_list = False
l = l[2:]
if not in_code:
html.append('<pre>')
html.append("<pre>")
in_code = True
html.append(l)
continue
if in_code:
html.append('</pre>')
html.append("</pre>")
in_code = False
l = RE_EM.sub(r'<em>\1</em>', l)
l = RE_EM.sub(r"<em>\1</em>", l)
l = RE_URL.sub(r'<a href="\1">\1</a>', l)
if RE_LIST.match(l):
if in_list:
html.append('<br>')
html.append("<br>")
in_list = True
else:
in_list = False
html.append(l)
if in_code:
html.append('</pre>')
html.append('</p>')
return '\n'.join(html)
html.append("</pre>")
html.append("</p>")
return "\n".join(html)
if __name__ == '__main__':
if __name__ == "__main__":
import sys
print(html(sys.stdin.read()))
print(html_old(sys.stdin.read()))