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