From 8b2e02e42d7350c8b0d772d0b42e63586aecfa64 Mon Sep 17 00:00:00 2001
From: David Hoppenbrouwers
Date: Mon, 10 Oct 2022 01:11:51 +0200
Subject: [PATCH] Add italics, link detection, lists to minimd
---
README.md | 6 +++++
main.py | 15 ++----------
minimd.py | 58 ++++++++++++++++++++++++++++++++++++++++++++
templates/forum.html | 2 +-
4 files changed, 67 insertions(+), 14 deletions(-)
create mode 100755 minimd.py
diff --git a/README.md b/README.md
index 0f7bd73..df23156 100644
--- a/README.md
+++ b/README.md
@@ -8,6 +8,12 @@ Agreper is a forum board with a focus on being easy to set up and manage.
### 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).
Then setup with:
diff --git a/main.py b/main.py
index 71b8db7..58020cd 100644
--- a/main.py
+++ b/main.py
@@ -8,7 +8,7 @@ import os, sys, subprocess
import passlib.hash, secrets
import time
from datetime import datetime
-import captcha, password
+import captcha, password, minimd
app = Flask(__name__)
db = DB(os.getenv('DB'))
@@ -659,22 +659,11 @@ def utility_processor():
def format_time(t):
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('&', '&').replace('<', '<').replace('>', '>')
- # 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'{l}', paragraphs)
- paragraphs = map(lambda l: f'{l}
', paragraphs)
- # Glue together again
- return ''.join(paragraphs)
-
return {
'format_since': format_since,
'format_time': format_time,
'format_until': format_until,
- 'minimd': minimd,
+ 'minimd': minimd.html,
}
diff --git a/minimd.py b/minimd.py
new file mode 100755
index 0000000..3531a66
--- /dev/null
+++ b/minimd.py
@@ -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('&', '&').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 ''.join(html)
+
+
+if __name__ == '__main__':
+ import sys
+ print(html(sys.stdin.read()))
+
diff --git a/templates/forum.html b/templates/forum.html
index 102a386..7c4350f 100644
--- a/templates/forum.html
+++ b/templates/forum.html
@@ -9,7 +9,7 @@
{%- endmacro -%}
{% block content -%}
-{{ description }}
+{{ minimd(description) | safe }}
Create thread
{{- nav() -}}