|
| 1 | +import os |
| 2 | +import markdown |
| 3 | +import datetime |
| 4 | + |
| 5 | +from flask import Flask |
| 6 | +from flask import make_response |
| 7 | +from flask import render_template |
| 8 | +from flask import render_template_string |
| 9 | +from flask import send_from_directory |
| 10 | + |
| 11 | +from flask_flatpages import FlatPages |
| 12 | +from flask_flatpages import pygments_style_defs |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +def my_renderer(text): |
| 17 | + """Inject the markdown rendering into the jinga template""" |
| 18 | + rendered_body = render_template_string(text) |
| 19 | + pygmented_body = markdown.markdown(rendered_body, extensions=['codehilite', 'fenced_code', 'tables']) |
| 20 | + return pygmented_body |
| 21 | + |
| 22 | + |
| 23 | +app = Flask(__name__) |
| 24 | +app.config['DEBUG'] = False |
| 25 | + |
| 26 | +app.config.update({ |
| 27 | + 'FLATPAGES_EXTENSION': ['.md', '.markdown'], |
| 28 | + 'FLATPAGES_MARKDOWN_EXTENSIONS': ['codehilite', 'fenced_code'], |
| 29 | + 'FLATPAGES_HTML_RENDERER': my_renderer |
| 30 | +}) |
| 31 | +pages = FlatPages(app) |
| 32 | + |
| 33 | + |
| 34 | +@app.route('/favicon.ico') |
| 35 | +def favicon(): |
| 36 | + return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', |
| 37 | + mimetype='image/vnd.microsoft.icon') |
| 38 | + |
| 39 | + |
| 40 | +@app.route('/pygments.css') |
| 41 | +def pygments_css(): |
| 42 | + return pygments_style_defs('tango'), 200, {'Content-Type': 'text/css'} |
| 43 | + |
| 44 | + |
| 45 | +@app.route('/googleac8131d70b9df19d.html') |
| 46 | +def googlewebmastertools(): |
| 47 | + content = """google-site-verification: googleac8131d70b9df19d.html""" |
| 48 | + resp = make_response(content, 200, {'Content-Type': 'text/plain'}) |
| 49 | + return resp |
| 50 | + |
| 51 | + |
| 52 | +@app.route('/robots.txt') |
| 53 | +def robots(): |
| 54 | + content = """User-agent: * |
| 55 | +Disallow: /static/ |
| 56 | +""" |
| 57 | + resp = make_response(content, 200, {'Content-Type': 'text/plain'}) |
| 58 | + return resp |
| 59 | + |
| 60 | + |
| 61 | +@app.route('/meejinnz/') |
| 62 | +@app.route('/meejinnz/<path:path>') |
| 63 | +def meejinnz(path='index.html'): |
| 64 | + return send_from_directory('meejinnz', path) |
| 65 | + |
| 66 | + |
| 67 | +@app.route('/html/<path:path>') |
| 68 | +def htmldir(path): |
| 69 | + return send_from_directory('html', path) |
| 70 | + |
| 71 | + |
| 72 | +@app.route('/') |
| 73 | +def home(): |
| 74 | + # sort the posts to reverse date order |
| 75 | + posts = [p for p in pages if p.meta.get('tag', 'noindex')] |
| 76 | + posts.sort(reverse=True, key=lambda p: p.meta.get('published', datetime.date(1970, 1, 1))) |
| 77 | + |
| 78 | + # split them into technical and personal, omit those that aren't tagged |
| 79 | + opinions, technicals = list(), list() |
| 80 | + for post in posts: |
| 81 | + tag = post.meta.get('tag', 'noindex') |
| 82 | + if tag == 'noindex': |
| 83 | + continue |
| 84 | + elif tag == 'technical': |
| 85 | + technicals.append(post) |
| 86 | + else: |
| 87 | + opinions.append(post) |
| 88 | + |
| 89 | + return render_template("index.html", technical_posts=technicals, opinion_posts=opinions) |
| 90 | + |
| 91 | + |
| 92 | +@app.route('/<path:path>') |
| 93 | +def page(path): |
| 94 | + page = pages.get_or_404(path) |
| 95 | + return render_template('post.html', page=page) |
| 96 | + |
| 97 | + |
| 98 | +@app.context_processor |
| 99 | +def inject_debug(): |
| 100 | + return dict(debug=app.debug) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == '__main__': |
| 104 | + # if running as a raw script assume this is a development instance and try to run livereload |
| 105 | + app.config['DEBUG'] = True |
| 106 | + try: |
| 107 | + from livereload import Server |
| 108 | + server = Server(app.wsgi_app) |
| 109 | + server.serve() |
| 110 | + except ImportError: |
| 111 | + app.run(threaded=True) |
0 commit comments