forked from alexkay/hilite.me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·94 lines (77 loc) · 3.42 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2009-2011 Alexander Kojevnikov <[email protected]>
#
# hilite.me is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# hilite.me is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with hilite.me. If not, see <http://www.gnu.org/licenses/>.
import datetime
from urllib import quote, unquote
from flask import Flask, make_response, render_template, request
from pygments.lexers import get_all_lexers
from pygments.styles import get_all_styles
from tools import *
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def index():
code = request.form.get('code', "print 'hello world!'")
lexer = (
request.form.get('lexer', '') or
unquote(request.cookies.get('lexer', 'python')))
lexers = [(l[1][0], l[0]) for l in get_all_lexers()]
lexers = sorted(lexers, lambda a, b: cmp(a[1].lower(), b[1].lower()))
style = (
request.form.get('style', '') or
unquote(request.cookies.get('style', 'colorful')))
styles = sorted(get_all_styles(), key=str.lower)
linenos = (
request.form.get('linenos', '') or
request.method == 'GET' and
unquote(request.cookies.get('linenos', ''))) or ''
divstyles = request.form.get(
'divstyles', unquote(request.cookies.get('divstyles', '')))
divstyles = divstyles or get_default_style()
html = hilite_me(code, lexer, {}, style, linenos, divstyles)
response = make_response(render_template('index.html', **locals()))
next_year = datetime.datetime.now() + datetime.timedelta(days=365)
response.set_cookie('lexer', quote(lexer), expires=next_year)
response.set_cookie('style', quote(style), expires=next_year)
response.set_cookie('linenos', quote(linenos), expires=next_year)
response.set_cookie('divstyles', quote(divstyles), expires=next_year)
return response
@app.route("/api", methods=['GET', 'POST'])
def api():
code = request.values.get('code', '')
if not code:
response = make_response(render_template('api.txt'))
response.headers["Content-Type"] = "text/plain"
return response
lexer = request.values.get('lexer', '')
options = request.values.get('options', '')
def convert(item):
key, value = item
if value == 'False':
return key, False
elif value == 'True':
return key, True
else:
return key, value
options = dict(convert(option.split('=')) for option in options.split(',') if option)
style = request.values.get('style', '')
linenos = request.values.get('linenos', '')
divstyles = request.form.get('divstyles', get_default_style())
html = hilite_me(code, lexer, options, style, linenos, divstyles)
response = make_response(html)
response.headers["Content-Type"] = "text/plain"
return response
if __name__ == "__main__":
app.run(host='0.0.0.0')