From 859f2af6533e3de839d544ada6c6b073e004ae68 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Sun, 5 Jul 2020 21:52:28 +0800 Subject: Search bar --- utab/__main__.py | 22 ++++++++++++++++++++++ utab/data/config.yml | 12 ++++++++++++ utab/data/index.html | 13 +++++++++++++ 3 files changed, 47 insertions(+) diff --git a/utab/__main__.py b/utab/__main__.py index 578c9d6..fef3655 100644 --- a/utab/__main__.py +++ b/utab/__main__.py @@ -178,6 +178,28 @@ def delete_site(url): return "No site with such URL exists", 403 +@app.route("/search") +def search(): + # [/engine_keyword ]query + query: str = request.args.get("q").strip() + words = query.split(" ") + if not words: + return abort(400) + if words[0].startswith("/"): + try: + engine = config["engines"][words[0][1:]] # get engine from keyword + query = " ".join(words[1:]) # strip engine from query + except KeyError: + engine = config["engines"][config["default_engine"]] + else: + try: + engine = config["engines"][config["default_engine"]] + except (KeyError, IndexError): + return "No engines defined", 403 + + return redirect(engine["url"].replace("{{query}}", query), 302) + + @app.route("/css/") def serve_css(filename): # serve static CSS because browsers forbid file:// scheme diff --git a/utab/data/config.yml b/utab/data/config.yml index 824eb6c..60a765f 100644 --- a/utab/data/config.yml +++ b/utab/data/config.yml @@ -1,2 +1,14 @@ columns: 8 rows: 4 +engines: + ddg: + url: https://duckduckgo.com/?q={{query}} + wk: + url: https://en.wikipedia.org/wiki/{{query}} + wikt: + url: https://en.wiktionary.org/wiki/{{query}} + pacman: + url: https://www.archlinux.org/packages/?q={{query}} + archwiki: + url: https://wiki.archlinux.org/index.php?search={{query}} +default_engine: ddg diff --git a/utab/data/index.html b/utab/data/index.html index e2f20b0..f762f52 100644 --- a/utab/data/index.html +++ b/utab/data/index.html @@ -7,10 +7,23 @@ utab +

Search

+

%site_heading%

%sites%
+