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)\:\/\/.+?)(?:\|(.+?))\]\]")
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()
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)
# 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")