summaryrefslogtreecommitdiff
path: root/build.py
blob: 692eb4a5170f163f4b8a245f1bb50bfb43c2532b (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
"""Build html to be served at agenda.blaise.zone."""

import os
import pathlib

import datetime

__here__ = pathlib.Path(__file__).parent


# consume org file --------------------------------------------------------------------------------


global latest_scheduled
latest_scheduled = datetime.date.today()


class Event:
    def __init__(self, line):
        global latest_scheduled
        # timestamp (start time)
        year, month, day = (int(s) for s in line.split("<")[1][0:10].split("-"))
        self.timestamp = datetime.date(year, month, day)
        if self.timestamp > latest_scheduled:
            latest_scheduled = self.timestamp
        # end time
        if "--<" in line:
            year, month, day = (int(s) for s in line.split("--<")[1][0:10].split("-"))
            self.end = datetime.date(year, month, day)
            if self.end > latest_scheduled:
                latest_scheduled = self.end
        else:
            self.end = None
        # time of day
        tod = line[line.find("<") + 1 : line.find(">")][15:]
        # description
        self.description = " ".join(filter(None, [tod, line.split(">")[-1].strip()]))


org_fp = "/home/nginx/org/agenda.org"

events = []

with open(org_fp, "r") as org:
    for line in org:
        line = line.strip()
        if not line:
            continue
        if not line[0] == "*":
            continue
        if ":private:" in line:
            continue
        if ":info:" in line:
            continue
        events.append(Event(line))


# create html -------------------------------------------------------------------------------------


today = datetime.date.today()

if not os.path.isdir(__here__ / "public"):
    os.mkdir(__here__ / "public")

head = """<html>
<head>
    <meta charset="utf-8" name="viewport" content="width=80ch">
    <title>agenda.blaise.zone</title>
    <link rel="stylesheet" href="style.css">
</head>
"""

with open(__here__ / "public" / "index.html", "w") as html:
    # header
    html.write("<!DOCTYPE html>\n")
    html.write(head)
    html.write("<body>\n")
    html.write("<h1><a href='http://blaise.zone'>blaise</a>-agenda</h1>\n")
    html.write("<hr>")
    html.write("<p>")
    html.write("Welcome to my public agenda!<br>\n")
    html.write("All timestamps are in 'blaise-local-time'.<br>\n")
    html.write("Thanks for stopping by! —Blaise\n")
    html.write("</p>")
    # body
    monday = today - datetime.timedelta(days=today.weekday())
    while True:
        year, week, day = monday.isocalendar()
        id = f"{year}-w{week}"
        html.write(
            f'<h2 id="{id}"><a href=#{id} style=color:#f0c674>{year} W{week}</a></h2>'
        )
        for i in range(7):
            day = monday + datetime.timedelta(days=i)
            id = day.isoformat()
            html.write(
                f"<h3 id={id}><a href=#{id}>{day.isoformat()} {day.strftime('%A')}</a></h3>"
            )
            for event in events:
                if event.end:
                    if event.timestamp <= day and event.end >= day:
                        total = event.end - event.timestamp
                        elapsed = total - (event.end - day)
                        html.write(
                            f"(day {elapsed.days+1}/{total.days+1}) {event.description}<br>"
                        )
                else:
                    if event.timestamp == day:
                        html.write(f"{event.description}<br>")
        # increment
        monday += datetime.timedelta(days=7)
        if monday > latest_scheduled:
            break
    # footer
    html.write("<hr>")
    html.write("<p style=\"display:inline;\">"
    html.write(f"built {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} UTC")
    html.write("<a href=\"https://creativecommons.org/publicdomain/zero/1.0/\">CC0</a>: no copyright")
    html.write(" (<a href=\"http://git.blaise.zone/website/blog.git/\">source</a>)")
    html.write("</div>")
    html.write("</p>")
    html.write("</body>\n")
    html.write("</html>\n")