diff --git a/bebop/browser.py b/bebop/browser.py index 0fedac1..b2d8856 100644 --- a/bebop/browser.py +++ b/bebop/browser.py @@ -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}") diff --git a/bebop/navigation.py b/bebop/navigation.py index 1d2e712..799b7c9 100644 --- a/bebop/navigation.py +++ b/bebop/navigation.py @@ -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))