From c683a1ff6a79bc6c5dbd80b5de8bd70fb26febe9 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 7 May 2019 19:07:12 +0100 Subject: [PATCH] wiki: Add support for Interwiki links Signed-off-by: Michael Tremer --- src/backend/wiki.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ src/scss/style.scss | 4 ++++ 2 files changed, 50 insertions(+) diff --git a/src/backend/wiki.py b/src/backend/wiki.py index c50d8ea1..ebb57d81 100644 --- a/src/backend/wiki.py +++ b/src/backend/wiki.py @@ -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("" % icon) + + s.append("""%s""" % (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), diff --git a/src/scss/style.scss b/src/scss/style.scss index c0280318..23116f4a 100644 --- a/src/scss/style.scss +++ b/src/scss/style.scss @@ -296,6 +296,10 @@ section { @extend .table-sm; @extend .table-striped; } + + .link-external { + @extend .text-secondary; + } } hr.divider { -- 2.47.3