browser: handle local file:// URIs

exec
dece 3 years ago
parent 85934eedcf
commit 84221a7816

@ -11,8 +11,6 @@ def main():
if args.url:
start_url = args.url
if not start_url.startswith("gemini://"):
start_url = "gemini://" + start_url
else:
start_url = None

@ -65,12 +65,10 @@ class Browser:
)
self.command_line = CommandLine(command_line_window)
pending_url = start_url
while self.running:
if pending_url:
self.open_url(pending_url)
pending_url = None
if start_url:
self.open_url(start_url, assume_absolute=True)
while self.running:
char = self.screen.getch()
if char == ord(":"):
self.quick_command("")
@ -191,6 +189,8 @@ class Browser:
self.open_gemini_url(sanitize_url(url), redirects)
elif parts.scheme.startswith("http"):
self.open_web_url(url)
elif parts.scheme == "file":
self.open_file(parts.path)
else:
self.set_status_error(f"Protocol {parts.scheme} not supported.")
@ -461,3 +461,15 @@ class Browser:
"""Open a Web URL. Currently relies in Python's webbrowser module."""
self.set_status(f"Opening {url}")
open_new_tab(url)
def open_file(self, filepath):
"""Open a file and render it.
This should be used only on Gemtext files or at least text files.
Anything else will produce garbage and may crash the program.
"""
try:
with open(filepath, "rb") as f:
self.load_page(f.read())
except (OSError, ValueError) as exc:
self.set_status_error(f"Failed to open file: {exc}")

@ -17,6 +17,9 @@ def parse_url(url: str, absolute: bool =False):
though it technically is not, e.g. "dece.space" is not absolute as it
misses either the // delimiter.
"""
url = url.strip()
if url.startswith("file://"):
return urllib.parse.urlparse(url)
if url.startswith("gemini://"):
url = url[7:]
parts = urllib.parse.urlparse(url, scheme="gemini")
@ -42,5 +45,5 @@ def set_parameter(url: str, user_input: str):
"""Return a new URL with the user input escaped (RFC 3986) appended."""
quoted_input = urllib.parse.quote(user_input)
if "?" in url:
url = url.rsplit("?", maxsplit=1)[0]
url = url.split("?", maxsplit=1)[0]
return url + "?" + quoted_input

@ -24,6 +24,10 @@ class TestNavigation(unittest.TestCase):
self.assertEqual(res.netloc, "dece.space")
self.assertEqual(res.path, "/index.html")
res = parse_url("file:///home/dece/gemini/index.gmi")
self.assertEqual(res.scheme, "file")
self.assertEqual(res.path, "/home/dece/gemini/index.gmi")
def test_join_url(self):
url = join_url("gemini://dece.space/", "some-file.gmi")
self.assertEqual(url, "gemini://dece.space/some-file.gmi")

Loading…
Cancel
Save