-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestapp.py
More file actions
63 lines (52 loc) · 1.58 KB
/
testapp.py
File metadata and controls
63 lines (52 loc) · 1.58 KB
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
from flask import Flask, render_template, Markup
import markdown
import glob
# Setup flask, include Jade processing
app = Flask(__name__)
app.jinja_env.add_extension('pyjade.ext.jinja.PyJadeExtension')
# Retrieve info on .md posts
def get_posts():
posts = []
allPosts = glob.glob('./posts/*.md')
for thisPost in allPosts:
f = open(thisPost, 'r')
file_contents = f.read()
meta_ind = file_contents.index('\n')
meta = file_contents[0:meta_ind].split(', ')
contents = file_contents[meta_ind+2:len(file_contents)]
url = meta[0].lower().replace(' ', '-')
print(url)
posts.append({
'dir': thisPost,
'title': meta[0],
'url': url,
'author': meta[1],
'contents': contents
})
return posts
# store this for the app to use
posts = get_posts();
# Route index
@app.route('/')
def hello_world():
return 'Hello, World!'
# Route post index
@app.route('/posts/')
def jade(someString=None):
return render_template('postIndex.jade', posts=posts)
# Route post
@app.route('/posts/<post_url>')
def render_post(post_url=None):
i = -1
for index in range(len(posts)):
if (posts[index]['url'] == post_url):
i = index
if i != -1:
thisPost = posts[i]
title = thisPost['title']
text = Markup(markdown.markdown(thisPost['contents']))
return render_template('post.jade', text=text, title=title)
else:
return 'Post not found'
# Force app to run on specific port?
app.run(debug=True, host='0.0.0.0', port=5001)