def png_snippet(id): """ Show image (png) with highlighted code of snippet. """ api_url = api_url_for("api.get_snippet_by_id", id=id) response, body = g.h.request(api_url, "GET") if response.status == 200: snippet = json.loads(body) # highlight snippet as plain text if ``language`` isn't specified language = snippet.get("language", "text") if language is None: language = "text" lexer = get_lexer_by_name(language, stripall=True) formatter = ImageFormatter(font_name="Ubuntu Mono") png = highlight(snippet["content"], lexer, formatter) return png, 200, {"Content-Type": "image/png"} elif response.status == 404: abort(404) else: abort(500)
def raw_snippet(id): api_url = api_url_for("api.get_snippet_by_id", id=id) response, body = g.h.request(api_url, "GET") if response.status == 200: snippet = json.loads(body) headers = {"Content-Type": "text/plain; charset=utf-8"} return snippet["content"], 200, headers elif response.status == 404: abort(404) else: abort(500)
def download_snippet(id): api_url = api_url_for("api.get_snippet_by_id", id=id) response, body = g.h.request(api_url, "GET") if response.status == 200: snippet = json.loads(body) attachment = "attachment; filename='{0}{1}'".format( snippet["id"], ext_by_short_name(snippet.get("language", "text")) ) headers = {"Content-Type": "text/plain; charset=utf-8", "Content-Disposition": attachment} return snippet["content"], 200, headers elif response.status == 404: abort(404) else: abort(500)
def recent_snippets(): filters = {} if "author" in request.args: filters["author"] = r"^{0}$".format(request.args["author"]) if "language" in request.args: filters["language"] = request.args["language"] if "tag" in request.args: filters["tag"] = request.args["tag"] api_url = api_url_for("api.get_snippets", **filters) response, body = g.h.request(api_url, "GET") if response.status == 200: snippets = json.loads(body) return render_template("recent_snippets.html", snippets=snippets) elif response.status == 404: abort(404) else: abort(500)
def show_snippet(id): api_url = api_url_for("api.get_snippet_by_id", id=id) response, body = g.h.request(api_url, "GET") if response.status == 200: snippet = json.loads(body) # highlight snippet as plain text if ``language`` isn't specified language = snippet.get("language", "text") if language is None: language = "text" lexer = get_lexer_by_name(language, stripall=True) formatter = HtmlFormatter(linenos="table") snippet["content"] = highlight(snippet["content"], lexer, formatter) return render_template("show_snippet.html", snippet=snippet) elif response.status == 404: abort(404) else: abort(500)
def new_snippet(): """ Create a new snippet entry :query title: the snippet title :query author: the snippet author's name :query content: the snippet body (**MUST NOT** be empty) :query language: the language name the snippet is written in (for syntax highlighting) :query tags: the list of text tags (separated by commas) which are associated with the snippet :statuscode 201: the snippet has been created successfully. The ``id`` of the snippet is returned in the response body :statuscode 400: bad request (some request parameters might be missing or incorrect) :statuscode 500: internal server error """ snippet = { "language": request.form.get("language"), "content": request.form.get("content"), "author": request.form.get("author"), "title": request.form.get("title"), "created": datetime.datetime.now(), "tags": [], } # parse string with tags tags = request.form.get("tags") if tags: snippet["tags"] = [tag.lstrip() for tag in tags.split(',')] # save snippet if content isn't empty if snippet["content"]: id = current_app.db.snippets.insert(snippet) response = make_response(jsonify(id=id), 201) response.headers["Location"] = api_url_for("api.get_snippet_by_id", id=id) return response else: return jsonify(error="The snippet content is empty"), 400
def new_snippet(): api_url = api_url_for("api.new_snippet") # check for spam-bots filling all form fields if request.form.get("email", ""): abort(400) data = { "title": request.form.get("title"), "tags": request.form.get("tags"), } if not request.files: data["language"] = request.form.get("language", "text") data["content"] = request.form.get("content") else: filename, ext = os.path.splitext(request.files["file"].filename) data["language"] = short_name_by_ext(ext) data["content"] = request.files["file"].read() if not data["title"]: data["title"] = filename response, body = g.h.request( api_url, "POST", urlencode(data), headers={"Content-Type": "application/x-www-form-urlencoded"} ) if response.status == 201: content = json.loads(body) return redirect(abs_url_for(".show_snippet", id=content["id"])) elif response.status == 400: abort(400) # 400 BAD REQUEST else: abort(500)