summaryrefslogtreecommitdiff
path: root/build.py
blob: 45d89823a9e5b7c75e4432b7fa8e47836702bf6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import markdown
import os
import time
import shutil

from const import *

def getTemplateHTML(name):
    html = ""
    with open(os.path.join(templates, name), "r") as file:
        html = file.read();
    return html

def lowerHeadings(html):
    # This is a dumb lol
    return html.replace("<h6>", "<p>")\
                .replace("</h6>", "</p>")\
                .replace("<h5>", "<h6>")\
                .replace("</h5>", "</h6>")\
                .replace("<h4>", "<h5>")\
                .replace("</h4>", "</h5>")\
                .replace("<h3>", "<h4>")\
                .replace("</h3>", "</h4>")\
                .replace("<h2>", "<h3>")\
                .replace("</h2>", "</h3>")\
                .replace("<h1>", "<h2>")\
                .replace("</h1>", "</h2>")\

def listPages():
    return [
        (lambda path: 
            (lambda content: 
                (lambda timestamp: 
                    (lambda name: {
                        "source_file" : path,
                        "source_content" : content,
                        "html" : markdown.markdown("\n".join(content.split("\n...\n"))),
                        "summary" : lowerHeadings(markdown.markdown(content.split("\n...\n")[0])),
                        "timestamp" : timestamp,
                        "date": time.strftime(date_format, time.localtime(timestamp)),
                        "name" : name,
                        "url" : f"entries/{name}.html"
                    })(".".join(p.split(".")[:-1]))
                )(os.stat(path).st_ctime)
            )(open(path, "r").read())
        )(os.path.join(source, p)) for p in os.listdir(source)
    ]


def formatEntry(content, page):
        return content.replace("%date%", page["date"])\
            .replace("%name%", page["name"])\
            .replace("%time%", str(page["timestamp"]))\
            .replace("%source%", site_index + page["source_file"])\
            .replace("%url%", site_index + page["url"])

def make():

    try:
        os.makedirs(os.path.join(dist, "entries"))
    except:
        print("Already have content")
    shutil.rmtree(os.path.join(dist, "src"))
    shutil.rmtree(os.path.join(dist, "images"))
    shutil.copytree(source, os.path.join(dist, "src"))
    shutil.copytree(images, os.path.join(dist, "images"))
    
    pages = listPages()

    summary_templ = getTemplateHTML("summary.html")

    summariesHTML = getTemplateHTML("about.html") + "\n<hr>\n"+ "\n<hr>\n".join(
                    [
                        formatEntry(summary_templ, page)
                            .replace(
                                "%content%", 
                                page["summary"] + (f"<a href={page['url']}>read more...</a>" if len(page["source_content"].split("\n...\n")) > 1 else "")
                            ) 
                
                        for page in pages
                    ][: : -1]
    )

    entry_templ = getTemplateHTML("page.html")
    
    for page in pages:
        with open(os.path.join(dist, page["url"]), "w") as entry:
            entry.write(
                        formatEntry(
                                entry_templ,
                                page
                            )
                        .replace("%content%", page["html"])
                    )

        

    index_templ = getTemplateHTML("page.html")

    with open(os.path.join(dist, "index.html"), "w") as index:
        index.write(
                    index_templ.replace("%content%", summariesHTML)
                )


    item_templ = getTemplateHTML("item.xml")
    rss_templ = getTemplateHTML("rss.xml")
    itemsXML = "\n".join(
                [
                    formatEntry(item_templ, page).replace("%content%", page["html"])
                    for page in pages
                    ][: : -1]
            )

    with open(os.path.join(dist, "rss.xml"), "w") as index:
        index.write(
                    rss_templ.replace("%items%", itemsXML)
                )

    print(f"built in {len(pages)} pages")
make()