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
|
#!/bin/sh
set -eu
MARKDOWN=pandoc
IFS=' '
# Create tab separated file with filename, title, creation date, last update
index_tsv() {
for f in "$1"/*.md
do
created=$(git log --pretty='format:%aI' "$f" 2> /dev/null | tail -1)
updated=$(git log --pretty='format:%aI' "$f" 2> /dev/null | head -1)
title=$(sed -n '/^# /{s/# //p; q;}' "$f")
printf '%s\t%s\t%s\t%s\n' "$f" "${title:="No Title"}" "${created}" "${updated}"
done
}
index_html() {
# Print header
title=$(sed -n '/^# /{s/# //p; q;}' index.md)
sed -e 's/{{TITLE}}//g' header.html
# Intro text
$MARKDOWN index.md
# Posts
while read -r f title created updated; do
link=$(echo "$f" | sed -E 's|.*/(.*).md|\1.html|')
created=$(echo "$created" | sed -E 's/T.*//')
# Reverse date from YYYY-MM-DD to DD-MM-YY
created=$(echo "$created" | awk -F'-' '{printf("%02d-%02d-%02d\n",$3,$2,$1)}')
echo "$created — <a href=\"$link\">$title</a><br>"
done < "$1"
# Print footer after post list
year=$(date +"%Y")
sed "s/{{YEAR}}/$year/" footer.html
}
atom_xml() {
uri=$(sed -rn '/atom.xml/ s/.*href="([^"]*)".*/\1/ p' header.html)
host=$(echo "$uri" | sed -r 's|.*//([^/]+).*|\1|')
first_commit_date=$(git log --pretty='format:%ai' . | cut -d ' ' -f1 | tail -1)
cat <<EOF
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<icon>https://justine.smithies.me.uk/public/favicon-32x32.png</icon>
<title>$(grep -o '<title>.*</title>' header.html | sed 's/\(<title>\|{{TITLE}}<\/title>\)//g')</title>
<link href="$uri" rel="self" />
<updated>$(date -Iseconds)</updated>
<author>
<name>$(git config user.name)</name>
</author>
<id>tag:$host,$first_commit_date:default-atom-feed</id>
EOF
while read -r f title created updated; do
day=$(echo "$created" | sed 's/T.*//')
content=$($MARKDOWN "$f" | sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g')
cat <<EOF
<entry>
<title>$title</title>
<content type="html">$content</content>
<link href="$(echo "$f" | sed -E 's|posts/(.*).md|\1.html|')"/>
<id>tag:$host,$day:$f</id>
<published>$created</published>
<updated>$updated</updated>
</entry>
EOF
done < "$1"
echo '</feed>'
}
write_page() {
filename=$1
target=$(echo "$filename" | sed -r 's|\w+/(.*).md|build/\1.html|')
# year=$(date +"%Y")
created=$(echo "$3" | sed 's/T.*//')
# Reverse date from YYYY-MM-DD to DD-MM-YY
created=$(echo "$created" | awk -F'-' '{printf("%02d-%02d-%02d\n",$3,$2,$1)}')
updated=$(echo "$4" | sed 's/T.*//')
# Reverse date from YYYY-MM-DD to DD-MM-YY
updated=$(echo "$updated" | awk -F'-' '{printf("%02d-%02d-%02d\n",$3,$2,$1)}')
dates_text="Published on ${created}"
if [[ "$created" == "00-00-00" ]]; then
dates_text=""
elif [ "$created" != "$updated" ]; then
dates_text="$dates_text ( Updated ${updated} )"
fi
title=$2
$MARKDOWN "$filename" | \
sed -e '1,/h1>/{/h1>/a\
<small><b>'$dates_text'</b></small>' -e '}' | \
cat header.html - | \
sed "s/{{TITLE}}/ - $title/" \
> "$target" && sed "s/{{YEAR}}/$year/" footer.html >> "$target"
}
rm -fr build && mkdir build
# Blog posts
index_tsv posts | sort -rt " " -k 3 > build/posts.tsv
index_html build/posts.tsv > build/index.html
atom_xml build/posts.tsv > build/atom.xml
while read -r f title created updated; do
write_page "$f" "$title" "$created" "$updated"
done < build/posts.tsv
# Pages
index_tsv pages > build/pages.tsv
while read -r f title created updated; do
write_page "$f" "$title" "$created" "$updated"
done < build/pages.tsv
# Create public directory if it doesn't exist and sync with build/public
mkdir -p public/
rsync -r public/ build/public
|