- def _render(self, text):
- logging.debug("Rendering %s" % self)
-
- # Link images
- replacements = []
- for match in re.finditer(r"!\[(.*?)\]\((.*?)\)", text):
- alt_text, url = match.groups()
-
- # Skip any absolute and external URLs
- if url.startswith("/") or url.startswith("https://") or url.startswith("http://"):
- continue
-
- # Try to split query string
- url, delimiter, qs = url.partition("?")
-
- # Parse query arguments
- args = urllib.parse.parse_qs(qs)
-
- # Find image
- file = self.backend.wiki.find_image(self.page, url)
- if not file:
- continue
-
- # Scale down the image if not already done
- if not "s" in args:
- args["s"] = "768"
-
- # Format URL
- url = "%s?%s" % (file.url, urllib.parse.urlencode(args))
-
- replacements.append((match.span(), file, alt_text, url))
-
- # Apply all replacements
- 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:]
-
- # 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),
-
- # External links
- (r"\[\[((?:ftp|git|https?|rsync|sftp|ssh|webcal)\:\/\/.+?)(?:\|(.+?))\]\]",
- r"\1", r"\2", None, False),
-
- # Mail
- (r"\[\[([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)\]\]",
- r"\1", r"\1", None, False),
- )
-
- 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")
-