from . import util
from .decorators import *
-INTERWIKIS = {
- "google" : ("https://www.google.com/search?q=%(url)s", None, "fab fa-google"),
- "rfc" : ("https://tools.ietf.org/html/rfc%(name)s", "RFC %s", None),
- "wp" : ("https://en.wikipedia.org/wiki/%(name)s", None, "fab fa-wikipedia-w"),
-}
-
class Wiki(misc.Object):
def _get_pages(self, query, *args):
res = self.db.query(query, *args)
class WikiRenderer(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)\:\/\/.+?)(?:\|(.+?))?\]\]")
-
- # Interwiki links e.g. [[wp>IPFire]]
- interwiki_link = re.compile(r"\[\[(\w+)>(.+?)(?:\|(.+?))?\]\]")
-
- # Mail link
- email_link = re.compile(r"\[\[([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)(?:\|(.+?))?\]\]")
+ schemas = (
+ "ftp://",
+ "git://",
+ "http://",
+ "https://",
+ "rsync://",
+ "sftp://",
+ "ssh://",
+ "webcal://",
+ )
+
+ # Links
+ links = re.compile(r"<a href=\"(.*?)\">(.*?)</a>")
# Images
images = re.compile(r"<img src=\"(.*?)\" alt=\"(.*?)\" (?:title=\"(.*?)\" )?/>")
def init(self, path):
self.path = path
- def _render_wiki_link(self, m):
- path, alias = m.groups()
-
- path = self.backend.wiki.make_path(self.path, 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()
-
- return """<a class="link-external" href="%s">%s</a>""" % (url, alias or url)
+ def _render_link(self, m):
+ url, text = m.groups()
- def _render_interwiki_link(self, m):
- wiki = m.group(1)
- if not wiki:
- return
-
- # Retrieve URL
- try:
- url, repl, icon = INTERWIKIS[wiki]
- except KeyError:
- logging.warning("Invalid interwiki: %s" % wiki)
- return
+ # Emails
+ if "@" in url:
+ # Strip mailto:
+ if url.startswith("mailto:"):
+ url = url[7:]
- # Name of the page
- name = m.group(2)
+ return """<a class="link-external" href="mailto:%s">%s</a>""" % \
+ (url, text or url)
- # Expand URL
- url = url % {
- "name" : name,
- "url" : urllib.parse.quote(name),
- }
+ # External Links
+ for schema in self.schemas:
+ if url.startswith(schema):
+ return """<a class="link-external" href="%s">%s</a>""" % \
+ (url, text or url)
- # Get alias (if present)
- alias = m.group(3)
+ # Everything else must be an internal link
+ path = self.backend.wiki.make_path(self.path, url)
- if not alias and repl:
- alias = repl % name
-
- # Put everything together
- s = []
-
- if icon:
- s.append("<span class=\"%s\"></span>" % icon)
-
- s.append("""<a class="link-external" href="%s">%s</a>""" % (url, alias or name))
-
- return " ".join(s)
-
- def _render_email_link(self, m):
- address, alias = m.groups()
-
- return """<a class="link-external" href="mailto:%s">%s</a>""" \
- % (address, alias or address)
+ return """<a href="%s">%s</a>""" % \
+ (path, text or self.backend.wiki.get_page_title(path))
def _render_image(self, m):
url, alt_text, caption = m.groups()
def render(self, text):
logging.debug("Rendering %s" % self.path)
- # 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 external links
- text = self.external_link.sub(self._render_external_link, text)
-
- # Handle email links
- text = self.email_link.sub(self._render_email_link, text)
-
# Borrow this from the blog
text = self.backend.blog._render_text(text, lang="markdown")
+ # Postprocess links
+ text = self.links.sub(self._render_link, text)
+
# Postprocess images to <figure>
text = self.images.sub(self._render_image, text)