-
-
Save hrj/6191345 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf8 -*- | |
from flask import Flask, redirect, url_for | |
from markdown import markdown | |
import os | |
import re | |
# create the app | |
# TODO: load config/template from files, with fallbacks | |
app = Flask(__name__) | |
root_dir = os.path.expanduser('~/Text') | |
template = """ | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>%s | Brainerd</title> | |
<style> | |
body { padding: 0; margin: 0; } | |
header { padding: 1em; background: royalblue; color: white; } | |
#container { width: 650px; margin: 0 auto; } | |
#content { padding: 1em; } | |
</style> | |
</head> | |
<body> | |
<div id="container"> | |
<header> | |
<p>Brainerd wiki system</p> | |
<h1>%s</h1> | |
</header> | |
<div id="content"> | |
%s | |
</div> | |
</div> | |
</body> | |
</html> | |
""" | |
@app.route("/") | |
@app.route("/w/") | |
def homepage(): | |
# TODO: make this a directory listing of root_dir | |
return "Hello there." | |
@app.route("/w/<path:page>") | |
def wiki(page): | |
# reformat the URL - space becomes hyphen | |
# also, strip out path injection attacks | |
# TODO: substitute back in the spaces, somehow | |
new_page = page.replace(" ", "-") | |
new_page = re.sub(r"\.+/", "", new_page) | |
if new_page is not page: | |
return redirect(url_for("wiki", page=new_page)) | |
# render file or directory | |
# TODO: directory listings, non–Markdown files, catch Markdown errors | |
file_path = "%s/%s.mdown" % (root_dir, page) | |
if os.path.isfile(file_path): | |
try: | |
with open(file_path, 'r') as f: | |
return template % (page, page, markdown(f.read())) | |
except: | |
return "EXCEPTION: Couldn’t render %s.mdown." % page | |
# render the page | |
return "Wiki page: %s" % page | |
# if running brainerd directly, boot the app | |
if __name__ == "__main__": | |
if not os.path.exists(root_dir) and not os.path.isdir(root_dir): | |
print "Error: Root directory %s doesn't exist." % root_dir | |
exit() | |
app.run() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.codehilite .hll { background-color: #ffffcc } | |
.codehilite .c { color: #808080 } /* Comment */ | |
.codehilite .err { color: #F00000; background-color: #F0A0A0 } /* Error */ | |
.codehilite .k { color: #008000; font-weight: bold } /* Keyword */ | |
.codehilite .o { color: #303030 } /* Operator */ | |
.codehilite .cm { color: #808080 } /* Comment.Multiline */ | |
.codehilite .cp { color: #507090 } /* Comment.Preproc */ | |
.codehilite .c1 { color: #808080 } /* Comment.Single */ | |
.codehilite .cs { color: #cc0000; font-weight: bold } /* Comment.Special */ | |
.codehilite .gd { color: #A00000 } /* Generic.Deleted */ | |
.codehilite .ge { font-style: italic } /* Generic.Emph */ | |
.codehilite .gr { color: #FF0000 } /* Generic.Error */ | |
.codehilite .gh { color: #000080; font-weight: bold } /* Generic.Heading */ | |
.codehilite .gi { color: #00A000 } /* Generic.Inserted */ | |
.codehilite .go { color: #808080 } /* Generic.Output */ | |
.codehilite .gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */ | |
.codehilite .gs { font-weight: bold } /* Generic.Strong */ | |
.codehilite .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ | |
.codehilite .gt { color: #0040D0 } /* Generic.Traceback */ | |
.codehilite .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ | |
.codehilite .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ | |
.codehilite .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ | |
.codehilite .kp { color: #003080; font-weight: bold } /* Keyword.Pseudo */ | |
.codehilite .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ | |
.codehilite .kt { color: #303090; font-weight: bold } /* Keyword.Type */ | |
.codehilite .m { color: #6000E0; font-weight: bold } /* Literal.Number */ | |
.codehilite .s { background-color: #fff0f0 } /* Literal.String */ | |
.codehilite .na { color: #0000C0 } /* Name.Attribute */ | |
.codehilite .nb { color: #007020 } /* Name.Builtin */ | |
.codehilite .nc { color: #B00060; font-weight: bold } /* Name.Class */ | |
.codehilite .no { color: #003060; font-weight: bold } /* Name.Constant */ | |
.codehilite .nd { color: #505050; font-weight: bold } /* Name.Decorator */ | |
.codehilite .ni { color: #800000; font-weight: bold } /* Name.Entity */ | |
.codehilite .ne { color: #F00000; font-weight: bold } /* Name.Exception */ | |
.codehilite .nf { color: #0060B0; font-weight: bold } /* Name.Function */ | |
.codehilite .nl { color: #907000; font-weight: bold } /* Name.Label */ | |
.codehilite .nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */ | |
.codehilite .nt { color: #007000 } /* Name.Tag */ | |
.codehilite .nv { color: #906030 } /* Name.Variable */ | |
.codehilite .ow { color: #000000; font-weight: bold } /* Operator.Word */ | |
.codehilite .w { color: #bbbbbb } /* Text.Whitespace */ | |
.codehilite .mf { color: #6000E0; font-weight: bold } /* Literal.Number.Float */ | |
.codehilite .mh { color: #005080; font-weight: bold } /* Literal.Number.Hex */ | |
.codehilite .mi { color: #0000D0; font-weight: bold } /* Literal.Number.Integer */ | |
.codehilite .mo { color: #4000E0; font-weight: bold } /* Literal.Number.Oct */ | |
.codehilite .sb { background-color: #fff0f0 } /* Literal.String.Backtick */ | |
.codehilite .sc { color: #0040D0 } /* Literal.String.Char */ | |
.codehilite .sd { color: #D04020 } /* Literal.String.Doc */ | |
.codehilite .s2 { background-color: #fff0f0 } /* Literal.String.Double */ | |
.codehilite .se { color: #606060; font-weight: bold; background-color: #fff0f0 } /* Literal.String.Escape */ | |
.codehilite .sh { background-color: #fff0f0 } /* Literal.String.Heredoc */ | |
.codehilite .si { background-color: #e0e0e0 } /* Literal.String.Interpol */ | |
.codehilite .sx { color: #D02000; background-color: #fff0f0 } /* Literal.String.Other */ | |
.codehilite .sr { color: #000000; background-color: #fff0ff } /* Literal.String.Regex */ | |
.codehilite .s1 { background-color: #fff0f0 } /* Literal.String.Single */ | |
.codehilite .ss { color: #A06000 } /* Literal.String.Symbol */ | |
.codehilite .bp { color: #007020 } /* Name.Builtin.Pseudo */ | |
.codehilite .vc { color: #306090 } /* Name.Variable.Class */ | |
.codehilite .vg { color: #d07000; font-weight: bold } /* Name.Variable.Global */ | |
.codehilite .vi { color: #3030B0 } /* Name.Variable.Instance */ | |
.codehilite .il { color: #0000D0; font-weight: bold } /* Literal.Number.Integer.Long */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment