diff options
author | Lukas Fleischer <lfleischer@archlinux.org> | 2017-04-23 14:48:01 +0200 |
---|---|---|
committer | Lukas Fleischer <lfleischer@archlinux.org> | 2017-04-23 18:43:26 +0200 |
commit | a9ac385cb90d0251253a2a3925f72a71af52f97b (patch) | |
tree | fbac139d63c8719ab9c1131c940fbd6b36e5d460 | |
parent | 9aa4203c7efd5ef1015eb32eca5e0764a5afe183 (diff) | |
download | aur-a9ac385cb90d0251253a2a3925f72a71af52f97b.tar.gz aur-a9ac385cb90d0251253a2a3925f72a71af52f97b.tar.xz |
Reintroduce backwards-compatible hyperlink syntax
Before switching to the new comment rendering script and Markdown, no
special syntax was needed to make URLs clickable. Reintroduce this
feature and automatically detect links in addition to the hyperlink
syntax already supported by Markdown.
Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
-rwxr-xr-x | aurweb/scripts/rendercomment.py | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/aurweb/scripts/rendercomment.py b/aurweb/scripts/rendercomment.py index 7e8a16b8..c8921f8f 100755 --- a/aurweb/scripts/rendercomment.py +++ b/aurweb/scripts/rendercomment.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 +import re import sys import bleach import markdown @@ -7,6 +8,19 @@ import markdown import aurweb.db +class LinkifyPreprocessor(markdown.preprocessors.Preprocessor): + _urlre = re.compile(r'(\b(?:https?|ftp):\/\/[\w\/\#~:.?+=&%@!\-;,]+?' + r'(?=[.:?\-;,]*(?:[^\w\/\#~:.?+=&%@!\-;,]|$)))') + + def run(self, lines): + return [self._urlre.sub(r'<\1>', line) for line in lines] + + +class LinkifyExtension(markdown.extensions.Extension): + def extendMarkdown(self, md, md_globals): + md.preprocessors.add('linkify', LinkifyPreprocessor(md), '_end') + + def get_comment(conn, commentid): cur = conn.execute('SELECT Comments FROM PackageComments WHERE ID = ?', [commentid]) @@ -24,7 +38,7 @@ def main(): conn = aurweb.db.Connection() text = get_comment(conn, commentid) - html = markdown.markdown(text, extensions=['nl2br']) + html = markdown.markdown(text, extensions=['nl2br', LinkifyExtension()]) allowed_tags = bleach.sanitizer.ALLOWED_TAGS + ['p', 'br'] html = bleach.clean(html, tags=allowed_tags) save_rendered_comment(conn, commentid, html) |