Skip to content

Instantly share code, notes, and snippets.

@markhamilton
Last active December 16, 2015 17:19
Show Gist options
  • Save markhamilton/5469125 to your computer and use it in GitHub Desktop.
Save markhamilton/5469125 to your computer and use it in GitHub Desktop.
Compiles a directory tree of single file journal entries in this structure: ./2013/04/23 ./2013/04/24 ./2013/04/26 ... etc.
#!/usr/bin/python
import glob
# uncomment for FTP support -- requires ftplib
#from ftplib import FTP
output = open("index.html", "w")
html_escape_table = {
"&": "&",
'"': """,
"'": "'",
">": ">",
"<": "&lt;",
}
# Credit: http://stackoverflow.com/questions/2077283/
def htmlEscape(text):
return "".join(html_escape_table.get(c,c) for c in text)
def startBody():
output.write("<!DOCTYPE HTML>\r\n<html><head><title>My Journal</title></head><body>")
output.write("<h1>My Journal<h1>")
def endBody():
output.write("</body></html>");
def startEntry(date):
output.write("<h2>" + date + "</h2><!--startentry-->");
def endEntry():
output.write("<!--endentry-->");
def insertParagraph(paragraph):
p = paragraph.strip()
if p != "" and p != "\n":
p = htmlEscape(p)
output.write("<ul><li>")
output.write(p.replace("\n", "</li><li>"))
output.write("</li></ul>")
output.write("<br>")
#BEGIN
startBody();
#list all journal files and sort them in reverse chrono order (newest first)
#directory structure should be in ./YYYY/MM/DD format like so:
#./2013/04/31
files = glob.glob("./*/*/*")
files.sort(reverse=True)
# loop through journal files
for fileitem in files:
# read the journal file
f = open(fileitem)
j = f.read()
j = j.replace("\r", "")
# split into paragraphs
j = j.split("\n\n")
startEntry(fileitem)
# loop through paragraphs
for p in j:
insertParagraph(p)
endEntry()
endBody()
output.close()
# additional feature!
# uncomment the code here to upload this file to an FTP site:
#ftpuser="FTPUSER"
#ftppass="FTPPASS"
#ftphost="example.com"
#ftp = FTP(ftphost, ftpuser, ftppass)
#ftp.storlines("STOR index.html", open("index.html"))
@markhamilton
Copy link
Author

The FTP feature at the bottom is great for publishing journals automatically to the web. See it in action here:
http://iregretnothing.net/trent/journal/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment