/
cling.py
executable file
·165 lines (129 loc) · 5.21 KB
/
cling.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
# encoding: utf-8
"""
cling.py
main site file
"""
import os.path
from tornado import httpserver, ioloop, web
from tornado.options import define, options
from utils import parse_page, directory_listing, slug_to_name, get_template_page
import config
class Application(web.Application):
"""main appliction launcher"""
def __init__(self):
settings = {
'debug': True,
'autoescape': None,
'static_path': os.path.join(os.path.dirname(__file__), 'static'),
'ui_modules': {'TOC': TocModule,}
}
route = (r'/([\w_\-\/]+)?', PageHandler)
if options.allow_data:
route = (r'/([\w_\-\/\.\+?]+)?(?:\.preview)?(?:\.data)?', PageHandler)
routes = [
route,
]
web.Application.__init__(self, routes, **settings)
class BaseHandler(web.RequestHandler):
"""All web request handlers extends this class"""
def _template_string(self, string, **kwargs):
template = web.template.Template(string, autoescape=None)
args = dict(
handler=self,
request=self.request,
current_user=self.current_user,
locale=self.locale,
_=self.locale.translate,
static_url=self.static_url,
xsrf_form_html=self.xsrf_form_html,
reverse_url=self.reverse_url,
options=options,
)
args.update(**kwargs)
args.update(self.ui)
return template.generate(**args)
def parse_page(self, page=None, is_preview=False):
"""method used to render a page from the page directory
args:
stirng page
"""
title, lead_image, slug, date, template, content, meta = parse_page(page, is_preview=is_preview)
content = self._template_string(content)
content_template = get_template_page(template)
content_rendered = self.render_string(content_template, content=content, page=os.path.join(options.page_dir, page))
page_template = get_template_page('page/page')
page_content = self.render_string(page_template, title=title,
slug=slug, date=date, content=content_rendered, page=page, lead_image=lead_image, meta=meta)
return title, slug, date, template, page_content
def is_ajax(self):
"""simply determines if the request was an ajax
request or not
"""
return 'X-Requested-With' in self.request.headers and \
self.request.headers['X-Requested-With'] == 'XMLHttpRequest'
def is_data(self):
"""method that checks to see if the request was for data"""
return self.request.uri.endswith('.data')
def is_preview(self):
"""method that will allow loading of non-markdown files (files that do not appear in toc)"""
return '.preview' in self.request.uri
class PageHandler(BaseHandler):
"""main controller class"""
def get(self, path=None):
"""main page loader
if the path doesnt exists, it is set index
if path isnt toc, toc is loaded
if path isnt index, index is loaded
"""
page_content = ''
ajax = self.is_ajax()
data = False
preview = False
theme = self.get_argument('theme', None)
if theme is not None:
options.define('theme', default=theme, mutable=True)
if path is None:
path = 'index'
if options.allow_data:
data = self.is_data()
if options.allow_preview:
preview = self.is_preview()
path = slug_to_name(path)
if ajax is False or data is False:
if path is None or str(path).lower() != 'toc':
title, slug, date, template, content= self.parse_page('toc')
if path is None or str(path).lower() != 'index':
title, slug, date, template, content = self.parse_page('index')
title, slug, date, template, content = self.parse_page(path, is_preview=preview)
page_content += content
if ajax is True or data is True:
self.write({
'title': title,
'slug': slug,
'date': date,
'path': '/' + path,
'content': page_content
})
else:
page_template = get_template_page('base')
self.render(page_template, title=title, page_content=page_content)
class TocModule(web.UIModule):
"""this class is used to create the table of contents module
that can be used in all of the templates
"""
def render(self, directory=options.page_dir):
"""
args:
string directory -- the directory
"""
if 'page/toc' in directory:
directory = options.page_dir
toc = directory_listing(directory)
toc_page = get_template_page('asset/toc')
return self.render_string(toc_page, data=toc)
if __name__ == '__main__':
"""Start the application"""
http_server = httpserver.HTTPServer(Application())
http_server.listen(9002)
ioloop.IOLoop.instance().start()