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