]> Git — Sourcephile - julm/rezine-rfcs.git/blob - scripts/lib.sh
scripts: add removeAtExit array
[julm/rezine-rfcs.git] / scripts / lib.sh
1 #!/usr/bin/env bash
2 # SPDX-FileCopyrightText: 2024 Julien Moutinho (adh14) <julm+rezine@autogeree.net>
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4 # shellcheck disable=SC2034
5 # shellcheck disable=SC2046
6 # shellcheck disable=SC2086
7 # shellcheck disable=SC2119
8 # shellcheck disable=SC2154
9 rfcOrga=Rézine
10 rfcRepo=rezine-rfcs
11 rfcList=rfcs
12 rfcDomain=rezine.org
13 # FIXME: host it on rezine.org?
14 rfcGitweb="https://git.sourcephile.fr/julm/$rfcRepo.git"
15
16 removeAtExit=()
17 exitHook () {
18 rm -f "${removeAtExit[@]}"
19 }
20 trap exitHook EXIT
21
22 rfcDirMetadata () {
23 rfcRoot=$(realpath --relative-to . "${0%/*}"/..)
24 rfcDir=$(realpath --relative-to "$rfcRoot" "$rfcDir")
25 rfcBranch=$(realpath --relative-to "$rfcRoot"/rfcs "$rfcDir")
26 rfcYear=$(git log -1 --pretty=%cd --date=format:%Y "$rfcDir/advocacy.md")
27 rfcDate=$(git log -1 --pretty=%cd --date=short "$rfcDir/advocacy.md")
28 }
29
30 rfcRevision () {
31 local advocacyLatestCommit criticismsLatestCommit
32 advocacyLatestCommit=$(git rev-list --max-count=1 HEAD -- "$rfcDir/advocacy.html")
33 criticismsLatestCommit=$(git rev-list --max-count=1 HEAD -- "$rfcDir/criticisms.html")
34 rfcTagAdvocacyOld="$(git describe --tags "$advocacyLatestCommit")" || true
35 rfcTagCriticismsOld="$(git describe --tags "$criticismsLatestCommit")" || true
36 if test "${rfcTagAdvocacyOld#"${rfcBranch}v"}" != "$rfcTagAdvocacyOld"
37 then IFS=psvc read -r _num rfcRevAdvocacy _ <<<"$rfcTagAdvocacyOld"
38 else rfcRevAdvocacy=0; rfcTagAdvocacyOld=init
39 fi
40 if test "${rfcTagCriticismsOld#"${rfcBranch}v"}" != "$rfcTagCriticismsOld"
41 then IFS=psvc read -r _num _rfcRevAdvocacy rfcRevCriticisms _ <<<"$rfcTagCriticismsOld"
42 else rfcRevCriticisms=0; rfcTagCriticismsOld=init
43 fi
44 rfcRevision=v"$rfcRevAdvocacy"c"$rfcRevCriticisms"
45 rfcTag="$rfcBranch$rfcRevision"
46 }
47
48 rfcSource () {
49 if test ! "${rfcNoAdvocacy:+set}"; then
50 cat - "$rfcDir"/advocacy.md <<EOF
51 ::: information
52 Ce document est publié sous licence
53 [\${SPDX-License-Identifier}](https://spdx.org/licenses/\${SPDX-License-Identifier}.html).
54
55 Pour le citer :
56
57 > \${rfcOrga} <[RFC\${rfcTag}](mailto:\${rfcList}+\${rfcTag}@\${rfcDomain})>
58 > — « *\${title}* » — \${rfcDate} — série *Requests for Criticisms*
59 > — \${RFC-Category} « \` \${rfcBranch}\` »,
60 > plaidoirie « \` \${rfcRevisionAdvocacy}\` »,
61 > critiques « \` \${rfcRevisionCriticisms}\` »
62 > — \${for(RFC-Compasses)}boussole \${it}\${sep} & \${endfor}
63 > — <<\${rfcGitweb}/blob_plain/\${rfcTag}:/rfcs/\${rfcBranch}/index.html>>.
64
65 Vous _devriez_ consulter en priorité sa dernière version
66 qui est disponible en ligne à l'adresse :
67 <<\${rfcGitweb}/blob_plain/\${rfcBranch}:/rfcs/\${rfcBranch}/index.html>>
68 :::
69
70 EOF
71 printf '\n'
72 fi
73 if test ! "${rfcNoCriticisms:+set}"; then
74 cat "$rfcDir"/criticisms.md
75 fi
76 cat <<EOF
77
78 # Références
79
80 ::: {#refs}
81 :::
82
83 EOF
84 }
85
86 rfcMetadata () {
87 printf ' --metadata %s' \
88 rfcBranch="$rfcBranch" \
89 rfcDate="$rfcDate" \
90 rfcDir="$rfcDir" \
91 rfcDomain="$rfcDomain" \
92 rfcGitweb="$rfcGitweb" \
93 rfcList="$rfcList" \
94 rfcOrga="$rfcOrga" \
95 rfcRepo="$rfcRepo" \
96 rfcRevision="$rfcRevision" \
97 rfcRevisionAdvocacy="v$rfcRevAdvocacy" \
98 rfcRevisionCriticisms="c$rfcRevCriticisms" \
99 rfcTag="$rfcTag" \
100 rfcYear="$rfcYear"
101 # --metadata-file provides defaults,
102 # its metadata do not override the document's metadata.
103 printf ' --metadata-file %s' "$metaFile"
104 cat >"$metaFile" <<EOF
105 ---
106 title: "[RFC${rfcBranch}]"
107 ...
108 EOF
109 }
110
111 rfcTemplate () {
112 local sourceFile metaFile
113 sourceFile=$(mktemp --suffix ".md")
114 metaFile=$(mktemp --suffix ".md")
115 removeAtExit+=("$sourceFile" "$metaFile")
116 rfcSource >"$sourceFile"
117 pandoc \
118 --wrap none \
119 $(rfcMetadata) \
120 --template "$sourceFile" \
121 "$sourceFile" | "$@"
122 }
123
124 rfcPlain () {
125 rfcTemplate \
126 pandoc --from markdown+emoji --to plain \
127 --citeproc \
128 --csl "$rfcRoot"/styles/rfc.csl \
129 $(printf " --bibliography %s" "$rfcRoot"/refs/*.json) \
130 --lua-filter "$rfcRoot"/styles/rfc.lua \
131 --variable lang=fr-FR \
132 "$@"
133 }
134
135 rfcMarkdown () {
136 rfcTemplate \
137 pandoc --from markdown+emoji --to markdown+emoji \
138 --table-of-contents \
139 --toc-depth 6 \
140 --number-sections \
141 --citeproc \
142 --csl "$rfcRoot"/styles/rfc.csl \
143 $(printf " --bibliography %s" "$rfcRoot"/refs/*.json) \
144 --lua-filter "$rfcRoot"/styles/rfc.lua \
145 --variable lang=fr-FR \
146 "$@"
147 }
148
149 rfcHTML () {
150 rfcTemplate \
151 pandoc --from markdown+emoji --to html5 \
152 --standalone \
153 --embed-resources \
154 --include-in-header "$rfcRoot"/styles/rfc.header.html \
155 --table-of-contents \
156 --toc-depth 6 \
157 --number-sections \
158 --citeproc \
159 --csl "$rfcRoot"/styles/rfc.csl \
160 $(printf " --bibliography %s" "$rfcRoot"/refs/*.json) \
161 --lua-filter "$rfcRoot"/styles/rfc.lua \
162 --variable lang=fr-FR \
163 "$@"
164 }
165
166 rfcPDF () {
167 rfcTemplate \
168 pandoc --from markdown+emoji --to pdf \
169 --pdf-engine lualatex \
170 --include-in-header "$rfcRoot"/styles/rfc.header.tex \
171 --standalone \
172 --embed-resources \
173 --table-of-contents \
174 --toc-depth 6 \
175 --number-sections \
176 --citeproc \
177 --csl "$rfcRoot"/styles/rfc.csl \
178 $(printf " --bibliography %s" "$rfcRoot"/refs/*.json) \
179 --lua-filter "$rfcRoot"/styles/rfc.lua \
180 --variable colorlinks=true \
181 --variable lang=fr-FR \
182 --variable links-as-notes=true \
183 "$@"
184 }
185
186 rfcTag () {
187 git reset
188 git diff --exit-code -- "$rfcDir" :'!*.html'
189
190 : "Check if \$rfcRevAdvocacy must be increased."
191 rfcNoCriticisms="set" \
192 rfcHTML -o "$rfcDir"/advocacy.html
193 git add "$rfcDir"/advocacy.html
194 rfcRevAdvocacyIncrement=0
195 if ! git diff --cached --quiet "$rfcDir"/advocacy.html; then
196 rfcRevAdvocacyIncrement=1
197 # Note that $rfcRevCriticisms is not reset:
198 # criticisms have their own independant revisions
199 # because, in practice, criticisms to an old $rfcRevAdvocacy
200 # can be received and added after a new $rfcRevAdvocacy has been tagged.
201 fi
202
203 : "Check if \$rfcRevCriticisms must be increased."
204 rfcRevAdvocacy="1" \
205 rfcNoAdvocacy="set" \
206 rfcHTML -o "$rfcDir"/criticisms.html
207 git add "$rfcDir"/criticisms.html
208 rfcRevCriticismsIncrement=0
209 if ! git diff --cached --quiet "$rfcDir"/criticisms.html; then
210 rfcRevCriticismsIncrement=1
211 fi
212
213 if test "$rfcRevAdvocacyIncrement" -gt 0 ||
214 test "$rfcRevCriticismsIncrement" -gt 0
215 then
216 rfcRevAdvocacy=$((rfcRevAdvocacy + rfcRevAdvocacyIncrement))
217 rfcRevCriticisms=$((rfcRevCriticisms + rfcRevCriticismsIncrement))
218 rfcRevision=v"$rfcRevAdvocacy"c"$rfcRevCriticisms"
219 rfcTag="$rfcBranch$rfcRevision"
220 rfcNoCriticisms="set" \
221 rfcHTML -o "$rfcDir"/advocacy.html
222 rfcRevAdvocacy="1" \
223 rfcNoAdvocacy="set" \
224 rfcHTML -o "$rfcDir"/criticisms.html
225 rfcHTML -o "$rfcDir"/index.html
226 #rfcPDF -o "$rfcDir/${rfcRepo}-${rfcBranch}".pdf
227 git add "$rfcDir"/{advocacy,criticisms,index}.html
228 #git add "$rfcDir/${rfcRepo}-${rfcBranch}".pdf
229 git diff --cached --quiet ||
230 git commit -m "$rfcBranch: v$rfcRevAdvocacy c$rfcRevCriticisms"
231 git tag --force --sign --message "${rfcOrga} RFC${rfcTag}" "$rfcTag"
232 fi
233 }