From 013bc1ec71854ae59db9cd1c868d9bacdcc1044e Mon Sep 17 00:00:00 2001 From: Kumi Date: Mon, 19 Aug 2024 07:47:52 +0200 Subject: [PATCH] fix: improve URL parsing and linkage for wiki articles Refine wiki article URL parsing to handle paths with minimal segments and ensure proper language and project mapping. Updated href assignment to use `url_for` for more precise route generation. These changes improve the accuracy and reliability of linking external wiki pages within the project ecosystem. Fixes #13. --- src/wikimore/app.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/wikimore/app.py b/src/wikimore/app.py index b93d36c..5a4b571 100644 --- a/src/wikimore/app.py +++ b/src/wikimore/app.py @@ -155,21 +155,24 @@ def wiki_article(project, lang, title): a["href"] = f"/{project}/{lang}{href}" elif href.startswith("//") or href.startswith("https://"): + print(f"Checking {href}") parts = urlparse(href) - target_domain = parts.netloc + target_domain = f"https://{parts.netloc}" path_parts = parts.path.split("/") - if len(path_parts) > 4: - target_title = "/".join(path_parts[4:]) - target_lang = target_domain.split(".")[0] + if len(path_parts) >= 3: + target_title = "/".join(path_parts[2:]) found = False for language, language_projects in app.languages.items(): - for project_name, project_url in language_projects.items(): - if target_domain == project_url: - a["href"] = ( - f"/{project_name}/{target_lang}/wiki/{target_title}" + for project_name, project_url in language_projects["projects"].items(): + if project_url == target_domain: + a["href"] = url_for( + "wiki_article", + project=project_name, + lang=language, + title=target_title, ) found = True break