~fkfd/utab

859f2af6533e3de839d544ada6c6b073e004ae68 — Frederick Yin 2 years ago b3ac517
Search bar
3 files changed, 47 insertions(+), 0 deletions(-)

M utab/__main__.py
M utab/data/config.yml
M utab/data/index.html
M utab/__main__.py => utab/__main__.py +22 -0
@@ 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

M utab/data/config.yml => utab/data/config.yml +12 -0
@@ 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

M utab/data/index.html => utab/data/index.html +13 -0
@@ 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="[/&lt;engine&gt;] &lt;query&gt;"
      />
      <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" />