diff options
author | Matt Singleton <matt@xcolour.net> | 2022-01-16 21:45:30 -0600 |
---|---|---|
committer | Matt Singleton <matt@xcolour.net> | 2022-01-16 21:53:09 -0600 |
commit | 704be7d2e7db33635fc9de684c8b3916cd798c68 (patch) | |
tree | 188ad6c77eec042809f963fcc52100c649a5967a /gemini.py | |
parent | 1ef62c2c3044b5b11dae92a08be5d83a780392b4 (diff) |
support input forms
Diffstat (limited to 'gemini.py')
-rw-r--r-- | gemini.py | 26 |
1 files changed, 26 insertions, 0 deletions
@@ -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: + ?<query> + Rather than the default way an html get form renders them: + ?<inputname>=<query> + 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 = {} |