navigation: add function to go to parent URL

This commit is contained in:
dece 2021-03-14 02:05:42 +01:00
parent 5d1e13ea6a
commit cf6b67f78b
2 changed files with 17 additions and 1 deletions

View file

@ -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}")

View file

@ -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))