-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.sh
executable file
·129 lines (112 loc) · 2.82 KB
/
index.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
#!/bin/sh
set -e
env="${1:-./.env}"
# shellcheck source=./.env.template
. "$env"
tmp=./index.tmp
exclude_files="${exclude_files}"
meta_ext="${meta_ext:-.meta}"
site_root="${site_root}"
html_dir="${html_dir:-html}"
#post_dir="${post_dir:-posts}"
log() {
printf 'shite: [%s] %s\n' "$1" "$2" >&2
}
info() {
log "$(printf '\033[32minfo\033[0m')" "$1"
}
warn() {
log "$(printf '\033[33mwarn\033[0m')" "$1"
}
error() {
log "$(printf '\033[31merror\033[0m')" "$1"
}
die() {
error "$1"
printf 'exiting...\n' >&2
exit 1
}
gen_header() {
printf '<header>\n'
printf '<nav>\n'
if [ -f "${site_root}/${html_dir}/header.html" ]; then
cat "${site_root}/${html_dir}/header.html"
else
warn "header_content file 'header.html' does not exist."
fi
printf '</nav>\n'
printf '</header>\n'
}
gen_index_head() {
cat "${html_dir}/index_head.html"
}
gen_index_tail() {
printf '<footer>\n<br><hr>\n'
if [ -n "$onion" ]; then
printf '<p>[ '
printf '<a href="%s/posts/">view on hidden service</a>' "${onion}"
printf ' ]</p>\n'
fi
cat "${site_root}/${html_dir}/footer.html"
printf '</footer>\n'
}
gen_index() {
find "${site_root}/${post_dir}/" -name '*.md' | while read -r post; do
post_html="${post%.*}.html"
post_meta="${post%.*}${meta_ext}"
post_url="/posts/$(basename "$post_html")"
excluded=0
for ex in $exclude_files; do
if [ "$(basename "$post")" = "$ex" ]; then
excluded=1
fi
done
if [ "$excluded" -eq 1 ]; then
warn "${post} excluded"
continue
fi
# parse metadata if .meta file exists
if [ -f "$post_meta" ]; then
# read the 'key: value' .meta file
while IFS=': ' read -r key val; do
[ "${key##\#*}" ] || continue
# export each key as a variable; '$post_<key>'
export "post_${key}=${val}" 2>/dev/null || \
warn "'${key}' is not a valid meta tag name"
done < "$post_meta"
else
warn "no ${meta_ext} - skipping metadata parsing"
fi
echo "${post_date}|${post_title}|${post_url}" >> "$tmp"
unset post_date
unset post_title
unset post_image
unset post_description
unset post_url
done
printf '<!DOCTYPE html>\n'
printf '<html lang="en">\n'
printf '<head>\n'
printf "<title>zakaria's web log</title>\n"
cat "${html_dir}/head.html"
printf "<meta property=\"og:title\" content=\"zakaria's web log\">\n"
printf '</head>\n'
printf '<body>\n'
gen_header
gen_index_head
printf '<ul>'
sort -r "$tmp" | while IFS='|' read -r post_date post_title post_url; do
printf '<li>%s <a href="%s">%s</a></li>\n' "$post_date" "$post_url" "$post_title"
done
printf '</ul>'
gen_index_tail
printf '</body>\n'
printf '</html>\n'
}
if [ -f "./index.tmp" ]; then
log 'old index.tmp file found. remove?'
rm -i "./index.tmp"
fi
gen_index > "${site_root}/${post_dir}/index.html"
# remove index tempfile
rm -i index.tmp