forked from earwig/bitshift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
68 lines (52 loc) · 1.74 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Module to contain all the project's Flask server plumbing.
"""
from json import dumps
from flask import Flask, make_response, render_template, request
from bitshift import assets
from bitshift.database import Database
from bitshift.languages import LANGS
from bitshift.query import parse_query, QueryParseException
app = Flask(__name__, static_folder="static", static_url_path="")
app.config.from_object("bitshift.config")
app_env = app.jinja_env
app_env.line_statement_prefix = "="
app_env.globals.update(assets=assets)
database = Database()
@app.route("/")
def index():
return render_template("index.html", autocomplete_languages=LANGS)
@app.route("/search.json")
def search():
def reply(json):
resp = make_response(dumps(json))
resp.mimetype = "application/json"
return resp
query = request.args.get("q")
if not query:
return reply({"error": "No query given"})
try:
tree = parse_query(query)
except QueryParseException as exc:
return reply({"error": exc.args[0]})
page = request.args.get("p", 1)
try:
page = int(page)
except ValueError:
return reply({"error": u"Invalid page number: %s" % page})
highlight = request.args.get("hl", "0")
highlight = highlight.lower() not in ["0", "false", "no"]
count, codelets = database.search(tree, page)
results = [clt.serialize(highlight) for clt in codelets]
return reply({"count": count, "results": results})
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/docs")
def docs():
return render_template("docs.html")
@app.errorhandler(404)
def error404(error):
return render_template("error404.html"), 404
if __name__ == "__main__":
app.run(debug=True)