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