summaryrefslogtreecommitdiff
path: root/build.py
blob: 5e9a0c3555b228910d376821c4e8af1b56c6b712 (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
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 listPages():
    return [
        (lambda path: 
            (lambda content: 
                (lambda timestamp: 
                    (lambda name: {
                        "source_file" : path,
                        "source_content" : content,
                        "html" : markdown.markdown(content),
                        "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 = "\n".join(
            [
                formatEntry(summary_templ, page)
                    .replace(
                            "%content%", 
                            "\n".join(page["html"].split("\n")[:summary_max])
                        ) 
                
                for page in pages
            ]
    )

    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("index.html")

    with open(os.path.join(dist, "index.html"), "w") as index:
        index.write(
                    index_templ.replace("%entries%", 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
                    ]
            )

    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()