summaryrefslogtreecommitdiff
path: root/src/conf
blob: b7aeb4589226aeba5141de3d963da6760bd3d1b8 (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
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
286
287
288
289
290
291
#!/bin/sh
set -euo pipefail


usage() {
	echo <<-'EOF'
		Usage:
		  conf GLOBALCONF FILENAME.adoc
		  conf -G BASECONF > GLOBALCONF
	EOF
}


GENERATE=false
while getopts 'G' flag; do
	case "$flag" in
		(G)
			GENERATE=true
			;;
		(*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))


needs() {
	if [ -z "${1:-}" ]; then
		printf '%s\n' "$2" >&2
		exit 1
	fi
}

DATE_I='+%Y-%m-%d'
DATE_Is="${DATE_I}T%H:%M:%S%:z"
datex() {
	# date(1), plus non-POSIX -d option
	DATESTR="$1"
	shift
	date -d "$DATESTR" "$@"
}

dateiso() {
	datex "$1" -u "$DATE_Is"
}

dateepoch() {
	datex "$1" '+%s'
}


unset header_links categories custombody

unset url_pre site_name feed_title feed_url feed_alternate_url
unset list_addr discussions_url_prefix
unset email author sourcecode_url sourcecode_url_prefix
unset logo_alt

unset root_dir img_dir
unset updatedat

unset css_url feed_url favicon_url
unset feedicon_url logo_url_prefix envelopeicon_url_prefix \
	lockicon_url_prefix

unset publickey_url
unset sort



CONF="${1:-}"
FILENAME="${2:-}"
eval "$(assert-arg -- "$CONF" 'CONF')"
. ./"$CONF"


needs "${url_pre:-}"            'Missing required $url_pre (e.g. https://example.com)'
needs "${site_name:-}"          'Missing required $site_name'
needs "${feed_title:-}"         'Missing required $feed_title'
needs "${feed_url:-}"           'Missing required $feed_url'
needs "${feed_alternate_url:-}" 'Missing required $feed_alternate_url'

needs "${list_addr:-}"              'Missing required $list_addr'
needs "${discussions_url_prefix:-}" 'Missing required $discussions_url_prefix'

needs "${email:-}"                 'Missing required $email'
needs "${author:-}"                'Missing required $author'
needs "${sourcecode_url:-}"        'Missing required $sourcecode_url'
needs "${sourcecode_url_prefix:-}" 'Missing required $sourcecode_url_prefix'


if [ "$GENERATE" = true ]; then
	now_iso="$(dateiso "@${SOURCE_DATE_EPOCH:-$(date '+%s')}" | shesc)"
	now_epoch="$(dateepoch "$now_iso" | shesc)"
	feed_title_html="$(printf '%s\n' "$feed_title" | htmlesc | shesc)"
	site_name_html="$( printf '%s\n' "$site_name"  | htmlesc | shesc)"
	if [ -z "${logo_alt:-}" ]; then
		logo_alt='The website logo'
	fi
	logo_alt="$(printf '%s\n' "$logo_alt" | htmlesc | shesc)"

	cat -- "$CONF"
	cat <<-EOF
		export now_iso="$now_iso"
		export now_epoch="$now_epoch"
		export feed_title_html="$feed_title_html"
		export site_name_html="$site_name_html"
		export feed_url_absolute="$url_pre/$feed_url"
		export feed_alternate_url_absolute="$url_pre/$feed_alternate_url"
		export logo_alt="$logo_alt"
	EOF
	exit
fi

eval "$(assert-arg -- "$FILENAME" 'FILENAME')"

trap 'rm -f -- "$FILENAME".embedded-config' EXIT

awk '
$0 == "" {exit}
/^= /    {next}
{ printf "export %s=\"%s\"\n",
	substr($1, 2, length($1) - 2),
	substr($0, length($1) + 2) }
' "$FILENAME" > "$FILENAME".embedded-config
. ./"$FILENAME".embedded-config

is_article() {
	printf '%s\n' "$FILENAME" | grep -qE \
		'^[-a-zA-Z0-9/]*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[-A-Za-z0-9]+\.adoc$'
		#^src/a/b/c/     /1970    /01      /01/     /some-file-N4me.adoc
}

base_url() {
	# src/sub/dirs/file.txt -> ../..
	# src/file.txt -> .
	printf '%s/\n' "$(dirname -- "$UNPREFIXED")" |
		sed 's|^\./$|.|' |
		sed \
			-e 's|[^/]*/|../|g' \
			-e 's|/$||'
}

last3dirnames() {
	dirname -- "$UNPREFIXED" | tr '/' '\n' | tail -n3 | paste -sd-
}

datefmt() {
	LANG=en_GB.UTF-8 datex "$1" -u '+%B %-d, %Y'
}


if [ -z "${root_dir:-}" ]; then
	root_dir="$(dirname -- "$CONF")"
fi
root_dir="${root_dir%/}"

if [ -z "${img_dir:-}" ]; then
	img_dir=img
fi
img_dir="${img_dir%/}"

UNPREFIXED="${FILENAME#$root_dir/}"

cat -- "$CONF"

url="$(printf '%s\n' "${UNPREFIXED%.adoc}.html" | shesc)"


lastmod_epoch="$(stat --printf='%Y' "$FILENAME" | shesc)"

collurl=
date_iso=
date_epoch=
date_formatted=
updatedat_iso=
updatedat_epoch=
updatedat_formatted=
if is_article; then
	lastdirnames="$(last3dirnames)"
	date_iso="$(dateiso "$lastdirnames"      | shesc)"
	date_epoch="$(dateepoch "$date_iso"      | shesc)"
	date_formatted="$(datefmt "$date_iso"    | shesc)"
	lastmod_epoch="$date_epoch"
	if [ -n "${updatedat:-}" ]; then
		updatedat_iso="$(dateiso "$updatedat" | shesc)"
		updatedat_epoch="$(dateepoch "$updatedat_iso"   | shesc)"
		updatedat_formatted="$(datefmt "$updatedat_iso" | shesc)"
		lastmod_epoch="$updatedat_epoch"
	fi
	file="$(basename "$url")"
	collurl="$(printf '%s\n' "$lastdirnames" | sed 's|-|/|g' | shesc)/$file"
fi

lastmod_iso="$(dateiso "@$lastmod_epoch"    | shesc)"
lastmod_formatted="$(datefmt "$lastmod_iso" | shesc)"


LEN="$( printf '%s..' "$root_dir" | sed -e 's|//|/|g' -e 's|/$||' | wc -c)"
lang="$(printf '%s' "$FILENAME" | cut -c "$LEN"- | cut -d/ -f1 | shesc)"


BASE_URL="$(base_url)"
TITLE_RAW="$(cat -- "$FILENAME" | grep '^= .*' | head -n1 | cut -d' ' -f2- ||
	echo 'MISSING')"
TITLEFULL_RAW="$TITLE_RAW | $site_name"

if [ -z "${css_url:-}" ]; then
	css_url="$BASE_URL/style.css"
fi
if [ -z "${feed_url:-}" ]; then
	feed_url="$BASE_URL/feed.xml"
fi
if [ -z "${feedicon_url:-}" ]; then
	needs "${img_dir:-}" 'Define either $feedicon_url or $img_dir'
	feedicon_url="$BASE_URL/$img_dir/atom.svg"
fi
if [ -z "${favicon_url:-}" ]; then
	needs "${img_dir:-}" 'Define either $favicon_url or $img_dir'
	favicon_url="$BASE_URL/$img_dir/favicon.svg"
fi
if [ -z "${logo_url_prefix:-}" ]; then
	needs "${img_dir:-}" 'Define either $logo_url_prefix or $img_dir'
	logo_url_prefix="$BASE_URL/$img_dir/logo"
fi
if [ -z "${envelopeicon_url_prefix:-}" ]; then
	needs "${img_dir:-}" 'Define either $envelopeicon_url_prefix or $img_dir'
	envelopeicon_url_prefix="$BASE_URL/$img_dir/envelope"
fi
if [ -z "${lockicon_url_prefix:-}" ]; then
	needs "${img_dir:-}" 'Define either $lockicon_url_prefix or $img_dir'
	lockicon_url_prefix="$BASE_URL/$img_dir/lock"
fi

title_uri="$(printf '%s' "$TITLE_RAW" | uri)"
comment_url="$(printf 'mailto:%s?Subject=Re%%3A%%20%s\n' "$list_addr" "$title_uri" | shesc)"
discussions_url="$(printf '%s%s\n' "$discussions_url_prefix" "$title_uri" | shesc)"

css_url="$(                printf '%s\n' "$css_url"                 | shesc)"
feed_url="$(               printf '%s\n' "$feed_url"                | shesc)"
feedicon_url="$(           printf '%s\n' "$feedicon_url"            | shesc)"
favicon_url="$(            printf '%s\n' "$favicon_url"             | shesc)"
logo_url_prefix="$(        printf '%s\n' "$logo_url_prefix"         | shesc)"
envelopeicon_url_prefix="$(printf '%s\n' "$envelopeicon_url_prefix" | shesc)"
lockicon_url_prefix="$(    printf '%s\n' "$lockicon_url_prefix"     | shesc)"

source_path="$(printf '%s\n' "$FILENAME" | shesc)"
base_url_prefix="$(printf '%s\n' "$BASE_URL" | shesc)"
title="$(         printf '%s\n' "$TITLE_RAW"     |           shesc)"
title_html="$(    printf '%s\n' "$TITLE_RAW"     | htmlesc | shesc)"
titlefull="$(     printf '%s\n' "$TITLEFULL_RAW" |           shesc)"
titlefull_html="$(printf '%s\n' "$TITLEFULL_RAW" | htmlesc | shesc)"
cat <<-EOF
	export css_url="$css_url"
	export feed_url="$feed_url"
	export feedicon_url="$feedicon_url"
	export favicon_url="$favicon_url"
	export logo_url_prefix="$logo_url_prefix"
	export envelopeicon_url_prefix="$envelopeicon_url_prefix"
	export lockicon_url_prefix="$lockicon_url_prefix"
	export source_path="$source_path"
	export publickey_url="$base_url_prefix/${publickey_url:-}"
	export url="$url"
	export collurl="$collurl"
	export url_absolute="$url_pre/$url"
	export base_url_prefix="$base_url_prefix"
	export title="$title"
	export title_html="$title_html"
	export titlefull="$titlefull"
	export titlefull_html="$titlefull_html"
	export lastmod_iso="$lastmod_iso"
	export lastmod_epoch="$lastmod_epoch"
	export lastmod_formatted="$lastmod_formatted"
	export date_iso="$date_iso"
	export date_epoch="$date_epoch"
	export date_formatted="$date_formatted"
	export updatedat_iso="$updatedat_iso"
	export updatedat_epoch="$updatedat_epoch"
	export updatedat_formatted="$updatedat_formatted"
	export comment_url="$comment_url"
	export discussions_url="$discussions_url"
	export lang="$lang"
EOF

cat "$FILENAME".embedded-config

if [ -z "${sort:-}" ]; then
	echo 'export sort=0'
fi