Prettify URLs before displaying them to the user

This commit is contained in:
Kovid Goyal
2022-12-29 17:04:02 +05:30
parent f4ac03b791
commit 306f9e5735
3 changed files with 21 additions and 6 deletions
+3 -5
View File
@@ -8,7 +8,7 @@ import os
import re
import sys
from contextlib import suppress
from functools import lru_cache, partial
from functools import partial
from gettext import gettext as _
from time import monotonic, sleep
from typing import (
@@ -64,7 +64,7 @@ from .utils import (
cleanup_ssh_control_masters, func_name, get_editor, get_new_os_window_size,
is_path_in_temp_dir, less_version, log_error, macos_version, open_url,
parse_address_spec, parse_uri_list, platform_window_id, remove_socket_file,
safe_print, single_instance, startup_notification_handler, which,
safe_print, single_instance, startup_notification_handler, which, sanitize_url_for_dispay_to_user
)
from .window import CommandOutput, CwdRequest, Window
@@ -2603,7 +2603,5 @@ class Boss:
pass
mouse_discard_event = discard_event
@lru_cache(maxsize=64)
def sanitize_url_for_dispay_to_user(self, url: str) -> str:
# TODO: Use punycode, remove percent encoding, etc.
return url
return sanitize_url_for_dispay_to_user(url)
+15
View File
@@ -1088,3 +1088,18 @@ def sanitize_for_bracketed_paste(text: bytes) -> bytes:
break
text = new_text
return text
@lru_cache(maxsize=64)
def sanitize_url_for_dispay_to_user(url: str) -> str:
from urllib.parse import urlparse, urlunparse, unquote
try:
purl = urlparse(url)
if purl.netloc:
purl = purl._replace(netloc=purl.netloc.encode('idna').decode('ascii'))
if purl.path:
purl = purl._replace(path=unquote(purl.path))
url = urlunparse(purl)
except Exception:
url = 'Unpareseable URL: ' + url
return url