Skip to content

Commit 85e0d92

Browse files
committed
Add freezer generators
1 parent d83dbe8 commit 85e0d92

1 file changed

Lines changed: 30 additions & 11 deletions

File tree

server.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#!/usr/bin/env python3
22
import json
33
import os
4-
import pathlib
5-
import shutil
64
import sys
7-
from jinja2 import Environment, FileSystemLoader, select_autoescape
85

9-
from flask import Flask, render_template, url_for
6+
from flask import Flask, render_template
107
from flask_flatpages import FlatPages
118
from flask_frozen import Freezer
129
from flaskext.markdown import Markdown
@@ -27,8 +24,8 @@
2724
freezer = Freezer(app)
2825
markdown_manager = Markdown(app, extensions=['fenced_code'], output_format='html5',)
2926

30-
pages_insights = [page for page in list(pages) if page.path.startswith('insights/')]
31-
pages_arenas = [page for page in list(pages) if page.path.startswith('arenas/')]
27+
pages_insights = [p for p in pages if p.path.startswith('insights/')]
28+
pages_arenas = [p for p in pages if p.path.startswith('arenas/')]
3229

3330
# Routes
3431
@app.route('/')
@@ -44,22 +41,45 @@ def page(path):
4441
def team():
4542
return render_template('team.html')
4643

47-
@app.route('/insights')
44+
@app.route('/insights/')
4845
def insights():
4946
return render_template('insights.html', pages=pages_insights)
5047

51-
@app.route('/arenas')
48+
@app.route('/insights/<path:path>/')
49+
def insight(path):
50+
return render_template('page.html', page=pages.get_or_404('insights/' + path))
51+
52+
@app.route('/arenas/')
5253
def arenas():
5354
return render_template('arenas.html', pages=pages_arenas)
5455

56+
@app.route('/arenas/<path:path>/')
57+
def arena(path):
58+
return render_template('page.html', page=pages.get_or_404('arenas/' + path))
59+
5560
@app.errorhandler(404)
5661
def page_not_found(path):
57-
# note that we set the 404 status explicitly
5862
return render_template('404.html'), 404
5963

64+
# Freezer generators
65+
@freezer.register_generator
66+
def page():
67+
for p in pages:
68+
if not p.path.startswith(('insights/', 'arenas/')):
69+
yield {'path': p.path}
70+
71+
@freezer.register_generator
72+
def insight():
73+
for p in pages_insights:
74+
yield {'path': p.path[9:]} # Strip 'insights/' prefix
75+
76+
@freezer.register_generator
77+
def arena():
78+
for p in pages_arenas:
79+
yield {'path': p.path[7:]} # Strip 'arenas/' prefix
80+
6081
@freezer.register_generator
6182
def static():
62-
# This will yield all static files for freezing
6383
for dirpath, dirnames, filenames in os.walk('static'):
6484
for filename in filenames:
6585
rel_dir = os.path.relpath(dirpath, 'static')
@@ -68,7 +88,6 @@ def static():
6888

6989

7090
if __name__ == "__main__":
71-
print(pages)
7291
if len(sys.argv) > 1 and sys.argv[1] == "build":
7392
freezer.freeze()
7493
else:

0 commit comments

Comments
 (0)