]> git.ipfire.org Git - ipfire.org.git/commitdiff
wiki: Embed images
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 18 Nov 2018 19:01:19 +0000 (19:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 18 Nov 2018 19:01:19 +0000 (19:01 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/wiki.py

index c963b9a1ca85b644dd1d2a16fbf1708603576ed7..3d105f7290fd25ef4ec8c7e498726e7918494dca 100644 (file)
@@ -5,6 +5,7 @@ import io
 import logging
 import os.path
 import re
+import urllib.parse
 
 from . import misc
 from . import util
@@ -122,6 +123,13 @@ class Wiki(misc.Object):
                        mimetype, blob_id, size) VALUES(%s,  %s, %s, %s, %s, %s, %s) RETURNING *", path,
                        filename, author.uid, address, mimetype, blob.id, len(data))
 
+       def find_image(self, path, filename):
+               for p in (path, os.path.dirname(path)):
+                       file = self.get_file_by_path(os.path.join(p, filename))
+
+                       if file and file.is_image():
+                               return file
+
 
 class Page(misc.Object):
        def init(self, id, data=None):
@@ -185,6 +193,44 @@ class Page(misc.Object):
        def _render(self, text):
                logging.debug("Rendering %s" % self)
 
+               print(text)
+
+               # Link images
+               replacements = []
+               for match in re.finditer(r"!\[(.*)\]\((.*)\)", text):
+                       alt_text, url = match.groups()
+
+                       # Skip any absolute and external URLs
+                       if url.startswith("/") or url.startswith("https://") or url.startswith("http://"):
+                               continue
+
+                       # Try to split query string
+                       url, delimiter, qs = url.partition("?")
+
+                       # Parse query arguments
+                       args = urllib.parse.parse_qs(qs)
+
+                       # Find image
+                       file = self.backend.wiki.find_image(self.page, url)
+                       if not file:
+                               continue
+
+                       # Scale down the image if not already done
+                       if not "s" in args:
+                               args["s"] = "768"
+
+                       # Format URL
+                       url = "%s?%s" % (file.url, urllib.parse.urlencode(args))
+
+                       replacements.append((match.span(), alt_text, url))
+
+               # Apply all replacements
+               for (start, end), alt_text, url in reversed(replacements):
+                       text = text[:start] + "![%s](%s)" % (alt_text, url) + text[end:]
+
+               print(text)
+
+               # Add wiki links
                patterns = (
                        (r"\[\[([\w\d\/]+)(?:\|([\w\d\s]+))\]\]", r"/\1", r"\2", None, None),
                        (r"\[\[([\w\d\/\-]+)\]\]", r"/\1", r"\1", self.backend.wiki.get_page_title, r"\1"),