]> Git — Sourcephile - julm/rezine-rfcs.git/blob - scripts/lib.sh
p1: consacre un fichier spécifique pour l'historique
[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 -rf "${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 - Statut de cette révision : « \${RFC-Phase} »
70 :::
71
72 EOF
73 printf '\n'
74 fi
75 if test ! "${rfcNoHistory:+set}"; then
76 cat "$rfcDir"/history.md
77 printf '\n'
78 fi
79 if test ! "${rfcNoCriticisms:+set}"; then
80 cat "$rfcDir"/criticisms.md
81 fi
82 cat <<EOF
83
84 # Références
85
86 ::: {#refs}
87 :::
88
89 EOF
90 }
91
92 rfcMetadata () {
93 printf ' --metadata %s' \
94 rfcBranch="$rfcBranch" \
95 rfcDate="$rfcDate" \
96 rfcDir="$rfcDir" \
97 rfcDomain="$rfcDomain" \
98 rfcGitweb="$rfcGitweb" \
99 rfcList="$rfcList" \
100 rfcOrga="$rfcOrga" \
101 rfcRepo="$rfcRepo" \
102 rfcRevision="$rfcRevision" \
103 rfcRevisionAdvocacy="v$rfcRevAdvocacy" \
104 rfcRevisionCriticisms="c$rfcRevCriticisms" \
105 rfcTag="$rfcTag" \
106 rfcYear="$rfcYear"
107 # --metadata-file provides defaults,
108 # its metadata do not override the document's metadata.
109 printf ' --metadata-file %s' "$metaFile"
110 cat >"$metaFile" <<EOF
111 ---
112 title: "[RFC${rfcBranch}]"
113 ...
114 EOF
115 }
116
117 rfcTemplate () {
118 local sourceFile metaFile
119 sourceFile=$(mktemp --suffix ".md")
120 metaFile=$(mktemp --suffix ".md")
121 removeAtExit+=("$sourceFile" "$metaFile")
122 rfcSource >"$sourceFile"
123 pandoc \
124 --wrap none \
125 $(rfcMetadata) \
126 --template "$sourceFile" \
127 "$sourceFile" | "$@"
128 }
129
130 rfcPlain () {
131 rfcTemplate \
132 pandoc --from markdown+emoji --to plain \
133 --citeproc \
134 --csl "$rfcRoot"/styles/rfc.csl \
135 $(printf " --bibliography %s" "$rfcRoot"/refs/*.json) \
136 --lua-filter "$rfcRoot"/styles/rfc.lua \
137 --variable lang=fr-FR \
138 "$@"
139 }
140
141 rfcMarkdown () {
142 rfcTemplate \
143 pandoc --from markdown+emoji --to markdown+emoji \
144 --table-of-contents \
145 --toc-depth 6 \
146 --number-sections \
147 --citeproc \
148 --csl "$rfcRoot"/styles/rfc.csl \
149 $(printf " --bibliography %s" "$rfcRoot"/refs/*.json) \
150 --lua-filter "$rfcRoot"/styles/rfc.lua \
151 --variable lang=fr-FR \
152 "$@"
153 }
154
155 rfcHTML () {
156 rfcTemplate \
157 pandoc --from markdown+emoji --to html5 \
158 --standalone \
159 --embed-resources \
160 --include-in-header "$rfcRoot"/styles/rfc.header.html \
161 --table-of-contents \
162 --toc-depth 6 \
163 --number-sections \
164 --citeproc \
165 --csl "$rfcRoot"/styles/rfc.csl \
166 $(printf " --bibliography %s" "$rfcRoot"/refs/*.json) \
167 --lua-filter "$rfcRoot"/styles/rfc.lua \
168 --variable lang=fr-FR \
169 "$@"
170 }
171
172 rfcPDF () {
173 rfcTemplate \
174 pandoc --from markdown+emoji --to pdf \
175 --pdf-engine lualatex \
176 --include-in-header "$rfcRoot"/styles/rfc.header.tex \
177 --standalone \
178 --embed-resources \
179 --table-of-contents \
180 --toc-depth 6 \
181 --number-sections \
182 --citeproc \
183 --csl "$rfcRoot"/styles/rfc.csl \
184 $(printf " --bibliography %s" "$rfcRoot"/refs/*.json) \
185 --lua-filter "$rfcRoot"/styles/rfc.lua \
186 --variable colorlinks=true \
187 --variable lang=fr-FR \
188 --variable links-as-notes=true \
189 "$@"
190 }
191
192 rfcTag () {
193 git reset
194 git diff --exit-code -- "$rfcDir" :'!*.html'
195
196 : "Check if \$rfcRevAdvocacy must be increased."
197 rfcNoCriticisms="set" \
198 rfcNoHistory="set" \
199 rfcHTML -o "$rfcDir"/advocacy.html
200 git add "$rfcDir"/advocacy.html
201 rfcRevAdvocacyIncrement=0
202 if ! git diff --cached --quiet "$rfcDir"/advocacy.html; then
203 rfcRevAdvocacyIncrement=1
204 # Note that $rfcRevCriticisms is not reset:
205 # criticisms have their own independant revisions
206 # because, in practice, criticisms to an old $rfcRevAdvocacy
207 # can be received and added after a new $rfcRevAdvocacy has been tagged.
208 fi
209
210 : "Check if \$rfcRevCriticisms must be increased."
211 rfcRevAdvocacy="1" \
212 rfcNoAdvocacy="set" \
213 rfcNoHistory="set" \
214 rfcHTML -o "$rfcDir"/criticisms.html
215 git add "$rfcDir"/criticisms.html
216 rfcRevCriticismsIncrement=0
217 if ! git diff --cached --quiet "$rfcDir"/criticisms.html; then
218 rfcRevCriticismsIncrement=1
219 fi
220
221 if test "$rfcRevAdvocacyIncrement" -gt 0 ||
222 test "$rfcRevCriticismsIncrement" -gt 0
223 then
224 rfcRevAdvocacy=$((rfcRevAdvocacy + rfcRevAdvocacyIncrement))
225 rfcRevCriticisms=$((rfcRevCriticisms + rfcRevCriticismsIncrement))
226 rfcRevision=v"$rfcRevAdvocacy"c"$rfcRevCriticisms"
227 rfcTag="$rfcBranch$rfcRevision"
228 rfcNoCriticisms="set" \
229 rfcNoHistory="set" \
230 rfcHTML -o "$rfcDir"/advocacy.html
231 rfcRevAdvocacy="1" \
232 rfcNoAdvocacy="set" \
233 rfcNoHistory="set" \
234 rfcHTML -o "$rfcDir"/criticisms.html
235 rfcHTML -o "$rfcDir"/index.html
236 #rfcPDF -o "$rfcDir/${rfcRepo}-${rfcBranch}".pdf
237 git add "$rfcDir"/{advocacy,criticisms,index}.html
238 #git add "$rfcDir/${rfcRepo}-${rfcBranch}".pdf
239 git diff --cached --quiet ||
240 git commit -m "$rfcBranch: v$rfcRevAdvocacy c$rfcRevCriticisms"
241 git tag --force --sign --message "${rfcOrga} RFC${rfcTag}" "$rfcTag"
242 fi
243 }