From: Michael Tremer Date: Sun, 24 Mar 2024 09:03:04 +0000 (+0000) Subject: wiki: Process mentioning users with @ X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8d4a78e883ef48b508ecb4b5749eb63a47e5b85e;p=ipfire.org.git wiki: Process mentioning users with @ Signed-off-by: Michael Tremer --- diff --git a/src/backend/wiki.py b/src/backend/wiki.py index 3b227bd2..e77a27b2 100644 --- a/src/backend/wiki.py +++ b/src/backend/wiki.py @@ -974,6 +974,9 @@ class PrettyLinksExtension(markdown.extensions.Extension): # Create links to CVE md.preprocessors.register(CVELinksPreprocessor(md), "cve", 10) + # Link mentioned users + md.preprocessors.register(UserMentionPreprocessor(md), "user-mention", 10) + class BugzillaLinksPreprocessor(markdown.preprocessors.Preprocessor): regex = re.compile(r"(?:#(\d{5,}))", re.I) @@ -991,6 +994,28 @@ class CVELinksPreprocessor(markdown.preprocessors.Preprocessor): yield self.regex.sub(r"[CVE-\1](https://cve.mitre.org/cgi-bin/cvename.cgi?name=\1)", line) +class UserMentionPreprocessor(markdown.preprocessors.Preprocessor): + regex = re.compile(r"@(\w+)") + + def run(self, lines): + for line in lines: + yield self.regex.sub(self._replace, line) + + def _replace(self, m): + # Fetch the user's handle + uid, = m.groups() + + # Fetch the user + user = self.md.backend.accounts.get_by_uid(uid) + + # If the user was not found, we put back the matched text + if not user: + return m.group(0) + + # Link the user + return "[%s](//users/%s)" % (user, user.uid) + + class LinkedFilesExtractor(markdown.treeprocessors.Treeprocessor): """ Finds all Linked Files