From db1e727ed88cd6668d70b2d3ebbddd1dca53efef Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 7 May 2019 22:16:48 +0100 Subject: [PATCH] wiki: Handle wiki links Signed-off-by: Michael Tremer --- src/backend/wiki.py | 51 +++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/src/backend/wiki.py b/src/backend/wiki.py index eb889bad..d57c70c6 100644 --- a/src/backend/wiki.py +++ b/src/backend/wiki.py @@ -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 """%s""" % ( + 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") -- 2.39.2