-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerate_catalog.sh
executable file
·203 lines (189 loc) · 5.08 KB
/
generate_catalog.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
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
#!/bin/sh
#-
# Copyright © 2020, 2023
# mirabilos <[email protected]>
#
# Provided that these terms and disclaimer and all copyright notices
# are retained or reproduced in an accompanying document, permission
# is granted to deal in this work without restriction, including un‐
# limited rights to use, publicly perform, distribute, sell, modify,
# merge, give away, or sublicence.
#
# This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
# the utmost extent permitted by applicable law, neither express nor
# implied; without malicious intent or gross negligence. In no event
# may a licensor, author or contributor be held liable for indirect,
# direct, other damage, loss, or other issues arising in any way out
# of dealing in the work, even if advised of the possibility of such
# damage or existence of a defect, except proven that it results out
# of said person’s immediate fault when using the work as intended.
LC_ALL=C; LANGUAGE=C
export LC_ALL
unset LANGUAGE
die() {
echo >&2 "E: $*"
exit 1
}
T=$(mktemp -d /tmp/gencat.XXXXXXXXXX) || \
die 'could not generate temporary directory'
cleanup() {
cd /
rm -rf "$T"
return $1
}
trap 'cleanup $?' EXIT
cd "$(dirname "$0")" || die 'could not cd'
set -e
rm -rf owner tags
mkdir owner tags
rm -f rezepte/index.md
htmlesc() {
tr -d '[--]' | tr '\n' '' | { cat; echo; } | sed \
-e 's/[ ]*$//g' \
-e 's/[&]/\&/g' \
-e 's/[]/\ /g' \
-e 's/["]/\"/g'
}
# for now (more escaping will be added as needed)
mdlink() {
echo "[$1]$(echo "($2)" | sed \
-e 's/%/%25/g' \
-e 's/ /%20/g' \
)"
}
exec 4>"$T/rezepte"
for fn in rezepte/*; do
test -f "$fn" || continue
set --
exec <"$fn"
fn=${fn##*/}
bn=${fn%.*}
name=
IFS= read -r line || die "not in correct format (too short): $fn"
case $line in
(---|'<!--') ;;
(*) die "not in correct format (1): $fn" ;;
esac
IFS= read -r line || die "not in correct format (too short): $fn"
case $line in
("owner: "?*) ;;
(*) die "not in correct format (2): $fn" ;;
esac
line=${line#owner: }
case $line in
# allow space, removed below, and: .
(*[-!--/:-@[-\`{-]*)
die "control or otherwise weird character in owner '$line' in $fn"
;;
esac
owner=$(echo "X$line" | sed -e 's/^X//' -e 's/ //g')
test -d owner/"$owner" || {
mkdir owner/"$owner"
echo "X$line" | sed 's/^X//' >owner/"$owner"/.name
}
exec 5>>"$T/owner-$owner"
IFS= read -r line || die "not in correct format (too short): $fn"
case $line in
("name: "?*)
name=${line#name: }
IFS= read -r line || die "not in correct format (tags?): $fn"
;;
esac
test x"$line" = x"tags:" || die "not in correct format (3): $fn"
while :; do
IFS= read -r line || die "not in correct format (aborted): $fn"
case $line in
(---|'-->') break ;;
('- '*) ;;
(*) die "not in correct format (tag $line): $fn" ;;
esac
line=${line#- }
case $line in
(*[-/:-@[-\`{-]*)
die "control or otherwise weird character in tag '$line' in $fn"
;;
esac
test -d tags/"$line" || mkdir tags/"$line"
set -- "$@" "$line"
done
sed \
-e 's!\.\./!&&!g' \
>owner/"$owner"/"$fn"
line=$(sed 1q <owner/"$owner"/"$fn") || line=
test -n "$name" || name=$line
test -n "$name" || name=$bn
line="* $(mdlink "$name" "$fn")"
sep=' '
for pic in pics/"$bn".*; do
test -e "$pic" || continue
case $pic in
(*.lbl)
continue
;;
(pics/"$bn".*.*)
pictag=${pic#pics/"$bn".}
pictag="$bn (${pictag%.*})"
;;
(*)
pictag=$bn
;;
esac
pictag=$(echo "X$pictag" | htmlesc); pictag=${pictag#X}
if test -s "${pic%.*}.lbl"; then
piclbl=$(expand <"${pic%.*}.lbl" | htmlesc)
picalt="$pictag: $piclbl"
pictit="$pictag: $piclbl"
picalt=$(echo "X$pictit" | sed 's/[&][#]10;/ /g')
picalt=${picalt#X}
else
picalt=$pictag
pictit=$pictag
fi
case $pic in
(pics/*[!A-Za-z0-9_.~-]*)
pictag=../../pics/$(echo "$pic" | perl -0777 -pe '
s!^pics/!!;
chop;
s/([^A-Za-z0-9_.~-])/sprintf "%%%02X", ord($1)/eg;
')
;;
(*)
pictag=../../$pic
;;
esac
# funnily enough, this is the correct way to do it in GFM…
pictag="<img src=\"$pictag\" width=\"30%\""
pictag="$pictag alt=\"$picalt\" title=\"$pictit\" />"
line=$line$sep$pictag
sep=' '
done
echo "$line" | sed 's!\(<img src="\)\.\./\(\.\./pics/\)!\1\2!g' >&4
echo "$line" >&5
for tag in "$@"; do
echo "$line" >>"$T/tags-$tag"
cp owner/"$owner"/"$fn" tags/"$tag"/
done
done
exec </dev/null 4>/dev/null 5>/dev/null
doindex() {
echo "$2"
echo =====================
echo
sort -f <"$1" | tr '' '\n'
}
for line in owner/*; do
ownername=$(cat "$line"/.name)
line=${line#*/}
echo "* $(mdlink "$ownername" "$line/index.md")"
doindex "$T/owner-$line" "Rezepte von $ownername" >owner/"$line"/index.md
done >"$T/owner"
doindex "$T/owner" "Rezepte nach Eigner" >owner/index.md
rm owner/*/.name
for line in tags/*; do
line=${line#*/}
echo "* $(mdlink "$line" "$line/index.md")"
doindex "$T/tags-$line" "Rezepte für $line" >tags/"$line"/index.md
done >"$T/tags"
doindex "$T/tags" "Rezepte nach Kategorie" >tags/index.md
doindex "$T/rezepte" "Alle Rezepte" >rezepte/index.md
echo >&2 "I: all done"