-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
124 lines (103 loc) · 2.87 KB
/
app.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
import os
import re
import logging
import random
from logging.handlers import RotatingFileHandler
from flask import Flask
from flaskext.markdown import Markdown
# Setup Flask App
app = Flask(__name__)
# Initialize Markdown Processing
md = Markdown(app,
safe_mode=False,
output_format='html5',
)
# Logging configuration
file_handler = RotatingFileHandler('/var/log/nateferrero.gallery.log')
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)
# Current app directory
cwd = os.path.dirname(os.path.realpath(__file__))
# Render method
def render(section, page):
# Load page head
try:
f = open(os.path.join(cwd, 'data', '__head__.html'))
head = f.read()
f.close()
except:
head = ""
# Load page sections
top_header = render_file('', '__header__')
top_footer = render_file('', '__footer__')
if section != '':
header = render_file(section, '__header__')
footer = render_file(section, '__footer__')
else:
header = footer = ''
content = render_file(section, page)
if content == "":
content = render_file('', '__notfound__')
page = "%s%s%s%s%s" % (top_header, header, content, footer, top_footer)
# Get title
treg = re.compile('<h1>(.+?)</h1>')
match = treg.search(page)
if match:
t = match.group(0)
n = treg.search(page, match.end())
if n:
t = t + ' / ' + n.group(0)
title = re.sub('<.+?>', '', t)
else:
title = 'Untitled'
# Generate HTML document
return """
<!doctype html>
<html>
<head>
<link rel="shortcut icon" href="/static/favicon.ico" />
<title>%s</title>
%s
</head>
<body>
<div id="page">
%s
</div>
</body>
</html>
""" % (title, head, page)
# Auto include a md file from within data/__include__
def auto_include(match):
try:
f = open(os.path.join(cwd, 'data', '__include__',
match.group(1) + '.md'))
content = f.read()
f.close()
return content
except:
return '« module not found »'
# Render a markdown file to HTML
def render_file(section, page):
try:
f = open(os.path.join(cwd, 'data', section, page + '.md'))
except:
return ""
html = f.read()
html = re.sub(r'\{\{(.+?)\}\}', auto_include, html)
html = md(html)
f.close()
return html
# Home Routing
@app.route("/")
def home():
return render('', '__index__')
# Section (index) Routing
@app.route("/<section>/")
def section(section):
return render(section, '__index__')
# Page Routing
@app.route('/<section>/<page>')
def page(section, page):
return render(section, page)
if __name__ == "__main__":
app.run()