-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·285 lines (244 loc) · 6.67 KB
/
build.sh
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/bash
# constants
SOURCE='src'
TARGET='docs'
MD_CONVERT='pandoc'
TITLE="˚⌇˚"
HOST="mxngls.github.io"
URL="https://""$HOST"
# metadata format:
# title
# subtitle
# created_at
# updated_at
# redefine IFS to split on tabs
IFS=$'\t'
# tabs as field separator
index_tsv() {
while read -r f; do
if [[ "$f" =~ index.html ]]; then continue; fi
read -r title subtitle content < <(
awk -v RS= '
BEGIN {
title = "";
subtitle = "";
}
# Parse title
NR == 1 {
gsub(/#[[:space:]]+/,"");
title=$0;
}
# Parse subtitle
NR == 2 {
gsub(/\n/,"\\n");
subtitle=$0;
}
# Parse content
NR > 2 {
gsub(/\n/,"\\n");
content = content $0 "\\n";
}
END {
print title "\t" subtitle "\t" content;
}
' "$f"
)
read -r created updated < <(git log \
--follow \
--format='format:%ad' \
--date='format:%Y/%m/%d' \
"$f" 2> /dev/null |
awk '
NR==1 { created=$0 }
END{updated=$0; print created "\t" updated;}
')
printf '%s\t%s\t%s\t%s\t%s\t%s\n' \
"$f" \
"${title:="No title"}" \
"${subtitle:="No subtitle"}" \
"${updated:="draft"}" \
"${created:="draft"}" \
"${content:="draft"}"
done < <(find "$SOURCE" -type f -name '*.md' -not -path "$SOURCE/drafts/*")
}
index_html() {
content="$(< "$SOURCE"/index.html)"
while read -r f title subtitle created updated; do
d="$(dirname "$f")"
ref="${f//$SOURCE/$TARGET}"
ref="${ref#*/}"
ref="${ref/%.md/.html}"
f="$(basename "$f")"
if [[ "$d" =~ "notes" ]]; then
notes+=$(printf '
<tr>
<td>%s</td>
<td style="padding-left: 10px">
<a href=%s>%s</a>
</td>
</tr>\n' "$created" "$ref" "$title")
else
posts+=$(printf '
<tr>
<td>%s</td>
<td style="padding-left: 10px">
<a href=%s>%s</a>
</td>
</tr>\n' "$created" "$ref" "$title")
fi
done < "$1"
# shfmt-ignore
content+="$(printf '
<div>
<h2>Weblog</h2>
<div style="font-size: 97%%; color: #696969; margin-bottom: 10px; border-bottom: 1.5px solid; border-color: #dedede;">
<p style="margin-top: 0.5em;">
Occasionally shared writings that feel hard to categorize <a href="./atom.xml"><em>(feed)</em></a>:
</p>
</div>
<table>
<tbody>
%s
</tbody>
</table>
<h2>Notes</h2>
<div style="font-size: 98%%; color: #696969; margin-bottom: 10px; border-bottom: 1.5px solid; border-color: #dedede;">
<p>
Things I worked on in the past or that I am still tinkering with (mostly of technical nature):
</p>
</div>
<table stlye="width: 100%%;">
<tbody>
%s
</tbody>
</table>
</div>
</main>' "$posts" "$notes")"
# Read input as arguments to avoid escaping newlines
awk '
BEGIN {
title=ARGV[1];
content=ARGV[2];
ARGV[1]="";
ARGV[2]="";
}
/{{TITLE}}/ { gsub(/{{TITLE}}/,title); }
/{{CONTENT}}/ { gsub(/{{CONTENT}}/, content); }
{ print $0; }' \
"$TITLE" \
"$content" \
'index_template.html'
}
create_dirs() {
for d in "$SOURCE"/*; do
if [[ -d "$d" ]] && [[ ! "$d" =~ "drafts" ]]; then
local dir="${d/$SOURCE/$TARGET}"
mkdir "$dir" &> /dev/null
fi
done
}
create_page() {
if [[ "$#" -ne 6 ]]; then
echo "Invalid number of arguments: $#. Expected six."
return 1
fi
tmp_target="${1/%.md/.html}"
target="${tmp_target//$SOURCE/$TARGET}"
title="$2"
subtitle="$3"
date_created="<div id=\"date-created\" style=\"margin: 2rem 0;\"><a href="/">${4}</a></div>"
date_updated="<div id=\"date-updated\" style=\"color: #696969; margin: 2rem 0;\"><small>Last Updated on ${5}</small></div>"
content="$($MD_CONVERT -f gfm -t html "$1")"
header="$date_created"
main="$content"
footer="$date_updated"
# provide multiline strings as arguments instead of using -v var=""
awk '
BEGIN {
title = ARGV[1];
header = ARGV[2];
main = ARGV[3];
footer = ARGV[4];
ARGV[1] = "";
ARGV[2] = "";
ARGV[3] = "";
ARGV[4] = "";
}
/{{HEADER}}/ {
r = "{{HEADER}}"
s = index($0, r)
$0 = substr($0, 1, s-1) header substr($0, s + length(r))
}
/{{CONTENT}}/ {
r = "{{CONTENT}}"
s = index($0, r)
$0 = substr($0, 1, s-1) main substr($0, s + length(r))
}
/{{FOOTER}}/ {
r = "{{FOOTER}}"
s = index($0, r)
$0 = substr($0, 1, s-1) footer substr($0, s + length(r))
}
/{{TITLE}}/ { sub(/{{TITLE}}/, title) }
{ print $0 }' \
"$title" \
"$header" \
"$main" \
"$footer" \
'template.html' > "$target"
}
atom_xml() {
# https://stackoverflow.com/a/5189296/13490131
since="$(git log --max-parents=0 HEAD --format='%aI')"
updated="$(awk -v FS="$IFS" 'NR == 1 { print $5; exit;}' "$1")"
uri="$URL""/atom.xml"
cat << EOF
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Max's Space</title>
<link href="$uri" rel="self" />
<updated>$updated</updated>
<author>
<name>Maximilian Hoenig</name>
</author>
<id>tag:www.$HOST,${since:0:10}:F2B3E23B-ECB6-4EE7-8197-D3C6C2594701</id>
EOF
while read -r f title subtitle created updated content; do
if [[ "$created" = "draft" ]]; then continue; fi
t="${f//$SOURCE/$TARGET}"
post_updated="${created:0:10}"
content="$(awk '{gsub(/\\n/, "\n"); print $0;}' <<< "$subtitle""\n""$content" |
$MD_CONVERT -f gfm -t html |
awk '
{
gsub(/&/, "\\&", $0);
gsub(/</, "\\<", $0);
gsub(/>/, "\\>", $0);
gsub(/"/, "\\"", $0);
gsub(/'\''/, "\\'", $0);
print $0;
}'
)"
cat << EOF
<entry>
<title>$title</title>
<content type="html">$content</content>
<link href="${f##"$SOURCE"/}"/>
<id>tag:www.$HOST,$post_updated:$t</id>
<published>$created</published>
<updated>$updated</updated>
</entry>
EOF
done < "$1"
echo '</feed>'
}
mkdir -p "$TARGET"
index_tsv | sort -r -t $'\t' -k4,4 > "$TARGET"/index.tsv # Use tab as seperator
index_html "$TARGET"/index.tsv > "$TARGET"/index.html
create_dirs
while read -r f title subtitle created updated content; do
create_page "$f" "$title" "$subtitle" "$created" "$updated" "$content" || exit 1
done < "$TARGET"/index.tsv
atom_xml "$TARGET"/index.tsv > "$TARGET"/atom.xml
cp style.css "$TARGET"/style.css
cp -R assets "$TARGET"