Browse Source

Updated styles, added home functionality

* Home now renders from content instead of all in-template
* Titles are parsed from HTML :D
* Updated stylesheet to have better fonts and scale better (e.g. add
  margin)
* Any content path with '_hidden' is ignored
* Added link to home page to all Blog Posts
* XSRF tokens are randomized for eventual security
master
Macoy Madson 4 years ago
parent
commit
70d8d8e4d2
  1. 4
      ContentConverter.py
  2. 37
      SimpleBlogServer.py
  3. 2
      content/Home_hidden.org
  4. 24
      templates/BlogPost.html
  5. 12
      templates/Home.html
  6. 35
      webResources/styles.css

4
ContentConverter.py

@ -87,6 +87,10 @@ Interface
def getAllPostsList():
allPosts = []
for key, value in renderedDictionary.items():
# Hide folders and files with '_hidden' (they can still be retrieved though)
if '_hidden' in key:
continue
allPosts.append(key)
return allPosts

37
SimpleBlogServer.py

@ -1,14 +1,16 @@
#!/usr/bin/env python3
import tornado.gen
import tornado.httpclient
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.httpclient
import tornado.httpserver
import tornado.gen
import os
from datetime import datetime
import random
import re
import ContentConverter
@ -16,25 +18,39 @@ import ContentConverter
# Tornado handlers
#
def getTitleFromBody(htmlBody):
# The cardinal sin: do not use regex to parse HTML! :D
match = re.match(r'<h1.*>(.*)</h1>', htmlBody)
return match[1]
def getBlogHtmlBody(requestedContent):
renderedBody = ContentConverter.getRenderedBody(requestedContent)
if not renderedBody:
renderedBody = "<p>The post under '{}' does not exist.</p>".format(requestedContent)
return renderedBody
class HomeHandler(tornado.web.RequestHandler):
def get(self):
allPosts = ContentConverter.getAllPostsList()
self.render("templates/Home.html", allPosts=allPosts)
renderedHomeBody = getBlogHtmlBody('Home_hidden')
self.render("templates/Home.html", allPosts=allPosts, homeBody=renderedHomeBody)
class BlogHandler(tornado.web.RequestHandler):
def get(self, request):
contentTitle = "Blog: " + request
renderedBody = ContentConverter.getRenderedBody(request)
if not renderedBody:
renderedBody = "<p>The post under '{}' does not exist.</p>".format(request)
renderedBody = getBlogHtmlBody(request)
self.render("templates/BlogPost.html", title=contentTitle, postBody=renderedBody)
self.render("templates/BlogPost.html",
title=getTitleFromBody(renderedBody), postBody=renderedBody)
#
# Startup
#
def make_app():
# Makes sure we don't have predictable cookies to combat spoofed requests
randomGenerator = random.SystemRandom()
cookieSecret = str(randomGenerator.getrandbits(128))
return tornado.web.Application([
# Home page
(r'/', HomeHandler),
@ -51,11 +67,10 @@ def make_app():
# (r'/output/(.*)', tornado.web.StaticFileHandler, {'path' : 'output'}),
# Static files. Keep this at the bottom because it handles everything else
# TODO put these in a subdir so everything isn't accessible
(r'/webResources/(.*)', tornado.web.StaticFileHandler, {'path' : 'webResources'}),
],
xsrf_cookies=True,
cookie_secret='this is my org blog')
cookie_secret=cookieSecret)
if __name__ == '__main__':

2
content/Home_hidden.org

@ -0,0 +1,2 @@
* Simple Org Blog
The owner has not set up their ~Home_hidden.org~ file.

24
templates/BlogPost.html

@ -1,9 +1,17 @@
<html>
<head>
<title>{{ title }}</title>
<link rel="stylesheet" type="text/css" href="/webResources/styles.css">
</head>
<body>
{% raw postBody %}
</body>
</html>
<head>
<title>{{ title }}</title>
<link rel="stylesheet" type="text/css" href="/webResources/styles.css">
</head>
<body>
<article>
{% raw postBody %}
</article>
<br />
<nav>
<a href="/">Back to Home</a>
</nav>
<br />
</body>
</html>

12
templates/Home.html

@ -4,10 +4,12 @@
<link rel="stylesheet" type="text/css" href="/webResources/styles.css">
</head>
<body>
<h1>Simple Blog</h1>
<p>The owner hasn't set up their templates/Home.html template yet.</p>
{% for post in allPosts %}
<a href="blog/{{post}}">{{post}}</a><br />
{% end %}
{% raw homeBody %}
<nav>
{% for post in allPosts %}
<a href="blog/{{post}}">{{post}}</a><br />
{% end %}
</nav>
</body>
</html>

35
webResources/styles.css

@ -1,18 +1,35 @@
html {
margin: 15 15 15 15;
background-color: #333333;
}
body {
/* Center body */
margin: 0 auto;
width: 80%;
height: 100%;
background-color: #333333;
max-width: 800px;
/* Make sure lines don't get too long */
max-width: 700px;
}
p,
blockquote,
h1,
h2,
h3,
a,
li {
li,
tr,
td,
th {
color: #cccccc;
font-family: Arial;
}
/* Font sizes for consistently sized text */
p,
blockquote,
a,
li {
font-size: medium;
}
label {
@ -20,14 +37,6 @@ label {
color: #cccccc;
}
li {
margin-top: 0px;
margin-bottom: 0px;
margin-left: 20px;
font-size: small;
color: #aaaaaa;
}
img {
width: 100%;
height: auto;

Loading…
Cancel
Save