Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 451d0c64c6
Fetching contributors…

Cannot retrieve contributors at this time

executable file 122 lines (104 sloc) 3.593 kB
#!/usr/bin/python
import sys, re, time, os, codecs, getopt, SimpleHTTPServer, BaseHTTPServer
import jinja2, markdown
from SimpleHTTPServer import SimpleHTTPRequestHandler
# Settings
SOURCE = "./posts/"
DESTINATION = "./blog/"
HOME_SHOW = 15
TEMPLATE_PATH = "./templates/"
TEMPLATE_OPTIONS = {}
TEMPLATES = {
'home': "home.html",
'post': "post.html",
'archive': "archive.html",
}
TIME_FORMAT = "%b %d, %Y"
ENTRY_TIME_FORMAT = "%m-%d-%Y"
# FORMAT should be a callable that takes in text and returns formatted text
FORMAT = lambda text: markdown.markdown(text, ['footnotes',])
STEPS = []
def step(func):
def wrapper(*args, **kwargs):
print "Starting " + func.__name__ + "...",
func(*args, **kwargs)
print "Done."
STEPS.append(wrapper)
return wrapper
def get_tree(source):
files = []
for root, ds, fs in os.walk(source):
for name in fs:
if name[0] == ".": continue
path = os.path.join(root, name)
f = open(path, "rU")
title = f.readline().rstrip()
date = time.strptime(f.readline().strip(), ENTRY_TIME_FORMAT)
year, month, day = date[:3]
files.append({
'title': title,
'epoch': time.mktime(date),
'content': FORMAT(''.join(f.readlines()[1:]).decode('UTF-8')),
'url': '/'.join([str(year), "%.2d" % month, "%.2d" % day, os.path.splitext(name)[0] + ".html"]),
'pretty_date': time.strftime(TIME_FORMAT, date),
'date': date,
'year': year,
'month': month,
'day': day,
'filename': name,
})
f.close()
return files
def compare_entries(x, y):
result = cmp(-x['epoch'], -y['epoch'])
if result == 0:
return -cmp(x['filename'], y['filename'])
return result
def write_file(url, data):
path = DESTINATION + url
dirs = os.path.dirname(path)
if not os.path.isdir(dirs):
os.makedirs(dirs)
file = open(path, "w")
file.write(data.encode('UTF-8'))
file.close()
@step
def generate_homepage(f, e):
"""Generate homepage"""
template = e.get_template(TEMPLATES['home'])
write_file("../index.html", template.render(entries=f[:HOME_SHOW]))
@step
def master_archive(f, e):
"""Generate master archive list of all entries"""
template = e.get_template(TEMPLATES['archive'])
write_file("archives.html", template.render(entries=f))
@step
def detail_pages(f, e):
"""Generate detail pages of individual posts"""
template = e.get_template(TEMPLATES['post'])
for file in f:
write_file(file['url'], template.render(entry=file))
def chisel():
print "Chiseling..."
print "\tReading files...",
files = sorted(get_tree(SOURCE), cmp=compare_entries)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_PATH), **TEMPLATE_OPTIONS)
print "Done."
print "\tRunning steps..."
for step in STEPS:
print "\t\t",
step(files, env)
print "\tDone."
print "Done."
def main():
opts, args = getopt.getopt(sys.argv[1:], "s", ["server"])
for opt, arg in opts:
if opt in ("-s", "--server"):
SimpleHTTPRequestHandler.protocol_version = "HTTP/1.0"
httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 8000), SimpleHTTPRequestHandler)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], sa[1], "..."
httpd.serve_forever()
chisel()
if __name__ == "__main__":
sys.exit(main())
Jump to Line
Something went wrong with that request. Please try again.