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 --- TODO.md | 4 ++-- browser.py | 8 +++++--- gemini.py | 26 ++++++++++++++++++++++++++ input_template.html | 16 ++++++++++++++++ 4 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 input_template.html diff --git a/TODO.md b/TODO.md index 93e8d78..450236c 100644 --- a/TODO.md +++ b/TODO.md @@ -9,6 +9,6 @@ - [x] nice typography - [x] html templates - [ ] deal with non utf8 charsets - - [ ] handle all response codes + - [x] handle all response codes - [x] errors (4x, 5x) - - [ ] search + - [x] search diff --git a/browser.py b/browser.py index f6107e9..f5475ab 100755 --- a/browser.py +++ b/browser.py @@ -22,8 +22,9 @@ class GeminiPage(QtWebEngineCore.QWebEnginePage): class GeminiSchemeHandler(QtWebEngineCore.QWebEngineUrlSchemeHandler): def requestStarted(self, request): - print(request.requestUrl().toString()) - gem = gemini.get(request.requestUrl().toString()) + request_url = gemini.hack_url(request.requestUrl().toString()) + print(request_url) + gem = gemini.get(request_url) print(gem['status'], gem['meta']) buf = QtCore.QBuffer(parent=request) buf.open(QtCore.QIODevice.WriteOnly) @@ -39,7 +40,8 @@ class GUrlBar(QtWidgets.QLineEdit): QtWidgets.QLineEdit.__init__(self) def setUrl(self, url): - return self.setText(url.toDisplayString()) + url = gemini.hack_url(url.toDisplayString()) + return self.setText(url) class GBrowser(QtWidgets.QMainWindow): 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 = {} diff --git a/input_template.html b/input_template.html new file mode 100644 index 0000000..defbb7a --- /dev/null +++ b/input_template.html @@ -0,0 +1,16 @@ + + + + + + + + +
+ + + + + -- cgit v1.2.3