-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathzappa_cms.py
executable file
·116 lines (86 loc) · 2.98 KB
/
zappa_cms.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
#!/usr/bin/env python
from flask import Flask, request, render_template
from datetime import datetime, timedelta
from jinja2 import Environment, FileSystemLoader
import markdown2
import os
app = Flask(__name__)
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
jenv = Environment(loader=FileSystemLoader(THIS_DIR), trim_blocks=True)
@app.route('/')
def index():
"""
The landing page.
"""
try:
path = 'index.md'
html = markdown2.markdown_path(path, extras=["metadata"])
metadata = html.metadata
if 'template' in metadata.keys():
template = metadata['template']
else:
template = 'templates/index.html'
render_data = metadata.copy()
render_data[u'body'] = html
rendered = jenv.get_template(template).render(render_data)
return rendered
except IOError as e:
return render_template('404.html'), 404
@app.route('/<category>', methods=['GET', 'POST'])
def category(category):
"""
Categories of items.
Essentially a 'directory listing' page.
Returns to the items to the 'index'.
"""
try:
# First do the items
items = []
for filename in os.listdir(category):
if 'index.md' in filename:
continue
item = markdown2.markdown_path(category + '/' + filename, extras=["metadata"])
item.metadata['slug'] = filename.split('/')[-1].replace('.md', '')
items.append(item)
# Then do the index
path = category + '/index.md'
html = markdown2.markdown_path(path, extras=["metadata"])
metadata = html.metadata
if 'template' in metadata.keys():
template = metadata['template']
else:
template = 'templates/category.html'
render_data = metadata.copy()
render_data[u'body'] = html
render_data[u'items'] = items
render_data[u'category'] = category
rendered = jenv.get_template(template).render(render_data)
return rendered
except IOError as e:
print(e)
return render_template('404.html'), 404
@app.route('/<category>/<item_slug>', methods=['GET', 'POST'])
def item(category, item_slug):
"""
A single specific item.
"""
try:
path = category + '/' + item_slug + '.md'
html = markdown2.markdown_path(path, extras=["metadata"])
metadata = html.metadata
if 'template' in metadata.keys():
template = metadata['template']
else:
template = 'templates/item.html'
render_data = metadata.copy()
render_data[u'body'] = html
render_data[u'category'] = category
render_data[u'item_slug'] = item_slug
rendered = jenv.get_template(template).render(render_data)
return rendered
except IOError as e:
return render_template('404.html'), 404
# We only need this for local development.
if __name__ == '__main__':
app.debug = True
app.run()