#!/usr/bin/python3

import cgi
import cgitb
import json
import os
cgitb.enable()


print("Content-type: text/html\n")
print("<head><style>")
print("body {background-color: mintcream;}")
print("</style></head>")
print("""<h1 style="color:black;">Blog</h1>""")
file_names= [f for f in os.listdir('posts') if os.path.isfile(os.path.join('posts', f))]
file_names = sorted(file_names, reverse=True)
for i in file_names:
    i = 'posts/' + i
    data = open(i, "r")
    data = json.load(data)
    print('<div style="background-color: mintcream; border: 5px solid seagreen; border-radius: 10px; padding: 10px;">')
    print(str(data['title']) + "<br>Datum: " + str(data['published']))
    print('<hr>')
    print(str(data['content']) + '<br><hr>')
    for k in data['tags']:
        print('<a href="tags-show.py?tag=' + str(k) + '">#' + str(k) + '</a>')
    print('</div><br>')

print()
print('<a href="tags-show.py">Alle Tags!</a><br>')
print('<a href="posts-create.py">Einen neuen Post verfassen!</a>')
