]> git.ipfire.org Git - ipfire.org.git/commitdiff
wiki: Process mentioning users with @
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 24 Mar 2024 09:03:04 +0000 (09:03 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 24 Mar 2024 09:03:04 +0000 (09:03 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/wiki.py

index 3b227bd2a122a051415d73d611a218fbd0e9b435..e77a27b24cb4f7738232d1acc8484be785c2cbbb 100644 (file)
@@ -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