[2/4] Add Markdown support to package comments

Message ID 20170423165302.25025-3-lfleischer@archlinux.org
State Accepted, archived
Headers show
Series Add Markdown syntax to comments | expand

Commit Message

Lukas Fleischer April 23, 2017, 4:53 p.m. UTC
Support Markdown syntax in package comments. Among other things, this
makes it easier to paste command line output and patches.

Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
---
 aurweb/scripts/rendercomment.py | 8 +++++---
 test/t2600-rendercomment.sh     | 3 ++-
 2 files changed, 7 insertions(+), 4 deletions(-)

Patch

diff --git a/aurweb/scripts/rendercomment.py b/aurweb/scripts/rendercomment.py
index 593cd36..7e8a16b 100755
--- a/aurweb/scripts/rendercomment.py
+++ b/aurweb/scripts/rendercomment.py
@@ -2,6 +2,7 @@ 
 
 import sys
 import bleach
+import markdown
 
 import aurweb.db
 
@@ -22,9 +23,10 @@  def main():
 
     conn = aurweb.db.Connection()
 
-    html = get_comment(conn, commentid)
-    html = html.replace('\n', '<br>')
-    html = bleach.clean(html, tags=['br'])
+    text = get_comment(conn, commentid)
+    html = markdown.markdown(text, extensions=['nl2br'])
+    allowed_tags = bleach.sanitizer.ALLOWED_TAGS + ['p', 'br']
+    html = bleach.clean(html, tags=allowed_tags)
     save_rendered_comment(conn, commentid, html)
 
     conn.commit()
diff --git a/test/t2600-rendercomment.sh b/test/t2600-rendercomment.sh
index 8d79336..50a5adb 100755
--- a/test/t2600-rendercomment.sh
+++ b/test/t2600-rendercomment.sh
@@ -11,7 +11,8 @@  test_expect_success 'Test comment rendering.' '
 	EOD
 	"$RENDERCOMMENT" 1 &&
 	cat <<-EOD >expected &&
-	Hello world!<br>This is a comment.
+	<p>Hello world!<br>
+	This is a comment.</p>
 	EOD
 	cat <<-EOD | sqlite3 aur.db >actual &&
 	SELECT RenderedComment FROM PackageComments WHERE ID = 1;