import logging
import os.path
import re
+import urllib.parse
from . import misc
from . import util
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):
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] + "" % (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"),