aboutsummaryrefslogtreecommitdiff
path: root/barf
blob: c19ec94bcedd198132eccb1445716774e76a478a (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/sh

start_time=$(date +%s)
domain="https://btxx.org"
timestamp_file="timestamps.tsv"

# Check the operating system and set aliases
case $(uname -s) in
    OpenBSD)
        alias sed=gsed
        alias date=gdate
        alias rsync=openrsync
        ;;
    Darwin)
        alias sed=gsed
        alias date=gdate
        ;;
esac

set -eu
MARKDOWN=lowdown
IFS=$'\t'

# Precompute values used in multiple places
header_title=$(sed -n '/^# /{s/# //p; q}' index.md)
git_user_name=$(git config user.name)
first_commit_date=$(git log --pretty='format:%ai' . | cut -d ' ' -f1 | tail -1)
now=$(date +%FT%TZ)
now_rss=$(date -u +"%a, %d %b %Y %H:%M:%S %z")

# Update the timestamp for a markdown file
update_timestamp() {
    local filename=$1
    local timestamp=$(date -r "$filename" +%s)
    # Remove old timestamp and add new
    awk -v file="$filename" -v ts="$timestamp" '$1 != file {print $0}' "$timestamp_file" > "${timestamp_file}.tmp"
    echo "$filename $timestamp" >> "${timestamp_file}.tmp"
    mv "${timestamp_file}.tmp" "$timestamp_file"
}

# Check if the markdown file needs rebuilding
needs_rebuilding() {
    local filename=$1
    local last_mod=$(date -r "$filename" +%s)
    local last_build=$(awk -v file="$filename" '$1 == file {print $2}' "$timestamp_file")

    if [ "$last_mod" -gt "${last_build:-0}" ]; then
        return 0  # needs rebuilding
    else
        return 1  # does not need rebuilding
    fi
}

# Define write_page function
write_page() {
    local filename=$1
    local title=$2
    local category=$3  # Category determines if it's a post or a page

    if needs_rebuilding "$filename"; then
        local directory
        if [ "$category" = "posts" ]; then
            directory="build/posts/$(basename "$filename" .md)"
        else
            directory="build/$(basename "$filename" .md)"
        fi
        mkdir -p "$directory"
        local target="${directory}/index.html"
        local created=$(head -3 "$filename" | tail -1)

        {
            sed "s|{{TITLE}}|$title|" header.html
            $MARKDOWN "$filename"
            cat footer.html
        } > "$target"
        update_timestamp "$filename"
        
        echo "Rebuilt: $filename"
    fi
}

# Create tab separated file with filename, title
index_tsv() {
    for f in "$1"/*.md; do
        title=$(sed -n '/^# /{s/# //p; q}' "$f")
        printf '%s\t%s\n' "$f" "${title:="No Title"}"
    done
}

# Generate index.html
index_html() {
    sed "s/{{TITLE}}/$header_title/" header.html
    $MARKDOWN index.md
    echo "<div>"

    while read -r f title; do
        link="${f%.md}/"
        created=$(head -3 "$f" | tail -1)
        echo "<span>$created &middot; <a href=\"$link\">$title</a></span><br>"
    done < "$1" | sort -r

    echo "</div>"
    cat footer.html
}

# Generate atom.xml
atom_xml() {
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<feed xmlns=\"http://www.w3.org/2005/Atom\">
    <title>$header_title</title>
    <link href=\"$domain/atom.xml\" rel=\"self\" />
    <updated>$now</updated>
    <author>
        <name>$git_user_name</name>
    </author>
    <id>$domain,$first_commit_date:default-atom-feed</id>"

    while read -r f title; do
        content=$($MARKDOWN "$f" | sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g')
        post_link="${f%.md}"
        basic_date=$(head -3 "$f" | tail -1)
        published_date=$(date -d "$basic_date" -u +%Y-%m-%dT10:%M:%SZ)
        echo "    <entry>
        <title>$title</title>
        <content type=\"html\">$content</content>
        <link href=\"$domain/$post_link\"/>
        <id>$domain/$post_link</id>
        <updated>$published_date</updated>
        <published>$published_date</published>
    </entry>"
    done < "$1"
    echo '</feed>'
}

# Generate RSS
rss_xml() {
    uri=$(sed -rn '/rss.xml/ s/.*href="([^"]*)".*/\1/ p' header.html)
    first_commit_date=$(git log --pretty='format:%ai' . | cut -d ' ' -f1 | tail -1)

    cat <<EOF
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
    <channel>
        <title>$(sed -n '/^# /{s/# //p; q}' index.md)</title>
        <link>$domain/rss.xml</link>
        <description>Feed description here</description>
        <lastBuildDate>$(date -u +"%a, %d %b %Y %H:%M:%S %z")</lastBuildDate>
        <pubDate>$(date -u +"%a, %d %b %Y %H:%M:%S %z")</pubDate>
        <generator>Custom RSS Generator</generator>
        <ttl>1800</ttl>
EOF

    while read -r f title created; do
        content=$($MARKDOWN "$f" | sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g')
        post_link=$(echo "$f" | sed -E 's|posts/(.*).md|\1|')
        basic_date=$(echo $(head -3 "$f" | tail -1))
        published_date=$(date -d "$basic_date" -u +"%a, %d %b %Y %H:%M:%S %z")

        cat <<EOF
        <item>
            <title>$title</title>
            <description>$content</description>
            <link>$domain/$post_link</link>
            <guid isPermaLink="false">$domain/$post_link</guid>
            <pubDate>$published_date</pubDate>
        </item>
EOF
    done < "$1"

    echo '</channel>'
    echo '</rss>'
}

# Initialize timestamps for all Markdown files if not already done
initialize_timestamps() {
    if [ ! -f "$timestamp_file" ] || [ ! -s "$timestamp_file" ]; then
        echo "Initializing timestamps..."
        for f in index.md posts/*.md pages/*.md; do
            update_timestamp "$f"
        done
        echo "Timestamps initialized for all Markdown files."
    fi
}

# Main operations
mkdir -p build/posts build/pages
initialize_timestamps

index_tsv posts > build/posts.tsv
index_tsv pages > build/pages.tsv

while read -r f title; do
    write_page "$f" "$title" "posts"
done < build/posts.tsv

while read -r f title; do
    write_page "$f" "$title" "pages"
done < build/pages.tsv

if needs_rebuilding "index.md"; then
    index_html build/posts.tsv > build/index.html
    echo "Rebuilt index.html due to changes in index.md"
    update_timestamp "index.md"
fi

atom_xml build/posts.tsv > build/atom.xml
rss_xml build/posts.tsv > build/rss.xml

end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
echo "Total time: ${elapsed_time}s"