first, breaking out all the setup optiosn into smaller functions for better readability. adding an option to set blog to be published or not, and checks this before printing public feels list.
238 lines
5.7 KiB
Python
238 lines
5.7 KiB
Python
#!/usr/bin/python
|
|
|
|
import os
|
|
import time
|
|
import subprocess
|
|
import re
|
|
import mistune
|
|
|
|
import chatter
|
|
|
|
SOURCE = os.path.join("/home", "endorphant", "projects", "ttbp", "bin")
|
|
USER = os.path.basename(os.path.expanduser("~"))
|
|
PATH = os.path.join("/home", USER, ".ttbp")
|
|
|
|
LIVE = "http://tilde.town/~"
|
|
WWW = os.path.join(PATH, "www")
|
|
CONFIG = os.path.join(PATH, "config")
|
|
DATA = os.path.join(PATH, "entries")
|
|
FEED = os.path.join(SOURCE, "www", "index.html")
|
|
|
|
HEADER = ""
|
|
FOOTER = ""
|
|
FILES = []
|
|
|
|
def load():
|
|
global HEADER
|
|
global FOOTER
|
|
|
|
HEADER = open(os.path.join(CONFIG, "header.txt")).read()
|
|
FOOTER = open(os.path.join(CONFIG, "footer.txt")).read()
|
|
|
|
load_files()
|
|
|
|
def load_files():
|
|
global FILES
|
|
|
|
FILES = []
|
|
for filename in os.listdir(DATA):
|
|
filename = os.path.join(DATA, filename)
|
|
if os.path.isfile(filename) and valid(filename):
|
|
FILES.append(filename)
|
|
|
|
FILES.sort()
|
|
FILES.reverse()
|
|
|
|
def write(outurl="default.html"):
|
|
outfile = open(os.path.join(WWW, outurl), "w")
|
|
|
|
outfile.write("<!--generated by the tilde.town blogging platform on "+time.strftime("%d %B %y")+"\nhttp://tilde.town/~endorphant/ttbp/-->\n\n")
|
|
|
|
for line in HEADER:
|
|
outfile.write(line)
|
|
|
|
outfile.write("\n")
|
|
|
|
for filename in FILES:
|
|
write_page(filename)
|
|
for line in write_entry(filename):
|
|
outfile.write(line)
|
|
|
|
outfile.write("\n")
|
|
|
|
for line in FOOTER:
|
|
outfile.write(line)
|
|
|
|
outfile.close()
|
|
|
|
return os.path.join(LIVE+USER,os.path.basename(os.path.realpath(WWW)),outurl)
|
|
|
|
def write_page(filename):
|
|
# makes a single permalink page
|
|
|
|
outurl = os.path.join(WWW, "".join(parse_date(filename))+".html")
|
|
outfile = open(outurl, "w")
|
|
|
|
outfile.write("<!--generated by the tilde.town blogging platform on "+time.strftime("%d %B %y")+"\nhttp://tilde.town/~endorphant/ttbp/-->\n\n")
|
|
|
|
for line in HEADER:
|
|
outfile.write(line)
|
|
|
|
outfile.write("\n")
|
|
|
|
for line in write_entry(filename):
|
|
outfile.write(line)
|
|
|
|
outfile.write("\n")
|
|
|
|
for line in FOOTER:
|
|
outfile.write(line)
|
|
|
|
outfile.close()
|
|
|
|
return outurl
|
|
|
|
def write_entry(filename):
|
|
# dump given file into entry format, return as list of strings
|
|
|
|
date = parse_date(filename)
|
|
|
|
entry = [
|
|
"\t\t<p><a name=\""+date[0]+date[1]+date[2]+"\"></a><br /><br /></p>\n",
|
|
"\t\t<div class=\"entry\">\n",
|
|
"\t\t\t<h5><a href=\"#"+date[0]+date[1]+date[2]+"\">"+date[2]+"</a> "+chatter.month(date[1])+" "+date[0]+"</h5>\n"
|
|
#"\t\t\t<P>"
|
|
]
|
|
|
|
raw = []
|
|
rawfile = open(os.path.join(DATA, filename), "r")
|
|
|
|
for line in rawfile:
|
|
raw.append(line)
|
|
rawfile.close()
|
|
|
|
entry.append("\t\t\t"+mistune.markdown("".join(raw), escape=False, hard_wrap=False))
|
|
|
|
#for line in raw:
|
|
#entry.append(line+"\t\t\t")
|
|
#if line == "\n":
|
|
# entry.append("</p>\n\t\t\t<p>")
|
|
|
|
#entry.append("</p>\n")
|
|
entry.append("\t\t\t<p style=\"font-size:.6em; font-color:#808080; text-align: right;\"><a href=\""+"".join(date)+".html\">permalink</a></p>\n")
|
|
entry.append("\n\t\t</div>\n")
|
|
|
|
return entry
|
|
|
|
def parse_date(file):
|
|
# assuming a filename of YYYYMMDD.txt, returns a list of
|
|
# ['YYYY', 'MM', 'DD']
|
|
|
|
rawdate = os.path.splitext(os.path.basename(file))[0]
|
|
|
|
date = [rawdate[0:4], rawdate[4:6], rawdate[6:]]
|
|
|
|
return date
|
|
|
|
def meta(entries = FILES):
|
|
# takes a list of filenames and returns:
|
|
# [0] absolute path
|
|
# [1] ctime
|
|
# [2] wc -w
|
|
# [3] timestamp "DD month YYYY at HH:MM"
|
|
# [4] entry date YYYY-MM-DD
|
|
# [5] author
|
|
# sorted in reverse date order by [4]
|
|
|
|
meta = []
|
|
|
|
for filename in entries:
|
|
ctime = os.path.getctime(filename)
|
|
wc = subprocess.check_output(["wc","-w",filename]).split()[0]
|
|
timestamp = time.strftime("%Y-%m-%d at %H:%M", time.localtime(ctime))
|
|
date = "-".join(parse_date(filename))
|
|
author = os.path.split(os.path.split(os.path.split(os.path.split(filename)[0])[0])[0])[1]
|
|
|
|
|
|
meta.append([filename, ctime, wc, timestamp, date, author])
|
|
|
|
meta.sort(key = lambda filename:filename[4])
|
|
meta.reverse()
|
|
|
|
return meta
|
|
|
|
def valid(filename):
|
|
# check if the filename is YYYYMMDD.txt
|
|
|
|
filesplit = os.path.splitext(os.path.basename(filename))
|
|
|
|
if filesplit[1] != ".txt":
|
|
return False
|
|
|
|
pattern = '^((19|20)\d{2})(0[1-9]|1[0-2])(0[1-9]|1\d|2\d|3[01])$'
|
|
|
|
if not re.match(pattern, filesplit[0]):
|
|
return False
|
|
|
|
return True
|
|
|
|
def write_global_feed(blogList):
|
|
# takes an array of the current global blog status and prints it to
|
|
# set www
|
|
|
|
outfile = open(FEED, "w")
|
|
|
|
## header
|
|
outfile.write("""\
|
|
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2//EN\">
|
|
<html>
|
|
<head>
|
|
<title>tilde.town feels engine</title>
|
|
<link rel=\"stylesheet\" href=\"style.css\" />
|
|
</head>
|
|
<body>
|
|
<h1>tilde.town feels engine</h1>
|
|
|
|
<h2><a href="https://github.com/modgethanc/ttbp">github
|
|
repo</a> | <a
|
|
href="http://tilde.town/~endorphant/blog/20160510.html">state
|
|
of the ttbp</a></h2> <div class="box">
|
|
<p>curious? run <b>~endorphant/bin/ttbp</b> while logged in to tilde.town.</p>
|
|
<p>it's still a little volatile. let me know if anything breaks.</p>
|
|
<p> </p>
|
|
|
|
<h3>live feels-sharing:</h3>
|
|
<p><i>(time not exactly to scale)</i></p>
|
|
<div class=\"feed\">
|
|
<ul>
|
|
""")
|
|
|
|
## feed
|
|
for blog in blogList:
|
|
outfile.write("""
|
|
<li>"""+blog+"""</li>\
|
|
""")
|
|
|
|
## footer
|
|
outfile.write("""
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
""")
|
|
|
|
outfile.close()
|
|
|
|
|
|
#############
|
|
#############
|
|
#############
|
|
|
|
def test():
|
|
load()
|
|
|
|
metaTest = meta()
|
|
|
|
for x in metaTest:
|
|
print(x)
|