]> Git — Sourcephile - julm/rezine-rfcs.git/blob - scripts/lib.sh
p1: clarifications sur le refus du terme « commentaire »
[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 _ rfcRevGit <<<"$rfcTagCriticismsOld"
42 else rfcRevCriticisms=0; rfcTagCriticismsOld=init
43 fi
44 rfcBranchRevision="${rfcBranch}v${rfcRevAdvocacy}"
45 rfcTag="${rfcBranchRevision}c$rfcRevCriticisms${rfcRevGit:+-$rfcRevGit}"
46 }
47
48 rfcCitation () {
49 mkdir -p "$rfcRoot/rfcs/$rfcBranch/citation"
50 pandoc >"$rfcRoot/rfcs/$rfcBranch/citation/v${rfcRevAdvocacy}.csl.json" \
51 --wrap none \
52 $(rfcMetadata) \
53 --template "$rfcRoot/styles/rfc.csl.json" \
54 "$sourceFile"
55 }
56
57 rfcSource () {
58 pandoc \
59 --wrap none \
60 $(rfcMetadata) \
61 --template "$rfcRoot"/styles/rfc.metadata.yaml \
62 "$rfcDir"/advocacy.md
63 if test ! "${rfcNoAdvocacy:+set}"; then
64 printf '\n'
65 cat "$rfcRoot/styles/rfc.information.md"
66 printf '\n'
67 cat "$rfcDir"/advocacy.md
68 printf '\n'
69 fi
70 if test ! "${rfcNoThanks:+set}"; then
71 pandoc \
72 --wrap none \
73 $(rfcMetadata) \
74 --template "$rfcRoot"/styles/rfc.thanks.md \
75 "$rfcDir"/thanks.yaml
76 printf '\n'
77 fi
78 if test ! "${rfcNoHistory:+set}"; then
79 pandoc \
80 --wrap none \
81 $(rfcMetadata) \
82 --template "$rfcRoot"/styles/rfc.history.md \
83 "$rfcDir"/history.yaml
84 printf '\n'
85 fi
86 if test ! "${rfcNoCriticisms:+set}"; then
87 pandoc \
88 --wrap none \
89 $(rfcMetadata) \
90 --template "$rfcRoot"/styles/rfc.criticisms.md \
91 "$rfcDir"/criticisms.yaml
92 printf '\n'
93 fi
94 cat "$rfcRoot"/styles/rfc.references.md
95 }
96
97 rfcMetadata () {
98 printf ' --metadata %s' \
99 rfcBranch="$rfcBranch" \
100 rfcDate="$rfcDate" \
101 rfcDir="$rfcDir" \
102 rfcDomain="$rfcDomain" \
103 rfcGitweb="$rfcGitweb" \
104 rfcList="$rfcList" \
105 rfcOrga="$rfcOrga" \
106 rfcRepo="$rfcRepo" \
107 rfcBranchRevision="$rfcBranchRevision" \
108 rfcRevAdvocacy="$rfcRevAdvocacy" \
109 rfcRevisionAdvocacy="v$rfcRevAdvocacy" \
110 rfcRevisionCriticisms="c$rfcRevCriticisms" \
111 rfcTag="$rfcTag" \
112 rfcYear="$rfcYear"
113 }
114
115 rfcTemplate () {
116 local sourceFile metaFile
117 sourceFile=$(mktemp --suffix ".md")
118 metaFile=$(mktemp --suffix ".md")
119 removeAtExit+=("$sourceFile" "$metaFile")
120 rfcSource >"$sourceFile"
121 if test ! "${rfcNoAdvocacy:+set}" && test ! "${rfcNoCriticisms:+set}"; then
122 rfcCitation
123 fi
124 pandoc \
125 --wrap none \
126 $(rfcMetadata) \
127 --template "$sourceFile" \
128 "$sourceFile" | "$@"
129 }
130
131 rfcPlain () {
132 rfcTemplate \
133 pandoc --from markdown+emoji --to plain \
134 --citeproc \
135 --csl "$rfcRoot"/styles/rfc.csl \
136 $(printf " --bibliography %s" "$rfcRoot"/refs/*.json \
137 "$rfcRoot"/rfcs/*/citation/*.csl.json) \
138 --lua-filter "$rfcRoot"/styles/rfc.lua \
139 --variable lang=fr-FR \
140 "$@"
141 }
142
143 rfcMarkdown () {
144 rfcTemplate \
145 pandoc --from markdown+emoji --to markdown+emoji \
146 --table-of-contents \
147 --toc-depth 6 \
148 --number-sections \
149 --citeproc \
150 --csl "$rfcRoot"/styles/rfc.csl \
151 $(printf " --bibliography %s" "$rfcRoot"/refs/*.json \
152 "$rfcRoot"/rfcs/*/citation/*.csl.json) \
153 --lua-filter "$rfcRoot"/styles/rfc.lua \
154 --variable lang=fr-FR \
155 "$@"
156 }
157
158 rfcHTML () {
159 rfcTemplate \
160 pandoc --from markdown+emoji --to html5 \
161 --standalone \
162 --embed-resources \
163 --include-in-header "$rfcRoot"/styles/rfc.header.html \
164 --table-of-contents \
165 --toc-depth 6 \
166 --number-sections \
167 --citeproc \
168 --csl "$rfcRoot"/styles/rfc.csl \
169 $(printf " --bibliography %s" "$rfcRoot"/refs/*.json \
170 "$rfcRoot"/rfcs/*/citation/*.csl.json) \
171 --lua-filter "$rfcRoot"/styles/rfc.lua \
172 --variable lang=fr-FR \
173 "$@"
174 }
175
176 rfcPDF () {
177 rfcTemplate \
178 pandoc --from markdown+emoji --to pdf \
179 --pdf-engine lualatex \
180 --include-in-header "$rfcRoot"/styles/rfc.header.tex \
181 --standalone \
182 --embed-resources \
183 --table-of-contents \
184 --toc-depth 6 \
185 --number-sections \
186 --citeproc \
187 --csl "$rfcRoot"/styles/rfc.csl \
188 $(printf " --bibliography %s" "$rfcRoot"/refs/*.json) \
189 --lua-filter "$rfcRoot"/styles/rfc.lua \
190 --variable colorlinks=true \
191 --variable lang=fr-FR \
192 --variable links-as-notes=true \
193 "$@"
194 }
195
196 rfcTag () {
197 git reset
198 git diff --exit-code -- "$rfcDir" :'!*.html' ||
199 echo >&2 "ERROR: you have changes to commit"
200
201 : "Check if \$rfcRevAdvocacy must be increased."
202 rfcNoCriticisms="set" \
203 rfcNoThanks="set" \
204 rfcNoHistory="set" \
205 rfcHTML -o "$rfcDir"/advocacy.html
206 git add "$rfcDir"/advocacy.html
207 rfcRevAdvocacyIncrement=0
208 if ! git diff --cached --quiet "$rfcDir"/advocacy.html; then
209 rfcRevAdvocacyIncrement=1
210 # Note that $rfcRevCriticisms is not reset:
211 # criticisms have their own independant revisions
212 # because, in practice, criticisms to an old $rfcRevAdvocacy
213 # can be received and added after a new $rfcRevAdvocacy has been tagged.
214 fi
215
216 : "Check if \$rfcRevCriticisms must be increased."
217 rfcRevAdvocacy="1" \
218 rfcNoAdvocacy="set" \
219 rfcNoThanks="set" \
220 rfcNoHistory="set" \
221 rfcHTML -o "$rfcDir"/criticisms.html
222 git add "$rfcDir"/criticisms.html
223 rfcRevCriticismsIncrement=0
224 if ! git diff --cached --quiet "$rfcDir"/criticisms.html; then
225 rfcRevCriticismsIncrement=1
226 fi
227
228 if test "$rfcRevAdvocacyIncrement" -gt 0 ||
229 test "$rfcRevCriticismsIncrement" -gt 0
230 then
231 rfcRevAdvocacy=$((rfcRevAdvocacy + rfcRevAdvocacyIncrement))
232 rfcRevCriticisms=$((rfcRevCriticisms + rfcRevCriticismsIncrement))
233 rfcBranchRevision="${rfcBranch}v$rfcRevAdvocacy"
234 rfcTag="${rfcBranchRevision}c$rfcRevCriticisms"
235 rfcNoCriticisms="set" \
236 rfcNoThanks="set" \
237 rfcNoHistory="set" \
238 rfcHTML -o "$rfcDir"/advocacy.html
239 rfcRevAdvocacy="1" \
240 rfcNoAdvocacy="set" \
241 rfcNoThanks="set" \
242 rfcNoHistory="set" \
243 rfcHTML -o "$rfcDir"/criticisms.html
244 rfcHTML -o "$rfcDir"/index.html
245 #rfcPDF -o "$rfcDir/${rfcRepo}-${rfcBranch}".pdf
246 git add "$rfcDir"/{advocacy,criticisms,index}.html
247 git add "$rfcDir/citation/v${rfcRevAdvocacy}.csl.json"
248 #git add "$rfcDir/${rfcRepo}-${rfcBranch}".pdf
249 git diff --cached --quiet ||
250 git commit -m "$rfcBranch: v$rfcRevAdvocacy c$rfcRevCriticisms"
251 git tag --force --sign --message "${rfcOrga} RFC${rfcTag}" "$rfcTag"
252 fi
253 }