commit fd8ee5bfb75f3408f9a3d11cc334e285898c92a0
parent 2fd8fe919bfc0269f4dcf50f2e13fb12b47d5ef4
Author: solderpunk <solderpunk@sdf.org>
Date: Tue, 2 Jun 2020 14:45:48 -0400
Merge pull request 'Add gemini:// support directly to urllib.parse' (#16) from dancek/AV-98:simplify-urljoin into master
Thanks a lot! I've never bothered to poke inside `urljoin` before so didn't realise this was so easy to do. I agree that this is far more readable and maintainable.
Diffstat:
1 file changed, 7 insertions(+), 11 deletions(-)
diff --git 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: