Pi66

merodi
A markdown-based static site generator.
git clone https://git.pi66.xyz/merodi

← back to log

fix: improve web path resolution for html files

author: pi66
date: 2026-06-26 23:28
hash: 5654b4e1c7e149ecc098f3bce3703d55cb5fdfec

Diffstat:

M

src/webviewer.py
32 ++++++++++++++++++++++--------
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
diff --git a/src/webviewer.py b/src/webviewer.py
index cc17bab..190ec2f 100644
--- a/src/webviewer.py
+++ b/src/webviewer.py
@@ -15,17 +15,31 @@ current_url = ""

 def http_server(host, port, routes):
     class Handler(SimpleHTTPRequestHandler):
-        def translate_path(self, url_path):
+        def translate_path(self, url_path: str) -> str:
             url = routes["url_path"]
-            fs  = routes["fs_path"]
-            relpath = url_path.lstrip("/").rstrip("/")
+            fs = routes["fs_path"]
+
             if url_path.startswith(url["static"]):
-                extracted_path = path.relpath(url_path,url["static"])
-                return path.join(fs["static"], extracted_path)
-            if url_path.startswith(url["html"]):
-                extracted_path = path.relpath(url_path,url["html"])
-                return path.join(fs["html"], extracted_path)
-            return path.join(fs["html"], relpath)
+                rel = url_path.removeprefix(url["static"]).lstrip("/")
+                resolved = path.join(fs["static"], rel)
+
+            elif url_path.startswith(url["html"]):
+                rel = url_path.removeprefix(url["html"]).lstrip("/")
+                resolved = path.join(fs["html"], rel)
+
+            else:
+                rel = url_path.lstrip("/")
+                resolved = path.join(fs["html"], rel)
+
+            if not path.exists(resolved) and path.exists(resolved + ".html"):
+                resolved += ".html"
+
+            if path.isdir(resolved):
+                index = path.join(resolved, "index.html")
+                if path.exists(index):
+                    resolved = index
+
+            return resolved

         def send_error(self, code, message=None, explain=None):
             # html = html_fatal(code, self.path, message, explain)