From 6f5863b746c249b86976b746575d4a68968b7d9f Mon Sep 17 00:00:00 2001 From: Kumi Date: Sat, 17 Aug 2024 11:42:09 +0200 Subject: [PATCH] fix(routing): handle redirects for nested wiki routes Updated the route to accept nested paths for wiki titles. Added logic to handle redirects within wiki articles, checking for redirection messages and appropriately redirecting if the "redirect" query parameter is not set to "no". This ensures smoother navigation between articles, even when dealing with paths that include slashes, which helps enhance user experience. Fixes #3. --- src/wikimore/app.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/wikimore/app.py b/src/wikimore/app.py index 960d4aa..c02a292 100644 --- a/src/wikimore/app.py +++ b/src/wikimore/app.py @@ -109,7 +109,7 @@ def search(): ) -@app.route("///wiki/") +@app.route("/<project>/<lang>/wiki/<path:title>") def wiki_article(project, lang, title): language_projects = app.languages.get(lang, {}).get("projects", {}) base_url = language_projects.get(project) @@ -127,6 +127,17 @@ def wiki_article(project, lang, title): soup = BeautifulSoup(article_html, "html.parser") + redirect_message = soup.find("div", class_="redirectMsg") + + if redirect_message and not (request.args.get("redirect") == "no"): + redirect_dest = redirect_message.find("a")["title"] + logger.debug(f"Redirecting to {redirect_dest}") + destination = url_for( + "wiki_article", project=project, lang=lang, title=redirect_dest + ) + logger.debug(f"Redirect URL: {destination}") + return redirect(destination) + for a in soup.find_all("a", href=True): href = a["href"]