-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
59 lines (51 loc) · 1.73 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
import web
import time
import os
from pygments import highlight
from pygments.formatters.html import HtmlFormatter
from pygments.lexers import get_lexer_by_name, get_lexer_for_filename
from web.template import ALLOWED_AST_NODES
ALLOWED_AST_NODES.append('Constant')
render = web.template.render('templates/')
urls = (
'/', 'index',
'/p/(.*)', 'html'
)
def trans(content, type, style, suffix):
print(type, style, suffix)
if not suffix:
lexer = get_lexer_by_name(type)
else:
try:
lexer = get_lexer_for_filename(suffix)
except:
lexer = get_lexer_by_name(type)
# 指定风格
formatter = HtmlFormatter(style=style)
# 获取html
html = highlight(content, lexer, formatter)
return html
class html:
def GET(self, file_id):
try:
with open("./data/" + str(file_id), 'r') as f:
content = f.read()
param = web.input(type="text",style="default", suffix=None)
html = trans(content, param.type, param.style, param.suffix)
return render.paste(html, "../static/css/"+param.style+".css", time.strftime("%Y-%m-%d %X", time.localtime()))
except:
return render.error()
class index:
def GET(self):
return open("index.html", encoding='UTF-8').read()
def POST(self):
form = web.input()
file_id = (int)(time.time())
if not os.path.exists("./data/"):
os.makedirs("./data/")
with open("./data/"+str(file_id), 'w', newline='') as f:
f.write(form.content)
raise web.seeother('/p/' + str(file_id) + '?type=' + form.type + '&suffix=' + form.suffix)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()