Latest articles:

Erster Eintrag

aus der Kategorie Coding

Einmal den Kopf Hoch:

Das hier stellt den ersten Eintrag dar und zeigt den tollen Coden um diese statische Website zu generieren.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# VERSION: Freitag 7 Oktober 2011, 23:13

import hashlib
import io
import subprocess
import sys

import simplejson

f = open("json/information.json", "r")
WEBSITE = simplejson.load(f)
f.close()


def readTemplate(filename):
    with io.open(filename, "r", encoding="utf-8") as f:
        return f.read()


def createheader(links):  # links look like this: rel title href
    try:
        t = len(links)
    except TypeError:
        print("Oops!  Links hat 0 Elemente... ")
        sys.exit(0)

    header = readTemplate("template/header.html")
    for i in range(0, t):
        header += (
            u'\t<link rel="'
            + links[i]["rel"]
            + u'" title="'
            + links[i]["title"]
            + u'" href="'
            + WEBSITE["URL"]
            + links[i]["href"]
            + u'" /> \n'
        )
    header += u"\n" + readTemplate("template/header1.html")
    return header


def createMOTD():
    f = open("json/motd.json", "r")
    j_obj = simplejson.load(f)
    f.close()
    MOTD = j_obj[0]["MOTD"]
    return MOTD


def createheaderbar(links):  # links look like this: rel title href
    try:
        t = len(links)
    except TypeError:
        print("Oops!  Links hat 0 Elemente... ")
        sys.exit(0)
    headerbar = u""
    with io.open("template/headerbar.html", "r", encoding="utf-8") as f:
        headerbar = f.read()
    # 	<link rel="contents" title="Inhaltsverzeichnis" href="show_categories.php">
    for i in range(0, t):
        headerbar += (
            u'\t\t\t<li><a href="'
            + WEBSITE["URL"]
            + links[i]["href"]
            + u'">'
            + links[i]["title"]
            + u"</a></li>\n"
        )
    with io.open("template/headerbar1.html", "r", encoding="utf-8") as f:
        headerbar += u"\n" + f.read()
    headerbar += (
        u"\n\t\t<h2><a href='https://www.airmack.de'>&#60;&#61;web@airmack.de: &#61;&#62;</a> echo \""
        + createMOTD()
        + u'" &#9647;</h2>\n\t\t<br />\n\t\n\t</div>\n<!-- end headerbar -->'
    )
    f.close()
    return headerbar


def createfooter(links):  # links look like this: rel title href
    try:
        t = len(links)
    except TypeError:
        print("Oops!  Links hat 0 Elemente... ")
        sys.exit(0)

    f = open("template/footer.html", "r")
    footer = f.read()
    f.close()

    for i in range(0, t):
        footer = (
            footer
            + u' <a href="'
            + WEBSITE["URL"]
            + links[i]["href"]
            + u'">'
            + links[i]["title"]
            + u"</a> \n"
        )
    f = open("template/footer1.html", "r")
    footer = footer + "\n" + f.read()
    f.close()
    return footer


def createleft(links, numberoflinks):  # links look like this: rel title href
    try:
        t = len(links)
    except TypeError:
        print("Oops!  Links hat 0 Elemente... ")
        sys.exit(0)
    # 	in case we should display more elements than we can reduce them
    if t < numberoflinks:
        numberoflinks = t

    f = open("template/left.html", "r")
    left = f.read()
    f.close()
    for i in range(0, numberoflinks):
        left = (
            left
            + u"\t\t<h3><a href ='"
            + WEBSITE["URL"]
            + u"single/"
            + links[i]["Hash"]
            + u".html'> \n \t\t\t "
            + links[i]["Titel"]
            + u"</a></h3><p class='category_date'> erstellt am "
            + links[i]["Datum"]
            + u"</p>\n \t\t\t"
        )
    f = open("template/left1.html", "r")
    left = left + u"\n" + f.read()
    f.close()
    return left


# links look like this: rel title href
def createoverview(links, Kategorien, header, headerbar, left, footer):
    try:
        t = len(Kategorien[0])
    except TypeError:
        print("Oops!  Links hat 0 Elemente... ")
        sys.exit(0)
    # 	in case we should display more elements than we can reduce them
    f = open("template/uebersicht.html", "r")
    uebersicht = header + headerbar + left + f.read()
    f.close()

    for i in range(0, t):
        uebersicht = (
            uebersicht + u"\t<div class='category_row'><img class='category_icon' src='"
        )
        uebersicht = (
            uebersicht
            + Kategorien[2][i]
            + u"' alt=''/>\n\t\t<h2  style='float:left;' class='trigger'> \n \t\t<a href='#'>"
            + Kategorien[0][i]
            + u" ("
            + str(Kategorien[1][i])
            + u" Artikel) </a></h2>\n \t\t\t<div class='toggle_container'><div class='block'>\n\t\t\t<ul style='list-style-type: none; margin-left: 10px;'><li>\n"
        )
        for j in range(0, len(links)):
            if Kategorien[0][i] == links[j]["Hauptkategorie"]:
                uebersicht = (
                    uebersicht
                    + u"\t\t\t\t<h3><a href='"
                    + WEBSITE["URL"]
                    + "single/"
                    + str(links[j]["Hash"])
                    + u".html'>"
                    + links[j]["Titel"]
                    + u"</a></h3>\n\t\t\t\t<p class='category_date'> erstellt am "
                    + links[j]["Datum"]
                    + u" </p> \n"
                )
        uebersicht = uebersicht + u"\t\t\t</li></ul>\n\t\t\t</div></div>\n\t</div> \n"

    f = open("template/uebersicht1.html", "r")
    uebersicht = uebersicht + "\n" + f.read() + footer
    f.close()

    f = open("single/uebersicht.html", "w")
    try:
        f.write(uebersicht.encode("UTF-8"))
    except TypeError:
        f.write(uebersicht)

    f.close()
    return uebersicht


def getcategories(articles):
    t = len(articles)
    f = open("json/kategorien.json", "r")
    k = simplejson.load(f)
    f.close()
    categories = []
    pictures = []
    number_of_elements = []
    for i in range(0, len(k)):
        categories.append(k[i]["Hauptkategorie"])
        pictures.append(WEBSITE["URL"] + k[i]["Bild"])
        number_of_elements.append(0)

    if not t:
        print("Oops!  Links hat 0 Elemente... ")
        sys.exit(0)
    else:
        for i in range(0, t):
            if not len(categories):
                categories.append(articles[i]["Hauptkategorie"])
                number_of_elements.append(1)
            else:
                found = -1
                for j in range(0, len(categories)):
                    if categories[j] == articles[i]["Hauptkategorie"]:
                        number_of_elements[j] = number_of_elements[j] + 1
                        found = j
                        break
                if found == -1:
                    categories.append(articles[i]["Hauptkategorie"])
                    number_of_elements.append(1)
                    pictures.append(WEBSITE["URL"] + "img/ka.png")
    return [categories, number_of_elements, pictures]


def createdisclaimer(header, headerbar, left, footer):
    f = open("json/information.json", "r")
    information = simplejson.load(f)
    f.close()
    f = open("template/impressum.html", "r")
    impres_header = f.read()
    f.close()
    disclaimer = (
        header
        + headerbar
        + left
        + impres_header
        + "\t\t<pre><code>"
        + information["Vorname"]
        + " "
        + information["Name"]
        + "\n"
        + information["Strasse"]
        + "\n"
        + information["Postleitzahl"]
        + " "
        + information["Stadt"]
        + "</code></pre>\n"
    )
    f = open("template/impressum1.html", "r")
    disclaimer = disclaimer + f.read() + information["Email"]
    f.close()
    f = open("template/impressum2.html", "r")
    disclaimer = (
        disclaimer
        + f.read()
        + "\t\t<pre><code>"
        + information["Vorname"]
        + " "
        + information["Name"]
        + "\n"
        + information["Strasse"]
        + "\n"
        + information["Postleitzahl"]
        + " "
        + information["Stadt"]
        + "</code></pre>\n"
    )
    f.close()

    f = open("template/impressum3.html", "r")
    disclaimer = disclaimer + f.read()
    disclaimer = disclaimer + footer
    f = open("single/impressum.html", "w")
    try:
        f.write(disclaimer.encode("UTF-8"))
    except TypeError:
        print("Fehler beim UTF8")
        f.write(disclaimer)
    f.close()


def createmain(header, headerbar, left, footer):
    f = open("json/articles.json", "r")
    information = simplejson.load(f)
    f.close()
    disclaimer = (
        header
        + headerbar
        + left
        + u"<div id='main'>\n\t<h2>"
        + information[0]["Titel"]
        + u"</h2>\n"
    )
    disclaimer += (
        u"\t<h3> aus der Kategorie " + information[0]["Hauptkategorie"] + " </h3>\n"
    )
    disclaimer += (
        u'\t<div id="article">'
        + parsearticle(information[0]["Inhalt"], information[0]["code"])
        + u"</div>\n"
    )
    disclaimer += (
        "\t<p class='category_date'> hzgf. am "
        + information[0]["Datum"]
        + u"</p>\n\t<br/>\n</div>\n<!-- end main division -->\n"
    )
    disclaimer += footer
    f = open("single/index.html", "w")
    try:
        f.write(disclaimer.encode("UTF-8"))
    except TypeError:
        print("Fehler beim UTF8")
        f.write(disclaimer)
    f.close()


def parsearticle(inhalt, code):
    # 	== == <h2> </h2> For headlines
    # 	=== ===  <h3> </h3>
    # 	<code> </code> Code
    #
    k = len(inhalt)
    t = 0
    counter = 0
    while True:
        vorher = inhalt
        if counter == 0:
            inhalt = inhalt.replace("===", "<h3>", 1)
            counter = 1

        else:
            inhalt = inhalt.replace("===", "</h3>", 1)
            counter = 0
        if vorher == inhalt:
            break

    counter = 0
    while True:
        vorher = inhalt
        if counter == 0:
            inhalt = inhalt.replace("==", "<h4>", 1)
            counter = 1

        else:
            inhalt = inhalt.replace("==", "</h4>", 1)
            counter = 0
        if vorher == inhalt:
            break

    counter = 0
    if len(code) != 0:
        while True:
            anfang = inhalt.find("<code>")
            ende = inhalt.find("</code>")
            if len(code) <= counter:
                break
            dataname = code[counter]
            if anfang != -1 and ende != 1:
                t = [
                    "/usr/bin/source-highlight",
                    "-i",
                    "code/" + dataname,
                    "-o",
                    "STDOUT",
                    "--failsafe",
                    "--out-format=html5",
                ]
                process = subprocess.check_output(t, shell=False).decode("utf-8")
                inhalt = inhalt.replace(inhalt[anfang : ende + 7], process, 1)
            else:
                break
            counter = counter + 1

    inhalt = inhalt.replace("#00DD00", "#00DD00")

    return inhalt


def createarticles(header, headerbar, left, footer):
    f = open("json/articles.json", "r")
    information = simplejson.load(f)
    f.close()
    for i in range(0, len(information)):

        disclaimer = (
            header
            + headerbar
            + left
            + "<div id='main'>\n\t<h2>"
            + information[i]["Titel"]
            + "</h2>\n"
        )
        disclaimer = (
            disclaimer
            + "\t<h3> aus der Kategorie "
            + information[i]["Hauptkategorie"]
            + " </h3>\n"
        )
        disclaimer = (
            disclaimer
            + '\t<div id="article">'
            + parsearticle(information[i]["Inhalt"], information[i]["code"])
            + "</div>\n"
        )
        disclaimer = (
            disclaimer
            + "\t<p class='category_date'> hzgf. am "
            + information[i]["Datum"]
            + "</p>\n\t<br/>\n</div>\n<!-- end main division -->\n"
        )
        disclaimer = disclaimer + footer
        f = open("single/" + str(information[i]["Hash"]) + ".html", "w")
        try:
            f.write(disclaimer.encode("UTF-8"))
        except TypeError:
            print("Fehler beim UTF8")
            f.write(disclaimer)

        f.close()


def createBio(header, headerbar, left, footer):
    f = open("template/bio.html", "r")
    uebersicht = header + headerbar + left + f.read()
    f.close()

    f = open("json/information.json", "r")
    information = simplejson.load(f)

    uebersicht += (
        '<img style="float: right" src="../img/RC3/RC3_orig_256_256.png" alt="Eat, Sleep, PWN, Repeat"/><h2>Basic Information:</h2>\n\t\t'
        + information["Vorname"]
        + " "
        + information["Name"]
        + "\n"
        "<br/>\n"
    )
    f.close()

    f = open("json/bio.json", "r")
    information = simplejson.load(f)
    for i in information:
        uebersicht += " <br/>\n<h2> " + i["Name"] + " </h2>\n \t\t\t<table>"
        for j in i["Data"]:
            uebersicht += "<tr>\n"
            uebersicht += '<td class="lefty"> ' + j["From"] + "</td>\n"
            uebersicht += '<td class="righty"> ' + j["What"] + "</td>\n"
            uebersicht += "</tr>\n"
            uebersicht += "<tr><td><br/></td></tr>\n"
        #            uebersicht += j["From"], j["What"]

        uebersicht += "</table>\n\n"

    uebersicht += "</div>\r\n" + footer
    f = open("single/bio.html", "w")
    try:
        f.write(uebersicht.encode("UTF-8"))
    except TypeError:
        print("Fehler beim UTF8")
        f.write(uebersicht)

    f.close()


def CreateHashForArtcile():
    f = open("json/articles.json", "r")
    j_obj = simplejson.load(f)
    f.close()
    m = hashlib.sha256()

    for i in range(0, len(j_obj)):
        if "Hash" in j_obj[i]:
            continue
        m.update(j_obj[i]["Inhalt"].encode("UTF-8"))
        j_obj[i]["Hash"] = m.hexdigest()
        print(m.hexdigest())
    f = open("json/articles.json", "w")
    simplejson.dump(j_obj, f, sort_keys=True, indent=4 * " ")
    f.close()


def main(argv=None):
    CreateHashForArtcile()
    f = open("json/up.json", "r")
    j_obj = simplejson.load(f)
    header = createheader(j_obj)
    headerbar = createheaderbar(j_obj)
    f.close()
    f = open("json/down.json", "r")
    footer = createfooter(simplejson.load(f))
    f.close()
    f = open("json/articles.json", "r")
    j_obj = simplejson.load(f)
    categories = getcategories(j_obj)
    left = createleft(j_obj, 7)
    createoverview(j_obj, categories, header, headerbar, left, footer)
    f.close()

    createdisclaimer(header, headerbar, left, footer)
    createmain(header, headerbar, left, footer)
    createarticles(header, headerbar, left, footer)
    createBio(header, headerbar, left, footer)


if __name__ == "__main__":
    main()

hzgf. am 7. Oktober 2011