aboutsummaryrefslogtreecommitdiff
path: root/build.sh
blob: 3cd53bf78c1874bffc3d55726ff1fae7f65f7660 (plain) (blame)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/bash

# hitler.rip
# script to build my website using pandoc
# © 2025 hitler.rip <git@hitler.rip>
# licensed under AGPLv3-or-later; see licenses/code.md for more information

filterfile=$( realpath ./con/links.lua )
convert () {
	pandoc "$1" -o "$2" \
		-f gfm -t html5 \
		--lua-filter="$filterfile"
}

echo "[build] starting to build webpage..."

rm -rf ./tmp/
rm -rf ./web
mkdir -p ./con/
mkdir -p ./src/articles/
mkdir -p ./tmp/articles/
mkdir -p ./web/articles/
mkdir -p ./web/licenses/
mkdir -p ./dir/

touch ./con/header.html
touch ./con/footer.html
touch ./con/style.css
touch ./con/rss-header.xml
touch ./con/rss-footer.xml
touch ./con/contact.md
touch ./src/index.md
touch ./src/articles/index.md

echo "[build] writing index..."
cd ./src/

for i in *.md; do

	echo "[build] converting: $i"

	filename=$( echo "$i" | rev | cut -c4- | rev )
	convert "$i" "../tmp/$filename.html"

	if [ "$filename" == "index" ]; then 

		convert "../con/contact.md" "../tmp/con-contact-tmp-file.html"

		echo "[build] skipping index file for later..."

	else

		mkdir -p ../web/"$filename"/

		cat ../con/header.html \
			../tmp/"$filename".html \
			../con/footer.html \
			> ../web/"$filename"/index.html

		cp "$i" ../web/"$filename"/index.md

	fi

done

echo "[build] writing articles..."
cd ./articles/

declare -A articlearr
declare -a workingarr
declare -a thebestarr

for i in *.md; do

	echo "[build] converting: $i"

	filename=$( echo "$i" | rev | cut -c4- | rev )
	convert "$i" "../../tmp/articles/$filename.html"

	if [ "$filename" == "index" ]; then
		echo "[build] skipping index file for later..."
	else

		mkdir -p ../../web/articles/"$filename"/

		cat ../../con/header.html \
			../../tmp/articles/"$filename".html \
			../../con/footer.html \
			> ../../web/articles/"$filename"/index.html

		cp "$i" ../../web/articles/"$filename"/index.md

		export WEBMETA_DATE=""

		if [ -e "$filename".meta ]; then
			echo "[build] running script: $filename.meta"
			source ./"$filename".meta
			# `.meta` is a simple shell script that uses `export` to
			# set some variables, notably:
			# WEBMETA_TITLE (title string)
			# WEBMETA_DATE (date string YYYY-MM-DD)
			# WEBMETA_AUTH (author string)
			# WEBMETA_SUMMARY (summary string)
			# please note that you have to actually check if this data is correct
			# and you should not just import random `.meta` files as they can arbitrarily
			# execute code on your system and are directly injected into the HTML.
			#
			# NOTE:
			# I do not expect to have multiple articles written on the same day
			# as I am a lazy fuck. The generator below does not expect multiple
			# articles to have the exact same date.
			# If I were you, I would maybe add hours and minutes to the timestamps
			# and maybe change this script accordingly.
		fi

		if [ "$WEBMETA_DATE" == "" ]; then
			export WEBMETA_DATE="2001-09-11*"
		fi

		articlearr["$filename"]="$WEBMETA_DATE"

	fi

done

echo "[build] sorting articles..."

workingarr=($( sort -nr < <(printf '%s\n' "${articlearr[@]}") ))

for date in "${workingarr[@]}"; do
	for key in "${!articlearr[@]}"; do
		if [ "${articlearr[$key]}" == "$date" ]; then
			thebestarr+=("$key")
		fi
	done
done

for i in "${thebestarr[@]}"; do

	export WEBMETA_TITLE=""
	export WEBMETA_DATE=""
	export WEBMETA_AUTH=""
	export WEBMETA_SUMMARY=""
	if [ -e "$i".meta ]; then
		echo "[build] re-running script: $i.meta"
		source ./"$i".meta
		# see above
	fi

	if [ "$WEBMETA_DATE" == "" ]; then
		export WEBMETA_DATE="2001-09-11*"
	fi
	if [ "$WEBMETA_AUTH" == "" ]; then
		export WEBMETA_AUTH="Anonymous"
	fi
	if [ "$WEBMETA_SUMMARY" == "" ]; then
		export WEBMETA_SUMMARY="redacted"
	fi
	if [ "$WEBMETA_TITLE" == "" ]; then
		export WEBMETA_TITLE="$i"
	fi

	if [ "$i" == "${thebestarr[0]}" ]; then
		tee ../../tmp/articles/gen-first-article-tmp.html > /dev/null << EOF
			<h2>Latest Post:</h2>
			<article>
				<a href="/articles/$i"><h3>$WEBMETA_TITLE</h3></a>
				<h6><code>$WEBMETA_DATE</code> by $WEBMETA_AUTH</h6>
				<p>$WEBMETA_SUMMARY</p>
			</article>
			<a href="/articles/"><h5>See more Articles →</h5></a>
EOF
		tee ../../tmp/articles/gen-first-article-tmp.md > /dev/null << EOF

## Latest Post:

## [$WEBMETA_TITLE](/articles/$i)
###### \`$WEBMETA_DATE\` by $WEBMETA_AUTH
$WEBMETA_SUMMARY
EOF
	fi

	touch ../../tmp/articles/gen-articles-tmp-list.html
	tee -a ../../tmp/articles/gen-articles-tmp-list.html > /dev/null << EOF
		<article>
			<a href="$i"><h2>$WEBMETA_TITLE</h2></a>
			<h6><code>$WEBMETA_DATE</code> by $WEBMETA_AUTH</h6>
			<p>$WEBMETA_SUMMARY</p>
		</article>
EOF

	touch ../../tmp/articles/gen-articles-tmp-list.md
	tee -a ../../tmp/articles/gen-articles-tmp-list.md > /dev/null << EOF

## [$WEBMETA_TITLE]($i)
###### \`$WEBMETA_DATE\` by $WEBMETA_AUTH
$WEBMETA_SUMMARY
EOF

	touch ../../tmp/articles/gen-articles-tmp-rss.xml
	tee -a ../../tmp/articles/gen-articles-tmp-rss.xml > /dev/null <<EOF
		<item>
			<title>$WEBMETA_TITLE</title>
			<pubDate>$(date -d "${WEBMETA_DATE:0:10}" +"%a, %d %b %Y %H:%M:%S +0000")</pubDate>
			<author>$WEBMETA_AUTH</author>
			<description>$WEBMETA_SUMMARY</description>
			<link>/$i</link>
			<guid>/$i</guid>
		</item>
EOF

done

echo "[build] generating feed..."
cd ../..

cat ./con/header.html \
	./tmp/index.html \
	./tmp/articles/gen-first-article-tmp.html \
	./tmp/con-contact-tmp-file.html \
	./con/footer.html \
	> ./web/index.html

cat ./src/index.md \
	./tmp/articles/gen-first-article-tmp.md \
	./con/contact.md \
	> ./web/index.md

cat ./con/header.html \
	./tmp/articles/index.html \
	./tmp/articles/gen-articles-tmp-list.html \
	./con/footer.html \
	> ./web/articles/index.html

cat ./src/articles/index.md \
	./tmp/articles/gen-articles-tmp-list.md \
	> ./web/articles/index.md

cat ./con/rss-header.xml \
	./tmp/articles/gen-articles-tmp-rss.xml \
	./con/rss-footer.xml \
	> ./web/articles/feed.xml

echo "[build] copying static files..."
cp ./con/style.css ./web/style.css
cp ./con/icon.png ./web/icon.png
cp ./licenses/* ./web/licenses/
cp -r ./dir/* ./web/

echo "[build] removing temporary build files..."
rm -rf ./tmp

echo "[build] done!"