import datetime , os , re
from bottle import error , route , run , static_file , template
loc = ' diary/entries/ '
def list_headlines ( articles ) :
head = count_lines ( loc + ' raw/res/head.txt ' )
result = [ ]
text = [ ]
for article in articles :
path = loc + article
with open ( path ) as f :
text = f . readlines ( )
b = [ ]
b . append ( path )
b . append ( clean ( text [ head ] . replace ( ' <br> ' , ' ' ) ) )
result . append ( b )
return result
def list_snippets ( articles ) :
limit = 4
total = len ( articles )
head = count_lines ( loc + ' raw/res/head.txt ' )
tail = count_lines ( loc + ' raw/res/tail.txt ' )
result = [ ]
for article in articles :
path = loc + article
text = [ ]
a = [ ]
length = 0
with open ( loc + article ) as f :
text = f . readlines ( )
length = len ( text )
a . append ( clean ( text [ head ] ) ) # title
content = snip_article ( find_content ( article , length , head , tail ) , path )
if content . count ( ' <ul> ' ) > content . count ( ' </ul> ' ) :
content + = ' </ul> '
a . append ( content ) # snippet
a . append ( text [ head + 1 ] . replace ( ' <br> ' , ' ' ) ) # timestamp
a . append ( ' / ' + path . replace ( ' .tpl ' , ' ' ) ) # URL
a . append ( clean ( text [ head ] ) . replace ( ' ' , ' + ' ) ) # social media title
result . append ( a )
return result
def count_lines ( fname ) :
with open ( fname ) as f :
for linenum , line in enumerate ( f , 1 ) :
pass
return linenum
def find_content ( article , length , head , tail ) :
content = " "
with open ( loc + article ) as f :
for pos , line in enumerate ( f ) :
if pos > head + 1 and pos < length - tail :
content + = line
return content
def clean ( result ) :
result = result . replace ( ' <br> ' , ' ' )
result = re . sub ( r ' <a href=.*?> ' , ' ' , result )
result = result . replace ( ' </a> ' , ' ' )
result = re . sub ( r ' <h \ d> ' , ' ' , result )
result = re . sub ( r ' </h \ d> ' , ' ' , result )
return result
def snip_article ( article , path ) :
article = clean ( article )
limit = 300
result = article [ 0 : min ( len ( article ) , limit ) ]
result = result . rsplit ( ' ' , 1 ) [ 0 ]
# cut off after 5 lines, too?
return result + " ... "
def sort_files ( files ) :
files . sort ( reverse = True )
return files #list(reversed(files))
def gather_files ( ) :
files = os . listdir ( loc )
files . remove ( ' raw ' )
return files
# Static
@route ( ' /static/css/<filename:path> ' )
def serve_css ( filename ) :
return static_file ( filename , root = ' static/css ' )
@route ( ' /static/img/<filename:path> ' )
def serve_img ( filename ) :
return static_file ( filename , root = ' static/img ' )
# Routes
@error ( 404 )
def error404 ( error ) :
return " unfortunately, a 404 error "
@route ( ' / ' )
def index ( ) :
""" home page """
info = { ' css ' : ' index ' , ' news ' : list_headlines ( sort_files ( gather_files ( ) ) [ 0 : 10 ] ) , ' title ' : ' chimchooree \' s dev space - blessfrey ' , ' year ' : datetime . datetime . now ( ) }
return template ( ' index.tpl ' , info )
@route ( ' /game ' )
def game ( ) :
""" game page """
info = { ' css ' : ' game ' , ' title ' : ' blessfrey - about the game ' , ' year ' : datetime . datetime . now ( ) }
return template ( ' game.tpl ' , info )
@route ( ' /presskit ' )
def presskit ( ) :
""" press page """
info = { ' css ' : ' presskit ' , ' title ' : ' blessfrey - presskit ' , ' year ' : datetime . datetime . now ( ) }
return template ( ' presskit.tpl ' , info )
@route ( ' /diary ' )
def diary2 ( ) :
return diary ( 0 )
@route ( ' /diary/<page:int> ' )
def diary ( page ) :
""" diary page """
assert isinstance ( page , int )
info = { ' css ' : ' diary ' , ' title ' : ' blessfrey - developer diary ' , ' year ' : datetime . datetime . now ( ) , ' snippets ' : list_snippets ( sort_files ( gather_files ( ) ) ) , ' latest ' : list_headlines ( sort_files ( gather_files ( ) ) [ 0 : 5 ] ) , ' total ' : len ( gather_files ( ) ) , ' limit ' : 4 , ' cluster ' : 3 , ' page ' : page }
return template ( ' diary.tpl ' , info )
@route ( ' /entries/<page:int> ' )
def entry ( page ) :
""" diary entry """
info = { ' css ' : ' diary ' , ' title ' : ' blessfrey - developer diary ' , ' year ' : datetime . datetime . now ( ) , ' latest ' : list_headlines ( sort_files ( gather_files ( ) ) [ 0 : 5 ] ) , ' page ' : page }
return template ( ' feature.tpl ' , info )
@route ( ' /box ' )
def box ( ) :
""" personal page """
info = { ' css ' : ' box ' , ' title ' : ' chimchooree \' s personal page ' , ' year ' : datetime . datetime . now ( ) }
return template ( ' box.tpl ' , info )
@route ( ' /credits ' )
def credits ( ) :
""" credits page """
info = { ' css ' : ' box ' , ' title ' : ' blessfrey - credits ' , ' year ' : datetime . datetime . now ( ) }
return template ( ' credits.tpl ' , info )
@route ( ' /contact ' )
def contact ( ) :
""" contact page """
info = { ' css ' : ' box ' , ' title ' : ' blessfrey - contact chimchooree ' , ' year ' : datetime . datetime . now ( ) }
return template ( ' contact.tpl ' , info )
if __name__ == ' __main__ ' :
run ( )