old-homepage/htmlgen/htmlgen.py
Morten Delenk 5263b05db1 Whoops
2016-06-12 18:04:59 +02:00

37 lines
1.6 KiB
Python

import cgitb
import markdown2
import datetime
import storage
cgitb.enable()
class HTMLgen:
def __init__(self, layout, title):
self.layout=layout
self.articles=[]
self.titles=[]
self.authors=[]
self.dates=[]
self.title=title
self.asideHTML=""
def addArticle(self, name, markdown, author="darklink", date=0):
self.articles.append(markdown2.markdown(markdown, extras=["tables","spoiler"]))
self.titles.append(name)
self.authors.append(author)
self.dates.append(date)
def prependHTML(self, text):
self.asideHTML=text+self.asideHTML
def appendHTML(self, text):
self.asideHTML=self.asideHTML+text
def renderSite(self):
nav=""
x=len(self.titles)-1
for title in self.titles[::-1]:
nav=nav+("<a href=\"#%i\">%s</a><br/>" % (x, title))
x=x-1
main=""
x=len(self.articles)-1
for article in self.articles[::-1]:
main=main+("<h2 id=\"%i\">%s</h2><p>Written on <time datetime=\"%s\">%s</time> by %s</p><article>%s</article>" %(x,self.titles[x],datetime.datetime.fromtimestamp(self.dates[x]).strftime("%Y-%m-%d %H:%M:%S"),datetime.datetime.fromtimestamp(self.dates[x]).strftime("%c"),self.authors[x],article))
main=main+("<a href=\"comments.py?aid=%i\">Comments (%i)</a>") % (x, storage.count("comments-%i"%x))
x=x-1
styleargs = {"title":self.title,"nav":nav,"main":main,"aside":self.asideHTML,"footer":"Copyright 2016 Morten"}
return self.layout%styleargs