From 704be7d2e7db33635fc9de684c8b3916cd798c68 Mon Sep 17 00:00:00 2001 From: Matt Singleton Date: Sun, 16 Jan 2022 21:45:30 -0600 Subject: support input forms --- gemini.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'gemini.py') diff --git a/gemini.py b/gemini.py index 275eab9..7bedda5 100644 --- a/gemini.py +++ b/gemini.py @@ -24,6 +24,9 @@ def gem2html(gem: dict) -> str: parser = fsm.Parser(gem['body'].split('\n'), body) parser.parse() params['body'] = body.getvalue() + elif gem['status'][0] == '1': + template = string.Template(open('input_template.html').read()) + params['meta'] = gem['meta'] else: template = string.Template(open('error_template.html').read()) if gem['status'] == '00': @@ -63,6 +66,29 @@ def get(url: str, follow_redirects: bool = True) -> dict: return response +def hack_url(url: str) -> str: + """ + An ugly hack to reformat input queries the way gemini wants them: + ? + Rather than the default way an html get form renders them: + ?= + I don't think this ever *should* break but I guess it *could*. + """ + url_parts = urllib.parse.urlsplit(url) + query = urllib.parse.parse_qs(url_parts.query) + if len(query) == 1 and '__client_internal_input' in query and len(query['__client_internal_input']) == 1: + query = str(query['__client_internal_input'][0]) + url = urllib.parse.urlunsplit(( + url_parts.scheme, + url_parts.netloc, + url_parts.path, + query, + url_parts.fragment, + )) + url_parts = urllib.parse.urlsplit(url) + return url + + def _parse_meta(meta: str) -> dict: mime, _, params_text = meta.lower().strip().partition(';') params = {} -- cgit v1.2.3