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