-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathlogr.py
executable file
·72 lines (55 loc) · 2.16 KB
/
logr.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
import os
from os import listdir
from flask import Flask, render_template
from flaskext.markdown import Markdown
from utils import list_articles, slugify
logr = Flask(__name__)
logr.config.from_object('config')
logr.config['ARTICLES'] = list_articles()
Markdown(logr)
ARTICLE_DIR = logr.config['ARTICLE_DIR']
PAGES_DIR = logr.config['PAGES_DIR']
ARTICLES = logr.config['ARTICLES']
EXTENSIONS = logr.config['EXTENSIONS']
@logr.route('/')
def index():
"""
Render a template to hold the front page blurb.
"""
with open(os.path.join(PAGES_DIR, 'FrontPage.md'), 'r') as f:
blurb = f.read().decode('utf8')
return render_template('index.html', articles=ARTICLES, blurb=blurb)
@logr.route('/code')
def code():
with open(os.path.join(PAGES_DIR, 'code.md'), 'r') as f:
code = f.read().decode('utf8')
return render_template('code.html', code=code)
@logr.route('/resume')
def resume():
with open(os.path.join(PAGES_DIR, 'resume.md'), 'r') as f:
resume = f.read().decode('utf8')
return render_template('resume.html', resume=resume)
@logr.route('/b/<slug>', methods=['GET'])
def show(slug):
"""
Search the `articles` directory for an article whose slug matches the URL
parameter. When we find the article, render it.
"""
# Find the right article
for file_ in listdir(ARTICLE_DIR):
if file_.endswith(EXTENSIONS):
with open(os.path.join(ARTICLE_DIR, file_), 'r') as f:
if slug == slugify(f.readline()):
article = os.path.join(ARTICLE_DIR, file_)
break
# Now that we've found the right article, let's process it.
with open(article, 'r') as f:
lines = f.read().split('\n')
# We don't need title or category, but it's easier to explicitly state
# why we're popping the first two lines.
title = lines.pop(0).strip() # Title should appear on the first line
category = lines.pop(0).strip() # Category should appear on the second
source = '\n'.join(lines).decode('utf8')
return render_template('show.html', article=dict(source=source))
if __name__ == '__main__':
logr.run()