-
Notifications
You must be signed in to change notification settings - Fork 0
/
gollum_press.py
111 lines (83 loc) · 2.99 KB
/
gollum_press.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
# -*- coding: utf-8 -*-
import sys
import os
import urllib
import shutil
from dateutil import parser
from flask import Flask, request, redirect, url_for, abort
from flask.templating import render_template
from werkzeug.contrib.atom import AtomFeed
current_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(current_dir)
from posts import Posts
app = Flask(__name__, static_path='')
app.config.from_pyfile('config.cfg')
# config
working_dir = app.config['REPOSITORY']
index_page = app.config['INDEX_PAGE']
posts_per_page = app.config['POSTS_PER_PAGE']
theme = app.config['THEME']
site_url = app.config['SITE_URL']
posts = Posts(working_dir=working_dir,
index=index_page,
per_page=posts_per_page)
@app.route('/')
def index():
return redirect(url_for('get_posts'))
@app.route('/posts', methods=['GET'])
def get_posts():
page = int(request.args.get('page', '1'))
path = u"{0}/{1}.md".format(working_dir, index_page)
if not os.path.exists(path):
abort(500)
posts.refresh()
return render_template(u"{0}/post_list.html".format(theme),
posts=posts.get_posts(page), recent=posts.get_recent_metas(),
page=page, total_pages=posts.get_num_of_pages())
@app.route('/posts/<path:post_id>', methods=['GET'])
def get_post(post_id):
posts.refresh()
post = posts.get_post(post_id)
if post is None:
abort(404)
return render_template(u"{0}/post.html".format(theme),
post=post, recent=posts.get_recent_metas())
@app.route('/atom.xml', methods=['GET'])
def get_feed():
feed = AtomFeed(app.config['TITLE'], feed_url=request.url, url=request.url_root)
posts.refresh()
for post in posts.get_posts(1):
date = parser.parse(post.date)
escaped = urllib.quote(post.post_id.encode("utf-8"))
url = u"{0}/posts/{1}".format(site_url, escaped)
feed.add(post.title, post.content_markup.unescape(),
id=url,
content_type='html',
author=post.author,
url=url,
updated=date,
published=date)
return feed.get_response()
@app.errorhandler(404)
def page_not_found(error):
posts.refresh()
return render_template(u"{0}/404.html".format(theme), recent=posts.get_recent_metas()), 404
@app.errorhandler(500)
def internal_server_error(error):
return render_template(u"{0}/500.html".format(theme)), 404
def symlink(src, dst):
if os.path.exists(src):
if not os.path.exists(dst):
os.symlink(src, dst)
elif not os.path.islink(dst):
if os.path.isdir(dst):
shutil.rmtree(dst)
else:
os.remove(dst)
os.symlink(src, dst)
elif os.readlink(dst) != src:
os.remove(dst)
os.symlink(src, dst)
symlink(u'{0}/images'.format(working_dir), u'{0}/static/images'.format(current_dir))
if __name__ == '__main__':
app.run(debug=True)