aboutsummaryrefslogtreecommitdiff
path: root/v2/src/bin/conf
blob: 9fb4664704851d47510ac111592b8d7ca77ae517 (about) (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
#!/bin/sh
set -eu


usage() {
	cat <<-'EOF'
		Usage:
		  conf [FILENAME]
		  conf -h
	EOF
}

help() {
	cat <<-'EOF'

		Options:
		  -h, --help    show this message

		  FILENAME      the name of the input file, also to be used as
		                URL.


		Separate the content from the "frontmatter", and emit the
		selected one, given the FILENAME.  If FILENAME is not given,
		generate the global config.


		Examples:

		  Get the "frontmatter" of src/f.conf:

		    $ conf src/f.md > src/f.conf
	EOF
}


for flag in "$@"; do
	case "$flag" in
		--)
			break
			;;
		--help)
			usage
			help
			exit
			;;
		*)
			;;
	esac
done

while getopts 'h' flag; do
	case "$flag" in
		h)
			usage
			help
			exit
			;;
		*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

. src/lib.sh


FILENAME="${1:-}"
if [ -z "$FILENAME" ]; then
	printf 'export style_url="%s"\n'  "$(url-for 'style.css')"
	printf 'export pubkey_url="%s"\n' "$(url-for 'public.asc.txt')"
	printf 'export pubkey_id="%s"\n'  "$(
		LANG=C.UTF-8 gpg --list-key "$EMAIL" |
			awk 'NR==2 { print substr($1, length($1) - 15) }' |
			shesc
	)"

	for f in "$CONTENT_PREFIX"/img/*.svg; do
		name="$(basename "$f" .svg | sed 's|-|_|g')"
		printf 'export icon_%s_url="%s"\n' "$name" "$(url-for "img/$(basename "$f")")"
	done
	for l in $(langs); do
		for f in "$CONTENT_PREFIX"/img/*.svg; do
			name="$(basename "$f" .svg | sed 's|-|_|g')"
			printf 'export icon_%s_alt="%s"\n' "$name" "$(shesc < "$CONTENT_PREFIX/img/alt/$(basename "$f" .svg).$l.txt" )"
		done > src/lib/generated."$l".conf
	done
	exit
fi


lang="$(lang-for "$FILENAME")"
export lang

cat src/lib/base.conf src/lib/generated.conf src/lib/generated."$lang".conf |
	tee "$FILENAME".tmp
DELIMITER=0
while read -r line; do
	if [ "$line" = '---' ]; then
		DELIMITER=$((DELIMITER + 1))
		continue
	fi
	if [ "$DELIMITER" = 2 ]; then
		break
	fi
	if [ -z "$line" ]; then
		continue
	fi

	KEY="$(  printf '%s' "$line" | cut -d: -f1)"
	VALUE="$(printf '%s' "$line" | cut -d: -f2- | sed 's|^ ||' | shesc)"
	printf 'export %s="%s"\n' "$KEY" "$VALUE"
done < "$FILENAME" | tee -a "$FILENAME".tmp
# shellcheck source=/dev/null
. "$FILENAME".tmp
rm -f "$FILENAME".tmp


lang="$(lang-for "$FILENAME")"
export lang
printf 'export lang="%s"\n' "$lang"

cat src/lib/base."${lang:?}".conf
# shellcheck source=/dev/null
. src/lib/base."$lang".conf
if [ -z "${title:-}" ]; then
	title="${site_name:?}"
	printf 'export title="%s"\n' "$(printf '%s' "$title" | shesc)"
fi


collection="$(collection-for "$FILENAME" ||:)"
if [ -n "$collection" ]; then
	export collection
	printf 'export collection="%s"\n' "$collection"

	coll_path="$(coll2path "$lang" "$collection")"
	feed_title="$(eval "echo \"\$feed_${collection}_title\"" | htmlesc)"

	collection_head_html="$(printf '    <link rel="alternate"  type="application/atom+xml" href="%s" title="%s" />' \
		"$(url-for "$lang/$coll_path")" \
		"$(htmlesc "$feed_title")"
	)"
	printf 'export collection_head_html="%s"\n' "$(shesc "$collection_head_html")"
fi

if [ -z "${layout:-}" ]; then
	if [ -z "$collection" ]; then
		layout=page
	else
		layout=post

		custom_layout="$(echo "$LAYOUTS" | grep . |
			awk -v coll="$collection" '$1 == coll { print $2 }'
		)"
		if [ -n "$custom_layout" ]; then
			layout="$custom_layout"
		fi
	fi
	export layout
	printf 'export layout="%s"\n' "$layout"
fi


if [ -r "$FILENAME".i18n ]; then
	translations_head="$(awk -F: -v lang="$lang" '$1 != lang{
		printf "    <link rel=\"alternate\"  type=\"text/html\"            href=\"%s\" hreflang=\"%s\" />\n",
			$3, $1
	}' "$FILENAME".i18n)"
	translations_body="$(cat <<-EOF
		        <ul class="translation-list">
		$(awk -F: '{
			printf "          <li><a href=\"%s\">%s</a></li>\n",
				$3, $1
		}' "$FILENAME".i18n)
		        </ul>
	EOF
	)"
	printf 'export translations_head_html="%s"\n' "$(shesc "$translations_head")"
	printf 'export translations_body_html="%s"\n' "$(shesc "$translations_body")"
fi

if [ -n "${date:-}" ]; then
	date_iso="$(date -ud "${date:?}" -Is)"
	printf 'export date_iso="%s"\n' "$date_iso"

	formatted_date="$(LANG="$lang" date -ud "${date:?}" +"${date_fmt:?}")"
	export formatted_date
	printf 'export formatted_date_html="%s"\n' "$(htmlesc "$formatted_date" | shesc)"
	printf 'export date_html="%s"\n' "$(envsubst < src/lib/date."$lang".html | shesc)"

	echo "$FILENAME" > "$(dirname "$FILENAME")/$date_iso.sortdata"
fi

if [ -n "${update:-}" ]; then
	update_iso="$(date -ud "${update:?}" -Is)"
	printf 'export update_iso="%s"\n' "$update_iso"

	formatted_update="$(LANG="$lang" date -ud "${update:?}" +"${date_fmt:?}")"
	export formatted_update
	printf 'export update_html="%s"\n' "$(envsubst < src/lib/update."$lang".html | shesc)"

	printf 'export update_xml="    <updated>%s</updated>"\n' "$update_iso"
fi


url="$(url-for "$FILENAME")"
url_part="$(printf '%s' "$url" | sed 's|^/||')"
title_uri="$(uri "$title")"

printf 'export title_html="%s"\n' "$(printf '%s' "$title" | htmlesc | shesc)"
printf 'export filename="%s"\n' "$FILENAME"
printf 'export url_part="%s"\n' "$url_part"
printf 'export url="%s"\n' "$url"
printf 'export url_absolute="%s"\n' "$(absolute "$url")"
printf 'export mailto_uri="%s%s"\n' "${mailto_uri_prefix:?}" "$title_uri"
printf 'export discussions_url="%s%s"\n' "${discussions_url_prefix:?}" "$title_uri"
printf 'export sourcecode_url="%s%s"\n' "${sourcecode_url_prefix:?}" "$FILENAME"


if [ "${layout:-}" = 'post' ]; then
	export mailto_uri="$mailto_uri_prefix$title_uri"
	export discussions_url="$discussions_url_prefix$title_uri"
	export sourcecode_url="$sourcecode_url_prefix$FILENAME"
	printf 'export comment_html="%s"\n' "$(envsubst < src/lib/comment."$lang".html | shesc)"
fi