From 4ddad3e56d0bc72f6ce2cb3f81ab7ff858c11a2e Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 16 Jul 2019 16:58:31 +0100 Subject: [PATCH] wiki: Postprocess links This removes all custom lingu from the wiki and only makes links fancy when we need it. Signed-off-by: Michael Tremer --- src/backend/wiki.py | 115 +++++++++++++------------------------------- 1 file changed, 34 insertions(+), 81 deletions(-) diff --git a/src/backend/wiki.py b/src/backend/wiki.py index 5c4d0499..fb597887 100644 --- a/src/backend/wiki.py +++ b/src/backend/wiki.py @@ -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"(.*?)") # Images images = re.compile(r"\"(.*?)\"") @@ -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 """%s""" % ( - path, - alias or self.backend.wiki.get_page_title(path), - ) - - def _render_external_link(self, m): - url, alias = m.groups() - - return """%s""" % (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 """%s""" % \ + (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 """%s""" % \ + (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("" % icon) - - s.append("""%s""" % (url, alias or name)) - - return " ".join(s) - - def _render_email_link(self, m): - address, alias = m.groups() - - return """%s""" \ - % (address, alias or address) + return """%s""" % \ + (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
text = self.images.sub(self._render_image, text) -- 2.47.2