3
# Copyright 2009 Bret Taylor
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
23
import wsgiref.handlers
26
class ContentHandler(tornado.web.RequestHandler):
28
paths = ("documentation", "index")
29
if not path: path = "index"
31
raise tornado.web.HTTPError(404)
32
self.render(path + ".html", markdown=self.markdown)
34
def markdown(self, path, toc=False):
35
if not hasattr(ContentHandler, "_md") or self.settings.get("debug"):
36
ContentHandler._md = {}
37
if path not in ContentHandler._md:
38
full_path = os.path.join(self.settings["template_path"], path)
39
f = open(full_path, "r")
40
contents = f.read().decode("utf-8")
42
if toc: contents = u"[TOC]\n\n" + contents
43
md = markdown.Markdown(extensions=["toc"] if toc else [])
44
ContentHandler._md[path] = md.convert(contents).encode("utf-8")
45
return ContentHandler._md[path]
49
"template_path": os.path.join(os.path.dirname(__file__), "templates"),
51
"debug": os.environ.get("SERVER_SOFTWARE", "").startswith("Development/"),
53
application = tornado.wsgi.WSGIApplication([
54
(r"/([a-z]*)", ContentHandler),
59
wsgiref.handlers.CGIHandler().run(application)
62
if __name__ == "__main__":