navigation: add function to go to parent URL

exec
dece 3 years ago
parent 5d1e13ea6a
commit cf6b67f78b

@ -12,7 +12,7 @@ from bebop.command_line import CommandLine
from bebop.history import History
from bebop.links import Links
from bebop.mouse import ButtonState
from bebop.navigation import join_url, parse_url, sanitize_url, set_parameter
from bebop.navigation import *
from bebop.page import Page, PagePad
from bebop.protocol import Request, Response
@ -111,6 +111,8 @@ class Browser:
self.quick_command("open")
elif char == ord("p"):
self.go_back()
elif char == ord("u"):
self.go_to_parent_page()
elif curses.ascii.isdigit(char):
self.handle_digit_input(char)
elif char == curses.KEY_MOUSE:
@ -463,6 +465,11 @@ class Browser:
if self.history.has_links():
self.open_gemini_url(self.history.pop(), history=False)
def go_to_parent_page(self):
"""Go to the parent URL if possible."""
if self.current_url:
self.open_gemini_url(get_parent_url(self.current_url))
def open_web_url(self, url):
"""Open a Web URL. Currently relies in Python's webbrowser module."""
self.set_status(f"Opening {url}")

@ -49,3 +49,12 @@ def set_parameter(url: str, user_input: str):
if "?" in url:
url = url.split("?", maxsplit=1)[0]
return url + "?" + quoted_input
def get_parent_url(url: str) -> str:
"""Return the parent URL (one level up)."""
scheme, netloc, path, params, query, frag = parse_url(url)
last_slash = path.rstrip("/").rfind("/")
if last_slash > -1:
path = path[:last_slash + 1]
return urllib.parse.urlunparse((scheme, netloc, path, params, query, frag))

Loading…
Cancel
Save