Add italics, link detection, lists to minimd

This commit is contained in:
David Hoppenbrouwers
2022-10-10 01:11:51 +02:00
parent 8db2d342ab
commit 8b2e02e42d
4 changed files with 67 additions and 14 deletions

View File

@@ -8,6 +8,12 @@ Agreper is a forum board with a focus on being easy to set up and manage.
### Linux ### Linux
Ensure you have the necessary packages, e.g. for Debian:
```
apt install git make sqlite3 python3-venv python3-pip
```
First clone or [download the latest release](https://github.com/Demindiro/agreper/archive/refs/tags/v0.1.tar.gz). First clone or [download the latest release](https://github.com/Demindiro/agreper/archive/refs/tags/v0.1.tar.gz).
Then setup with: Then setup with:

15
main.py
View File

@@ -8,7 +8,7 @@ import os, sys, subprocess
import passlib.hash, secrets import passlib.hash, secrets
import time import time
from datetime import datetime from datetime import datetime
import captcha, password import captcha, password, minimd
app = Flask(__name__) app = Flask(__name__)
db = DB(os.getenv('DB')) db = DB(os.getenv('DB'))
@@ -659,22 +659,11 @@ def utility_processor():
def format_time(t): def format_time(t):
return datetime.utcfromtimestamp(t / 10 ** 9).replace(microsecond=0) return datetime.utcfromtimestamp(t / 10 ** 9).replace(microsecond=0)
def minimd(text):
# Replace angle brackets to prevent XSS
# Also replace ampersands to prevent surprises.
text = text.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
# Split into paragraphs
paragraphs = map(lambda l: l.strip('\n'), text.split('\n\n'))
paragraphs = map(lambda l: l if not l.startswith(' ') else f'<pre>{l}</pre>', paragraphs)
paragraphs = map(lambda l: f'<p>{l}</p>', paragraphs)
# Glue together again
return ''.join(paragraphs)
return { return {
'format_since': format_since, 'format_since': format_since,
'format_time': format_time, 'format_time': format_time,
'format_until': format_until, 'format_until': format_until,
'minimd': minimd, 'minimd': minimd.html,
} }

58
minimd.py Executable file
View File

@@ -0,0 +1,58 @@
#!/usr/bin/env python3
import re
# 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]\.) .*')
def html(text):
# Replace angle brackets to prevent XSS
# Also replace ampersands to prevent surprises.
text = text.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
html = ['<p>']
lines = text.split('\n')
in_code = False
in_list = False
for l in lines:
if l == '':
in_list = False
if in_code:
html.append('</pre>')
in_code = False
html.append('</p><p>')
continue
if l.startswith(' '):
in_list = False
l = l[2:]
if not in_code:
html.append('<pre>')
in_code = True
html.append(l)
continue
if in_code:
html.append('</pre>')
in_code = False
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>')
in_list = True
else:
in_list = False
html.append(l)
if in_code:
html.append('</pre>')
html.append('</p>')
return ''.join(html)
if __name__ == '__main__':
import sys
print(html(sys.stdin.read()))

View File

@@ -9,7 +9,7 @@
{%- endmacro -%} {%- endmacro -%}
{% block content -%} {% block content -%}
<p>{{ description }}</p> <p>{{ minimd(description) | safe }}</p>
<p><a href="{{ url_for('new_thread', forum_id = forum_id) }}">Create thread</a></p> <p><a href="{{ url_for('new_thread', forum_id = forum_id) }}">Create thread</a></p>
{{- nav() -}} {{- nav() -}}
<table> <table>