2016-04-12 17:41:50 +00:00
import cgitb
import markdown2
2016-06-12 13:03:32 +00:00
import datetime
2016-06-12 16:04:01 +00:00
import storage
2016-04-12 17:41:50 +00:00
cgitb . enable ( )
class HTMLgen :
def __init__ ( self , layout , title ) :
self . layout = layout
self . articles = [ ]
self . titles = [ ]
2016-06-12 13:03:32 +00:00
self . authors = [ ]
self . dates = [ ]
2016-06-14 16:07:17 +00:00
self . ids = [ ]
2016-04-12 17:41:50 +00:00
self . title = title
2016-06-12 16:04:01 +00:00
self . asideHTML = " "
2016-06-14 16:07:17 +00:00
def addArticle ( self , name , markdown , author , date , aid , tags = None ) :
2016-06-12 12:27:29 +00:00
self . articles . append ( markdown2 . markdown ( markdown , extras = [ " tables " , " spoiler " ] ) )
2016-04-12 17:41:50 +00:00
self . titles . append ( name )
2016-06-12 13:03:32 +00:00
self . authors . append ( author )
self . dates . append ( date )
2016-06-14 16:07:17 +00:00
self . ids . append ( aid )
2016-06-12 16:04:01 +00:00
def prependHTML ( self , text ) :
self . asideHTML = text + self . asideHTML
def appendHTML ( self , text ) :
self . asideHTML = self . asideHTML + text
2016-06-12 16:49:18 +00:00
def renderSite ( self , comments = False ) :
2016-04-12 17:41:50 +00:00
nav = " "
2016-06-12 16:04:01 +00:00
x = len ( self . titles ) - 1
for title in self . titles [ : : - 1 ] :
2016-06-14 16:07:17 +00:00
nav = nav + ( " <a href= \" # %i \" > %s </a><br/> " % ( self . ids [ x ] , title ) )
2016-06-12 16:04:01 +00:00
x = x - 1
2016-04-12 17:41:50 +00:00
main = " "
2016-06-12 16:04:01 +00:00
x = len ( self . articles ) - 1
for article in self . articles [ : : - 1 ] :
2016-06-14 16:07:17 +00:00
main = main + ( " <h2 id= \" %i \" > %s </h2><p>Written on <time datetime= \" %s \" > %s </time> by %s </p><article> %s </article> " % ( self . ids [ 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 ) )
2016-06-12 16:49:18 +00:00
if not comments :
2016-06-14 16:07:17 +00:00
main = main + ( " <a href= \" comments.py?aid= %i \" >Comments ( %i )</a> " ) % ( self . ids [ x ] , storage . count ( " comments- %i " % self . ids [ x ] ) )
2016-06-12 16:04:01 +00:00
x = x - 1
2016-06-12 16:49:18 +00:00
if not comments :
styleargs = { " title " : self . title , " nav " : nav , " main " : main , " aside " : self . asideHTML , " footer " : " Copyright 2016 Morten " }
else :
styleargs = { " title " : self . title , " nav " : nav , " main " : self . asideHTML + main , " aside " : " " , " footer " : " Copyright 2016 Morten " }
2016-07-13 14:58:04 +00:00
return ' \n ' . join ( [ line for line in ( self . layout % styleargs ) . split ( ' \n ' ) if line . strip ( ) != ' ' ] )