]> git.ipfire.org Git - ipfire.org.git/blobdiff - src/backend/wiki.py
wiki: Add preview for editing pages
[ipfire.org.git] / src / backend / wiki.py
index 51aa2b26a26cb42080a840dee0239620371a1e8f..e3275c2a3472b8a1e5b724bd9451d066f33a2cc4 100644 (file)
@@ -213,20 +213,13 @@ class Wiki(misc.Object):
                        if file and file.is_image():
                                return file
 
+       def render(self, path, text):
+               r = WikiRenderer(self.backend, path)
 
-class Page(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+)>(.+?)(?:\|(.+?))?\]\]")
+               return r.render(text)
 
-       # Mail link
-       email_link = re.compile(r"\[\[([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)(?:\|(.+?))?\]\]")
 
+class Page(misc.Object):
        def init(self, id, data=None):
                self.id = id
                self.data = data
@@ -296,127 +289,13 @@ class Page(misc.Object):
                if self.data.author_uid:
                        return self.backend.accounts.get_by_uid(self.data.author_uid)
 
-       def _render_wiki_link(self, m):
-               path, alias = m.groups()
-
-               # Allow relative links
-               if not path.startswith("/"):
-                       path = os.path.join(self.page, path)
-
-               # Normalise links
-               path = os.path.normpath(path)
-
-               return """<a href="%s">%s</a>""" % (
-                       path,
-                       alias or self.backend.wiki.get_page_title(path),
-               )
-
-       def _render_external_link(self, m):
-               url, alias = m.groups()
-
-               return """<a class="link-external" href="%s">%s</a>""" % (url, alias or url)
-
-       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_email_link(self, m):
-               address, alias = m.groups()
-
-               return """<a class="link-external" href="mailto:%s">%s</a>""" \
-                       % (address, alias or address)
-
-       def _render(self, text):
-               logging.debug("Rendering %s" % self)
-
-               # 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(), file, alt_text, url))
-
-               # Apply all replacements
-               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 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
-               return self.backend.blog._render_text(text, lang="markdown")
-
        @property
        def markdown(self):
                return self.data.markdown or ""
 
        @property
        def html(self):
-               return self._render(self.markdown)
+               return self.backend.wiki.render(self.page, self.markdown)
 
        @property
        def timestamp(self):
@@ -600,3 +479,134 @@ class File(misc.Object):
                self.memcache.set(cache_key, thumbnail)
 
                return thumbnail
+
+
+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-.]+)(?:\|(.+?))?\]\]")
+
+       def init(self, path):
+               self.path = path
+
+       def _render_wiki_link(self, m):
+               path, alias = m.groups()
+
+               # Allow relative links
+               if not path.startswith("/"):
+                       path = os.path.join(self.path, path)
+
+               # Normalise links
+               path = os.path.normpath(path)
+
+               return """<a href="%s">%s</a>""" % (
+                       path,
+                       alias or self.backend.wiki.get_page_title(path),
+               )
+
+       def _render_external_link(self, m):
+               url, alias = m.groups()
+
+               return """<a class="link-external" href="%s">%s</a>""" % (url, alias or url)
+
+       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_email_link(self, m):
+               address, alias = m.groups()
+
+               return """<a class="link-external" href="mailto:%s">%s</a>""" \
+                       % (address, alias or address)
+
+       def render(self, text):
+               logging.debug("Rendering %s" % self.path)
+
+               # 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.path, 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(), file, alt_text, url))
+
+               # Apply all replacements
+               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 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
+               return self.backend.blog._render_text(text, lang="markdown")