]> git.ipfire.org Git - ipfire.org.git/commitdiff
wiki: Handle wiki links
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 7 May 2019 21:16:48 +0000 (22:16 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 7 May 2019 21:16:48 +0000 (22:16 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/wiki.py

index eb889bad803d8c4591e1a5ec8fba57fc1a6d9bd4..d57c70c6af3d3ef54e4a1b5d787b3f288137b905 100644 (file)
@@ -204,6 +204,9 @@ class Wiki(misc.Object):
 
 
 class Page(misc.Object):
+       # Wiki links
+       wiki_link = re.compile(r"\[\[([\w\d\/\-\.]+)(?:\|(.+?))?\]\]")
+
        # External links
        external_link = re.compile(r"\[\[((?:ftp|git|https?|rsync|sftp|ssh|webcal)\:\/\/.+?)(?:\|(.+?))\]\]")
 
@@ -282,6 +285,21 @@ class Page(misc.Object):
                if self.data.author_uid:
                        return self.backend.accounts.get_by_uid(self.data.author_uid)
 
+       def _render_wiki_link(self, m):
+               path, alias = m.groups()
+
+               # Allow relative links
+               if not path.startswith("/"):
+                       path = os.path.join(self.page, path)
+
+               # Normalise links
+               path = os.path.normpath(path)
+
+               return """<a href="%s">%s</a>""" % (
+                       path,
+                       alias or self.backend.wiki.get_page_title(path),
+               )
+
        def _render_external_link(self, m):
                url, alias = m.groups()
 
@@ -366,6 +384,9 @@ class Page(misc.Object):
                for (start, end), file, alt_text, url in reversed(replacements):
                        text = text[:start] + "[![%s](%s)](%s?action=detail)" % (alt_text, url, file.url) + text[end:]
 
+               # Handle wiki links
+               text = self.wiki_link.sub(self._render_wiki_link, text)
+
                # Handle interwiki links
                text = self.interwiki_link.sub(self._render_interwiki_link, text)
 
@@ -375,36 +396,6 @@ class Page(misc.Object):
                # Handle email links
                text = self.email_link.sub(self._render_email_link, text)
 
-               # Add wiki links
-               patterns = (
-                       (r"\[\[([\w\d\/\-\.]+)(?:\|(.+?))\]\]", r"\1", r"\2", None, True),
-                       (r"\[\[([\w\d\/\-\.]+)\]\]", r"\1", r"\1", self.backend.wiki.get_page_title, True),
-               )
-
-               for pattern, link, title, repl, internal in patterns:
-                       replacements = []
-
-                       for match in re.finditer(pattern, text):
-                               l = match.expand(link)
-                               t = match.expand(title)
-
-                               if internal:
-                                       # Allow relative links
-                                       if not l.startswith("/"):
-                                               l = os.path.join(self.page, l)
-
-                                       # Normalise links
-                                       l = os.path.normpath(l)
-
-                               if callable(repl):
-                                       t = repl(l) or t
-
-                               replacements.append((match.span(), t or l, l))
-
-                       # Apply all replacements
-                       for (start, end), t, l in reversed(replacements):
-                               text = text[:start] + "[%s](%s)" % (t, l) + text[end:]
-
                # Borrow this from the blog
                return self.backend.blog._render_text(text, lang="markdown")