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)
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)
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),