browser: handle local file:// URIs
This commit is contained in:
parent
85934eedcf
commit
84221a7816
|
@ -11,8 +11,6 @@ def main():
|
||||||
|
|
||||||
if args.url:
|
if args.url:
|
||||||
start_url = args.url
|
start_url = args.url
|
||||||
if not start_url.startswith("gemini://"):
|
|
||||||
start_url = "gemini://" + start_url
|
|
||||||
else:
|
else:
|
||||||
start_url = None
|
start_url = None
|
||||||
|
|
||||||
|
|
|
@ -65,12 +65,10 @@ class Browser:
|
||||||
)
|
)
|
||||||
self.command_line = CommandLine(command_line_window)
|
self.command_line = CommandLine(command_line_window)
|
||||||
|
|
||||||
pending_url = start_url
|
if start_url:
|
||||||
while self.running:
|
self.open_url(start_url, assume_absolute=True)
|
||||||
if pending_url:
|
|
||||||
self.open_url(pending_url)
|
|
||||||
pending_url = None
|
|
||||||
|
|
||||||
|
while self.running:
|
||||||
char = self.screen.getch()
|
char = self.screen.getch()
|
||||||
if char == ord(":"):
|
if char == ord(":"):
|
||||||
self.quick_command("")
|
self.quick_command("")
|
||||||
|
@ -191,6 +189,8 @@ class Browser:
|
||||||
self.open_gemini_url(sanitize_url(url), redirects)
|
self.open_gemini_url(sanitize_url(url), redirects)
|
||||||
elif parts.scheme.startswith("http"):
|
elif parts.scheme.startswith("http"):
|
||||||
self.open_web_url(url)
|
self.open_web_url(url)
|
||||||
|
elif parts.scheme == "file":
|
||||||
|
self.open_file(parts.path)
|
||||||
else:
|
else:
|
||||||
self.set_status_error(f"Protocol {parts.scheme} not supported.")
|
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."""
|
"""Open a Web URL. Currently relies in Python's webbrowser module."""
|
||||||
self.set_status(f"Opening {url}")
|
self.set_status(f"Opening {url}")
|
||||||
open_new_tab(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
|
though it technically is not, e.g. "dece.space" is not absolute as it
|
||||||
misses either the // delimiter.
|
misses either the // delimiter.
|
||||||
"""
|
"""
|
||||||
|
url = url.strip()
|
||||||
|
if url.startswith("file://"):
|
||||||
|
return urllib.parse.urlparse(url)
|
||||||
if url.startswith("gemini://"):
|
if url.startswith("gemini://"):
|
||||||
url = url[7:]
|
url = url[7:]
|
||||||
parts = urllib.parse.urlparse(url, scheme="gemini")
|
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."""
|
"""Return a new URL with the user input escaped (RFC 3986) appended."""
|
||||||
quoted_input = urllib.parse.quote(user_input)
|
quoted_input = urllib.parse.quote(user_input)
|
||||||
if "?" in url:
|
if "?" in url:
|
||||||
url = url.rsplit("?", maxsplit=1)[0]
|
url = url.split("?", maxsplit=1)[0]
|
||||||
return url + "?" + quoted_input
|
return url + "?" + quoted_input
|
||||||
|
|
|
@ -24,6 +24,10 @@ class TestNavigation(unittest.TestCase):
|
||||||
self.assertEqual(res.netloc, "dece.space")
|
self.assertEqual(res.netloc, "dece.space")
|
||||||
self.assertEqual(res.path, "/index.html")
|
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):
|
def test_join_url(self):
|
||||||
url = join_url("gemini://dece.space/", "some-file.gmi")
|
url = join_url("gemini://dece.space/", "some-file.gmi")
|
||||||
self.assertEqual(url, "gemini://dece.space/some-file.gmi")
|
self.assertEqual(url, "gemini://dece.space/some-file.gmi")
|
||||||
|
|
Reference in a new issue