]> Git — Sourcephile - julm/rezine-rfcs.git/blob - scripts/lib.sh
SelfDescr(styles): replace rfcDate by rfcDateAdvocacy and rfcDateCriticisims
[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 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 _ rfcRevGit <<<"$rfcTagCriticismsOld"
43 else rfcRevCriticisms=0; rfcTagCriticismsOld=init
44 fi
45 rfcBranchRevision="${rfcBranch}v${rfcRevAdvocacy}"
46 rfcTag="${rfcBranchRevision}c$rfcRevCriticisms${rfcRevGit:+-$rfcRevGit}"
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 echo >&2 "ERROR: you have changes to commit"
202
203 : "Check if \$rfcRevAdvocacy must be increased."
204 rfcNoCriticisms="set" \
205 rfcNoThanks="set" \
206 rfcNoHistory="set" \
207 rfcHTML -o "$rfcDir"/advocacy.html
208 git add "$rfcDir"/advocacy.html
209 rfcRevAdvocacyIncrement=0
210 if ! git diff --cached --quiet "$rfcDir"/advocacy.html; then
211 rfcRevAdvocacyIncrement=1
212 # Note that $rfcRevCriticisms is not reset:
213 # criticisms have their own independant revisions
214 # because, in practice, criticisms to an old $rfcRevAdvocacy
215 # can be received and added after a new $rfcRevAdvocacy has been tagged.
216 fi
217
218 : "Check if \$rfcRevCriticisms must be increased."
219 rfcRevAdvocacy="1" \
220 rfcNoAdvocacy="set" \
221 rfcNoThanks="set" \
222 rfcNoHistory="set" \
223 rfcHTML -o "$rfcDir"/criticisms.html
224 git add "$rfcDir"/criticisms.html
225 rfcRevCriticismsIncrement=0
226 if ! git diff --cached --quiet "$rfcDir"/criticisms.html; then
227 rfcRevCriticismsIncrement=1
228 fi
229
230 if test "$rfcRevAdvocacyIncrement" -gt 0 ||
231 test "$rfcRevCriticismsIncrement" -gt 0
232 then
233 rfcRevAdvocacy=$((rfcRevAdvocacy + rfcRevAdvocacyIncrement))
234 rfcRevCriticisms=$((rfcRevCriticisms + rfcRevCriticismsIncrement))
235 rfcBranchRevision="${rfcBranch}v$rfcRevAdvocacy"
236 rfcTag="${rfcBranchRevision}c$rfcRevCriticisms"
237 rfcNoCriticisms="set" \
238 rfcNoThanks="set" \
239 rfcNoHistory="set" \
240 rfcHTML -o "$rfcDir"/advocacy.html
241 rfcRevAdvocacy="1" \
242 rfcNoAdvocacy="set" \
243 rfcNoThanks="set" \
244 rfcNoHistory="set" \
245 rfcHTML -o "$rfcDir"/criticisms.html
246 rfcHTML -o "$rfcDir"/index.html
247 #rfcPDF -o "$rfcDir/${rfcRepo}-${rfcBranch}".pdf
248 git add "$rfcDir"/{advocacy,criticisms,index}.html
249 git add "$rfcDir/citation/v${rfcRevAdvocacy}.csl.json"
250 #git add "$rfcDir/${rfcRepo}-${rfcBranch}".pdf
251 git diff --cached --quiet ||
252 git commit -m "$rfcBranch: v$rfcRevAdvocacy c$rfcRevCriticisms"
253 git tag --force --sign --message "${rfcOrga} RFC${rfcTag}" "$rfcTag"
254 fi
255 }