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