summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--browset.py17
-rw-r--r--protocol/gemini.py13
2 files changed, 28 insertions, 2 deletions
diff --git a/browset.py b/browset.py
index 817ad91..c316c1d 100644
--- a/browset.py
+++ b/browset.py
@@ -7,6 +7,7 @@ from gemtext import Gemtext
 from protocol.gemini import GeminiProtocol
 
 class Browset(App):
+    url = ""
     CSS_PATH = "browset.css"
     BINDINGS = [
         Binding("ctrl+c,ctrl+q", "app.quit", "Quit", show=True),
@@ -21,12 +22,24 @@ class Browset(App):
             Button("🔜"), # ⏩
             Button("🔄"), # 🔁
             
-            Input(placeholder="Enter URI"),
+            Input(placeholder="Enter URI", id="url"),
             id="toolbar"
         )
         yield Gemtext(fp=self.content, id="content")
     async def on_input_submitted(self, message: Input.Submitted) -> None:
-        (mime, fp) = GeminiProtocol.get(message.value)
+        self._do_url(message.value)
+
+    async def on_button_pressed(self, event: Button.Pressed) -> None:
+        url = event.button.name
+        if not ":" in url:
+            url = GeminiProtocol.relativeURL(url, self.url)
+        input = self.query_one("#url")
+        input.value = url
+        self._do_url(url)
+        
+    def _do_url(self, url):
+        self.url = url
+        (mime, fp) = GeminiProtocol.get(url)
         self.query_one("#content").remove()
         self.mount(Gemtext(fp=fp, id="content"))
 
diff --git a/protocol/gemini.py b/protocol/gemini.py
index c55e523..ec51b7c 100644
--- a/protocol/gemini.py
+++ b/protocol/gemini.py
@@ -2,6 +2,19 @@ import socket
 import ssl
 
 class GeminiProtocol():
+    def relativeURL(path, prevurl):
+        if path.startswith("/"):
+            url = '/'.join(prevurl.split("/")[0:3])+path
+        else:
+            base = prevurl
+            if base.count('/') >= 3:
+                base = '/'.join(prevurl.split('/')[0:-1])
+            while path.startswith("../"):
+                base = '/'.join(base.split('/')[0:-1])
+                path = path.replace("../","",1)
+            url = base + '/' + path
+        return url
+
     def get(url):
         hostname = _gethostname(url)
         try: