mirror of
https://github.com/jimeh/pylight.git
synced 2026-02-19 09:56:43 +00:00
tweaked output a bit
This commit is contained in:
17
app.py
17
app.py
@@ -1,7 +1,8 @@
|
||||
import os
|
||||
|
||||
# import flask
|
||||
from flask import Flask, make_response, request
|
||||
from flask import *
|
||||
|
||||
|
||||
# import pygments
|
||||
from pygments import highlight
|
||||
@@ -20,27 +21,27 @@ def hello_world():
|
||||
|
||||
@app.route('/api')
|
||||
def api_root():
|
||||
resp = make_response('{"version": "0.0.1"}', 200)
|
||||
resp.headers['Content-Type'] = 'application/json'
|
||||
return resp
|
||||
return jsonify(version='0.0.1')
|
||||
|
||||
@app.route('/api/highlight', methods = ['POST'])
|
||||
@app.route('/api/highlight', methods=['POST'])
|
||||
def api_highlight():
|
||||
if 'source' not in request.json:
|
||||
return make_response(None, 422)
|
||||
|
||||
if 'lang' in request.json:
|
||||
lexer = get_lexer_by_name(request.json['lang'])
|
||||
elif 'mimetype' in request.json:
|
||||
lexer = get_lexer_for_mimetype(request.json['mimetype'])
|
||||
elif 'filename' in request.json:
|
||||
lexer = guess_lexer_for_filename(request.json['filename'],
|
||||
request.json['source'])
|
||||
elif 'mimetype' in request.json:
|
||||
lexer = get_lexer_for_mimetype(request.json['mimetype'])
|
||||
else:
|
||||
lexer = guess_lexer(request.json['source'])
|
||||
|
||||
formatter = get_formatter_by_name('html')
|
||||
return highlight(request.json['source'], lexer, formatter)
|
||||
body = highlight(request.json['source'], lexer, formatter)
|
||||
|
||||
return jsonify({ "lexer": lexer.name, "body": body })
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user