From 1958a22bd8564d7a6fe46711276c7204ccd3b1e3 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 27 Jun 2023 08:17:24 +0000 Subject: [PATCH] docs: Copy page handler from wiki Signed-off-by: Michael Tremer --- Makefile.am | 1 + src/web/__init__.py | 4 +++ src/web/docs.py | 88 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 src/web/docs.py diff --git a/Makefile.am b/Makefile.am index a95a0344..44271451 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/web/__init__.py b/src/web/__init__.py index e3dc4a2b..633d923e 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -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 index 00000000..0e87bb6c --- /dev/null +++ b/src/web/docs.py @@ -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) -- 2.39.2