]> git.ipfire.org Git - ipfire.org.git/commitdiff
docs: Copy page handler from wiki
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 27 Jun 2023 08:17:24 +0000 (08:17 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 27 Jun 2023 08:17:24 +0000 (08:17 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/web/__init__.py
src/web/docs.py [new file with mode: 0644]

index a95a0344660bb0fc6f6726e885ffe1b387dce45d..442714518168a682da1adfdda784bac8144cfafe 100644 (file)
@@ -84,6 +84,7 @@ web_PYTHON = \
        src/web/base.py \
        src/web/blog.py \
        src/web/boot.py \
+       src/web/docs.py \
        src/web/donate.py \
        src/web/downloads.py \
        src/web/fireinfo.py \
index e3dc4a2b80081eacd88d8892b913c259de06e494..633d923e2d42617cbf80a7721bfb5ed4acd9292d 100644 (file)
@@ -17,6 +17,7 @@ from .handlers import *
 from . import auth
 from . import blog
 from . import boot
+from . import docs
 from . import donate
 from . import downloads
 from . import fireinfo
@@ -135,6 +136,9 @@ class Application(tornado.web.Application):
                        (r"/blog/([0-9a-z\-\._]+)/edit", blog.EditHandler),
                        (r"/blog/([0-9a-z\-\._]+)/publish", blog.PublishHandler),
 
+                       # Docs
+                       (r"/docs([A-Za-z0-9\-_\/]+)?", docs.PageHandler),
+
                        # Downloads
                        (r"/downloads", downloads.IndexHandler),
                        (r"/downloads/mirrors", downloads.MirrorsHandler),
diff --git a/src/web/docs.py b/src/web/docs.py
new file mode 100644 (file)
index 0000000..0e87bb6
--- /dev/null
@@ -0,0 +1,88 @@
+#!/usr/bin/python3
+
+import difflib
+import tornado.web
+
+from . import base
+
+class PageHandler(base.BaseHandler):
+       @property
+       def action(self):
+               return self.get_argument("action", None)
+
+       def write_error(self, status_code, **kwargs):
+               # Render a custom page for 404
+               if status_code == 404:
+                       self.render("wiki/404.html", **kwargs)
+                       return
+
+               # Otherwise raise this to one layer above
+               super().write_error(status_code, **kwargs)
+
+       @tornado.web.removeslash
+       def get(self, path):
+               if path is None:
+                       path = "/"
+
+               # Check permissions
+               if not self.backend.wiki.check_acl(path, self.current_user):
+                       raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user))
+
+               # Check if we are asked to render a certain revision
+               revision = self.get_argument("revision", None)
+
+               # Fetch the wiki page
+               page = self.backend.wiki.get_page(path, revision=revision)
+
+               # Diff
+               if self.action == "diff":
+                       # Get both revisions
+                       a = self.get_argument("a")
+                       b = self.get_argument("b")
+
+                       # Fetch both versions of the page
+                       a = self.backend.wiki.get_page(path, revision=a)
+                       b = self.backend.wiki.get_page(path, revision=b)
+                       if not a or not b:
+                               raise tornado.web.HTTPError(404)
+
+                       # Cannot render a diff for the identical page
+                       if a == b:
+                               raise tornado.web.HTTPError(400)
+
+                       # Make sure that b is newer than a
+                       if a > b:
+                               a, b = b, a
+
+                       self.render("wiki/diff.html", page=page, a=a, b=b)
+                       return
+
+               # Restore
+               elif self.action == "restore":
+                       self.render("wiki/confirm-restore.html", page=page)
+                       return
+
+               # Revisions
+               elif self.action == "revisions":
+                       self.render("wiki/revisions.html", page=page)
+                       return
+
+               # If the page does not exist, we send 404
+               if not page or page.was_deleted():
+                       # Handle /start links which were in the format of DokuWiki
+                       if path.endswith("/start"):
+                               # Strip /start from path
+                               path = path[:-6] or "/"
+
+                               # Redirect user to page if it exists
+                               page = self.backend.wiki.page_exists(path)
+                               if page:
+                                       self.redirect(path)
+
+                       raise tornado.web.HTTPError(404)
+
+               # Fetch the latest revision
+               latest_revision = page.get_latest_revision()
+
+               # Render page
+               self.render("wiki/page.html", page=page, latest_revision=latest_revision)