summaryrefslogtreecommitdiffstats
path: root/aurweb
diff options
context:
space:
mode:
authorFrédéric Mangano-Tarumi <fmang@mg0.fr>2020-02-01 19:07:41 +0100
committerLukas Fleischer <lfleischer@archlinux.org>2020-02-02 12:12:43 +0100
commit199f34e42e78ca97f154a4880b77b3f1d01fa9da (patch)
tree41f90558369ee8a7491bec31c456a0ee2562aa32 /aurweb
parent0fc69e96bdd1197ca2b91bcd16a3064366954c06 (diff)
downloadaur-199f34e42e78ca97f154a4880b77b3f1d01fa9da.tar.gz
aur-199f34e42e78ca97f154a4880b77b3f1d01fa9da.tar.xz
rendercomment: safer auto-linkification of URLs
Fixes a few edge cases: - URLs within code blocks used to get redundant <> added, breaking bash code snippets like `curl https://...` into `curl <https://...>`. - Links written with markdown's <https://...> syntax also used to get an extra pair of brackets. Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
Diffstat (limited to 'aurweb')
-rwxr-xr-xaurweb/scripts/rendercomment.py19
1 files changed, 11 insertions, 8 deletions
diff --git a/aurweb/scripts/rendercomment.py b/aurweb/scripts/rendercomment.py
index 5c597481..346ccff1 100755
--- a/aurweb/scripts/rendercomment.py
+++ b/aurweb/scripts/rendercomment.py
@@ -13,17 +13,20 @@ repo_path = aurweb.config.get('serve', 'repo-path')
commit_uri = aurweb.config.get('options', 'commit_uri')
-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):
+ """
+ Turn URLs into links, even without explicit markdown.
+ Do not linkify URLs in code blocks.
+ """
+ # Captures http(s) and ftp URLs until the first non URL-ish character.
+ # Excludes trailing punctuation.
+ _urlre = (r'(\b(?:https?|ftp):\/\/[\w\/\#~:.?+=&%@!\-;,]+?'
+ r'(?=[.:?\-;,]*(?:[^\w\/\#~:.?+=&%@!\-;,]|$)))')
-class LinkifyExtension(markdown.extensions.Extension):
def extendMarkdown(self, md, md_globals):
- md.preprocessors.add('linkify', LinkifyPreprocessor(md), '_end')
+ processor = markdown.inlinepatterns.AutolinkInlineProcessor(self._urlre, md)
+ md.inlinePatterns.add('linkify', processor, '_end')
class FlysprayLinksPreprocessor(markdown.preprocessors.Preprocessor):