diff options
author | Frederick Yin <fkfd@macaw.me> | 2020-07-05 21:52:28 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@macaw.me> | 2020-07-05 21:52:28 +0800 |
commit | 859f2af6533e3de839d544ada6c6b073e004ae68 (patch) | |
tree | 68a7e19ccad7e110b97144ea4c5a26218de5c052 /utab | |
parent | b3ac51793f413f0697f61caa31c54559b1db8e4e (diff) |
Search bar
Diffstat (limited to 'utab')
-rw-r--r-- | utab/__main__.py | 22 | ||||
-rw-r--r-- | utab/data/config.yml | 12 | ||||
-rw-r--r-- | utab/data/index.html | 13 |
3 files changed, 47 insertions, 0 deletions
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/<string:filename>") 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 @@ <title>utab</title> </head> <body> + <h2>Search</h2> + <div id="search"> + <form action="/search" method="GET"> + <input + class="form-input" + name="q" + type="text" + placeholder="[/<engine>] <query>" + /> + <input class="form-button" type="submit" value="Search" /> + </form> + </div> <h2>%site_heading%</h2> <div id="sites"> %sites% </div> + <footer class="ctrl"> <a href="/"> <img class="ctrl-icon" src="/icons/home.svg" /> |