]> git.ipfire.org Git - ipfire.org.git/commitdiff
wiki: Add support for Interwiki links
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 7 May 2019 18:07:12 +0000 (19:07 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 7 May 2019 18:07:12 +0000 (19:07 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/wiki.py
src/scss/style.scss

index c50d8ea1590900a248663f1ff8949003c91a4dfa..ebb57d81549cbd7848dbe2259590bd4756fdf343 100644 (file)
@@ -11,6 +11,12 @@ 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)
@@ -267,6 +273,43 @@ class Page(misc.Object):
                if self.data.author_uid:
                        return self.backend.accounts.get_by_uid(self.data.author_uid)
 
+       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
+
+               # Name of the page
+               name = m.group(2)
+
+               # Expand URL
+               url = url % {
+                       "name" : name,
+                       "url"  : urllib.parse.quote(name),
+               }
+
+               # Get alias (if present)
+               alias = m.group(3)
+
+               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(self, text):
                logging.debug("Rendering %s" % self)
 
@@ -303,6 +346,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 interwiki links
+               text = re.sub(r"\[\[(\w+)>(.+?)(?:\|(.+?))?\]\]", self._render_interwiki_link, text)
+
                # Add wiki links
                patterns = (
                        (r"\[\[([\w\d\/\-\.]+)(?:\|(.+?))\]\]", r"\1", r"\2", None, True),
index c0280318b807785ffdb44d090da4be0fefb813e6..23116f4aff57af3051f6d8ffe508df1991e501e3 100644 (file)
@@ -296,6 +296,10 @@ section {
                @extend .table-sm;
                @extend .table-striped;
        }
+
+       .link-external {
+               @extend .text-secondary;
+       }
 }
 
 hr.divider {