From 9c82b63ff123cda54e991d0a9e847b31ad905b4e Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Mon, 1 Jun 2020 10:21:55 +0300 Subject: [PATCH] Add gemini:// support directly to urllib.parse --- av98.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/av98.py b/av98.py index 9ef7c5c..2aef9a5 100755 --- a/av98.py +++ b/av98.py @@ -86,6 +86,12 @@ _MIME_HANDLERS = { "text/gemini": "cat %s", } +# monkey-patch Gemini support in urllib.parse +# see https://github.com/python/cpython/blob/master/Lib/urllib/parse.py +urllib.parse.uses_relative.append("gemini") +urllib.parse.uses_netloc.append("gemini") + + def fix_ipv6_url(url): if not url.count(":") > 2: # Best way to detect them? return url @@ -159,17 +165,7 @@ class GeminiItem(): Convert a relative URL to an absolute URL by using the URL of this GeminiItem as a base. """ - # Absolutise URL, which annoyingly needs a valid scheme... - if self.url.startswith("gemini://"): - base_url = self.url.replace("gemini://", "https://") - was_gemini = True - else: - base_url = self.url - was_gemini = False - absolute = urllib.parse.urljoin(base_url, relative_url) - if absolute != relative_url and was_gemini: - absolute = absolute.replace("https://", "gemini://") - return absolute + return urllib.parse.urljoin(self.url, relative_url) def to_map_line(self, name=None): if name or self.name: