|
import re, os, markdown |
|
|
|
""" |
|
Credits: https://gist.github.com/Fedik/674f4148439698a6681032b3bec370b3 |
|
""" |
|
|
|
TEMPLATE = """ |
|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
|
<meta name="referrer" content="no-referrer" /> |
|
<meta name="referrer" content="unsafe-url" /> |
|
<meta name="referrer" content="origin" /> |
|
<meta name="referrer" content="no-referrer-when-downgrade" /> |
|
<meta name="referrer" content="origin-when-cross-origin" /> |
|
<title>donaldsebleung's blog</title> |
|
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> |
|
<style> |
|
body { |
|
font-family: Helvetica,Arial,sans-serif; |
|
} |
|
code, pre { |
|
font-family: monospace; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
<div class="container"> |
|
{{content}} |
|
</div> |
|
</body> |
|
</html> |
|
""" |
|
|
|
def handler(environ, start_response): |
|
path_info = environ['PATH_INFO'] |
|
valid_path = re.compile(r'^/blog/([0-9A-Za-z]+(\-[0-9A-Za-z]+)*)$') |
|
path_match = valid_path.match(path_info) |
|
if not path_match: |
|
status = '400 Bad Request' |
|
response_headers = [('Content-Type', 'text/plain')] |
|
start_response(status, response_headers) |
|
return [status] |
|
|
|
blog_name = path_match.group(1) |
|
blog_path = "/mnt/donaldsebleung-blog/%s.md" % blog_name |
|
if not os.path.isfile(blog_path): |
|
status = '404 Not Found' |
|
response_headers = [('Content-Type', 'text/plain')] |
|
start_response(status, response_headers) |
|
return [status] |
|
|
|
with open(blog_path, 'r') as blog_file: |
|
blog_contents = blog_file.read() |
|
blog_html = markdown.markdown(blog_contents, extensions=['extra', 'smarty'], output_format='html5') |
|
blog_page = TEMPLATE.replace('{{content}}', blog_html) |
|
status = '200 OK' |
|
response_headers = [('Content-Type', 'text/html')] |
|
start_response(status, response_headers) |
|
return [blog_page] |