]> git.ipfire.org Git - ipfire.org.git/commitdiff
wiki: Postprocess links
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 16 Jul 2019 15:58:31 +0000 (16:58 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 16 Jul 2019 15:58:31 +0000 (16:58 +0100)
This removes all custom lingu from the wiki and only makes
links fancy when we need it.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/wiki.py

index 5c4d0499cb713b19e7e4108b66675fb92e537b3c..fb597887b00b0569f039caf9d38cfcf24bfbe252 100644 (file)
@@ -11,12 +11,6 @@ from . import misc
 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)
@@ -492,17 +486,19 @@ class File(misc.Object):
 
 
 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=\"(.*?)\" )?/>")
@@ -510,63 +506,29 @@ class WikiRenderer(misc.Object):
        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()
@@ -602,21 +564,12 @@ class WikiRenderer(misc.Object):
        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)