-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
111 lines (83 loc) · 3.31 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#coding=utf-8
import os
import markdown
import tornado.autoreload
import tornado.ioloop
import tornado.web
import tornado.httpserver
from tornado.options import define, options
MD_PATH = os.path.join(os.path.dirname(__file__), "md")
define("port", default=8888, help="run on the givn port", type=int)
class MyApplication(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", IndexHandler),
(r"/archive", ArchiveHandler),
(r"/friendlinks", FriendlinksHandler),
(r"/recommendablelinks", RecommendablelinksHandler),
(r"/about", AboutHandler),
(r"/entry/(.*)", DetailHandler),
(".*", BaseHandler),
]
settings = dict(
blog_title="DUST8",
template_path=os.path.join(os.path.dirname(__file__),
"templates"),
static_path=os.path.join(os.path.dirname(__file__),
"static"),
debug=False,
)
tornado.web.Application.__init__(self, handlers, **settings)
class BaseHandler(tornado.web.RequestHandler):
def write_error(self, status_code, **kwargs):
self.finish('<html><head></head><body><script type="text/javascript" \
src="http://www.qq.com/404/search_children.js"\
charset="utf-8"></script></body></html>')
class IndexHandler(BaseHandler):
def get(self):
entries = os.listdir(MD_PATH)
entries.sort(key=lambda x: int(x.replace("-", "")[:8]))
entries = entries[::-1][:5]
entries = [filename.replace(".md", "") for filename in entries]
entriesdict = [splitfilename(filename) for filename in entries]
self.render("index.html", entriesdict=entriesdict)
class DetailHandler(BaseHandler):
def get(self, filename):
file_path = MD_PATH + os.sep + filename + ".md"
time, title = splitfilename(filename)
if os.path.isfile(file_path):
with open(file_path, encoding="utf-8") as f:
entry = f.read()
entry = md(entry)
self.render("detail.html", title=title, entry=entry)
else:
raise tornado.web.HTTPError(404)
class ArchiveHandler(BaseHandler):
def get(self):
entries = os.listdir(MD_PATH)
entries.sort(key=lambda x: int(x.replace("-", "")[:8]))
entries = entries[::-1]
entries = [filename.replace(".md", "") for filename in entries]
entriesdict = [splitfilename(filename) for filename in entries]
self.render("archive.html", entriesdict=entriesdict)
class FriendlinksHandler(BaseHandler):
def get(self):
self.render("friendlinks.html")
class RecommendablelinksHandler(BaseHandler):
def get(self):
self.render("recommendablelinks.html")
class AboutHandler(BaseHandler):
def get(self):
self.render("about.html")
def md(content):
return markdown.markdown(content)
def splitfilename(filename):
filenamelist = filename.split("_")
return (filenamelist[0], filenamelist[1])
if __name__ == "__main__":
tornado.options.parse_command_line()
app = MyApplication()
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
instance = tornado.ioloop.IOLoop.instance()
instance.start()