summaryrefslogtreecommitdiff
path: root/src/conf
blob: d132a344be1806f7a1b266553c0cce25812d4f84 (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
#!/bin/sh
set -euo pipefail


usage() {
	echo 'Usage: conf BASECONF [GLOBALCONF FILENAME.adoc]'
}


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"
}

BASECONF="${1:-}"
GLOBALCONF="${2:-}"
FILENAME="${3:-}"
eval "$(assert-arg -- "$BASECONF" 'BASECONF')"
. "$(realpath -- "$BASECONF")"


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 [ -z "$GLOBALCONF" ]; then
	now_iso="$(dateiso "@${SOURCE_DATE_EPOCH:-$(date '+%s')}" | 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 <<-EOF
		export now_iso="$now_iso"
		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 -- "$BASECONF" 'BASECONF')"
eval "$(assert-arg -- "$FILENAME" 'FILENAME')"
. "$(realpath -- "$GLOBALCONF")"

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

{
	if head -n1 -- "$FILENAME" | grep -q '^////$'; then
		awk 'sep > 1 {exit}; /^\/{4}$/ { sep++; next } { print }' \
			"$FILENAME"
	fi
} > "$FILENAME".embedded-config
. "$(realpath -- "$FILENAME".embedded-config)"

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

base_url() {
	# src/sub/dirs/file.txt -> ../..
	# src/file.txt -> .
	printf '%s/\n' "$(dirname -- "$UNPREFIXED")" |
		sed \
			-e 's|[^/]*/|../|g' \
			-e 's|/$||'         \
			-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 -- "$BASECONF")"
fi
root_dir="${root_dir%/}"

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

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

cat "$BASECONF" "$GLOBALCONF"

date_iso=
date_formatted=
updatedat_iso=
updatedat_formatted=
if is_article; then
	date_iso="$(dateiso "$(last3dirnames)" | shesc)"
	date_formatted="$(datefmt "$date_iso" | shesc)"
	if [ -n "${updatedat:-}" ]; then
		updatedat_iso="$(dateiso "$updatedat" | shesc)"
		updatedat_formatted="$(datefmt "$updatedat_iso" | shesc)"
	fi
fi

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

if [ -z "${css_url:-}" ]; then
	css_url="$BASE_URL/style.css"
fi
if [ -z "${atom_url:-}" ]; then
	atom_url="$BASE_URL/atom.xml"
fi
if [ -z "${atomicon_url:-}" ]; then
	needs "${img_dir:-}" 'Define either $atomicon_url or $img_dir'
	atomicon_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

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)"

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

css_url="$(                printf '%s\n' "$css_url"                 | shesc)"
atom_url="$(               printf '%s\n' "$atom_url"                | shesc)"
atomicon_url="$(           printf '%s\n' "$atomicon_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)"

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 atom_url="$atom_url"
	export atomicon_url="$atomicon_url"
	export favicon_url="$favicon_url"
	export logo_url_prefix="$logo_url_prefix"
	export envelopeicon_url_prefix="$envelopeicon_url_prefix"
	export source_path="$source_path"
	export url="$url"
	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 date_iso="$date_iso"
	export date_formatted="$date_formatted"
	export updatedat_iso="$updatedat_iso"
	export updatedat_formatted="$updatedat_formatted"
	export comment_url="$comment_url"
	export discussions_url="$discussions_url"
EOF

cat "$FILENAME".embedded-config