Files
website/scripts/generate-config.sh
T
tobias 224cfb5f68 Feldkatalog konsolidieren: admin/config.template.yml als einzige Quelle
Der Feldkatalog (Feldname/Label/Pflicht) war an mehreren Stellen von Hand
dupliziert - entdeckt beim Bau der Änderungshistorie, siehe
ableger/website#2. admin/config.template.yml bleibt die eine, dicht
kommentierte Quelle (Widget-Typ, Hinweistext, Pattern, Pflicht-Status,
Begründungen); ein neuer kleiner Parser (scripts/lib/feldkatalog.rb) liest
daraus, alle anderen Stellen lesen jetzt aus diesem Parser statt eigener
Kopien:

- scripts/validate.rb: methoden_pflichtfelder/varianten_pflichtfelder
  kommen jetzt aus Feldkatalog.pflichtfelder statt aus zwei hartcodierten
  Arrays.
- hugo/data/feldkatalog.json (neu generiert in scripts/generate-config.sh):
  {name: label}-Map für hugo/layouts/partials/aenderungssatz.html, dessen
  komplettes hartcodiertes $labels-Dict entfällt. Volle CMS-Labels
  erscheinen jetzt auch im Fließtext-Satz (z. B. "Dauer (Minuten)" statt
  bisher "Dauer").
- scripts/generate-config.sh's VOKABULAR-Renderer (fünfte, bisher nicht in
  ableger/website#2 erfasste Duplizierungsstelle): label/required werden
  jetzt aus dem ursprünglichen Template-Block gelesen statt aus einer
  separaten feld_labels-Kopie. "multiple" bleibt eine explizite kleine
  Liste - dafür gibt es im freitext-Fallback keine Entsprechung, das ist
  keine Duplizierung, sondern echte Zusatzinformation.
- scripts/pruefe-manifest-doku.rb (neu): vergleicht die Feldkatalog-Tabelle
  im öffentlichen Manifest-Kapitel (anderes Repo) gegen den Parser, manuell
  auszuführen - Regel 9 ("maschinell prüfbar") für genau diese Tabelle.

Inhaltliche Korrektur dabei (Rücksprache): kategorie, material und
initiatoren waren im Code bereits Pflicht, obwohl das Manifest-Kapitel sie
als optional auswies - nach Abgleich sollen sie tatsächlich optional
bleiben, config.template.yml bekommt entsprechend "required: false".

Verifiziert: admin/config.yml vor/nach Diff zeigt ausschließlich die drei
neuen "required: false"-Zeilen, sonst nichts (Renderer-Refactor
byte-identisch); validate.rb liefert dieselben Pflichtfeld-Listen wie
vorher (minus der drei jetzt optionalen Felder); hugo-Build + Sichtprüfung
bestätigen die neuen vollen CMS-Labels im Änderungsverlauf.
2026-08-30 17:47:31 +02:00

156 lines
6.6 KiB
Bash
Executable File

#!/bin/bash
# Generates admin/config.yml from admin/config.template.yml, AND
# hugo/hugo.toml from hugo/hugo.template.toml (see below) - both share the
# same "{{VARIABLE}}" substitution step, filled from install.env.
#
# admin/config.yml specifically has a second, independent substitution:
# fields between "# VOKABULAR:<field>:START/END" markers are replaced by a
# closed selection list if a vocabulary entry for <field> exists in the
# content repo under _content/ableger.yml - otherwise the block from the
# template stays unchanged (fallback: free text as before). See the
# Manifest chapter on the project website Teil C, vokabular/README.md and
# install.env.
#
# Runs both locally and in deploy (see the installation guide on the
# project website), AFTER sync-methoden.sh (which fetches
# _content/ableger.yml in the first place). Both admin/config.yml and
# hugo/hugo.toml are generated and not versioned (see .gitignore) -
# admin/config.template.yml and hugo/hugo.template.toml are the files to
# maintain.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
TEMPLATE="$ROOT/admin/config.template.yml"
OUT="$ROOT/admin/config.yml"
ABLEGER_YML="$ROOT/_content/ableger.yml"
# shellcheck source=/dev/null
source "$ROOT/install.env"
export GITEA_DOMAIN METHODEN_REPO OAUTH_CLIENT_ID WEBSITE_DOMAIN
# hugo.toml only needs the plain "{{VARIABLE}}" substitution, no VOKABULAR
# blocks - a plain sed pass is enough, no need for the Python step below.
HUGO_TEMPLATE="$ROOT/hugo/hugo.template.toml"
HUGO_OUT="$ROOT/hugo/hugo.toml"
sed -e "s|{{GITEA_DOMAIN}}|$GITEA_DOMAIN|g" \
-e "s|{{METHODEN_REPO}}|$METHODEN_REPO|g" \
-e "s|{{WEBSITE_DOMAIN}}|$WEBSITE_DOMAIN|g" \
"$HUGO_TEMPLATE" > "$HUGO_OUT"
echo "hugo.toml generiert: $HUGO_OUT"
# hugo/data/feldkatalog.json: {name: label} for hugo/layouts/partials/
# aenderungssatz.html, read from admin/config.template.yml via
# scripts/lib/feldkatalog.rb instead of a separate hardcoded label dict
# (see ableger/website#2). "inhalt" is added explicitly - it's not a real
# CMS field, but the pseudo-field scripts/generate-verlauf.rb emits for
# body/content changes.
ruby -I"$ROOT/scripts/lib" -rfeldkatalog -rjson -rfileutils -e '
katalog = {}
%w[methoden varianten].each do |collection|
Feldkatalog.felder(collection).each { |f| katalog[f[:name]] = f[:label] }
end
katalog["inhalt"] = "Inhalt"
out = File.join(ARGV[0], "hugo", "data", "feldkatalog.json")
FileUtils.mkdir_p(File.dirname(out))
File.write(out, JSON.pretty_generate(katalog))
' "$ROOT"
echo "feldkatalog.json generiert: $ROOT/hugo/data/feldkatalog.json"
# Export the vocabulary from ableger.yml (content repo) into a temporary
# directory with one text file per field (one allowed value per line) - this
# way the rest of the already-proven logic below (Python, reads from text
# files) stays unchanged. Ruby instead of Python for YAML parsing, since
# PyYAML isn't guaranteed to be available here, whereas Ruby's yaml library
# (standard library) is already used elsewhere in the project (see
# scripts/validate.rb).
VOKABULAR="$(mktemp -d)"
trap 'rm -rf "$VOKABULAR"' EXIT
if [ -f "$ABLEGER_YML" ]; then
ruby -ryaml -e '
daten = YAML.load_file(ARGV[0]) || {}
vokabular = daten["vokabular"] || {}
vokabular.each do |feld, werte|
File.write(File.join(ARGV[1], "#{feld}.txt"), Array(werte).join("\n") + "\n")
end
' "$ABLEGER_YML" "$VOKABULAR"
fi
python3 - "$TEMPLATE" "$OUT" "$VOKABULAR" <<'PYEOF'
import os
import re
import sys
from pathlib import Path
template_path, out_path, vokabular_dir = sys.argv[1:4]
text = Path(template_path).read_text()
vokabular_dir = Path(vokabular_dir)
# Always replace "{{VARIABLE}}" placeholders (see install.env), independent
# of the VOKABULAR mechanism below - no fallback logic needed, install.env
# (unlike _content/ableger.yml) is always versioned in this repo.
for feld in ("GITEA_DOMAIN", "METHODEN_REPO", "OAUTH_CLIENT_ID", "WEBSITE_DOMAIN"):
text = text.replace("{{" + feld + "}}", os.environ[feld])
# "multiple" (single- vs. multi-select once a closed vocabulary applies)
# has no equivalent in the free-text fallback widgets ("list"/"string")
# below - it can't be read off the template like label/required can, it's
# genuinely new information, not a duplicate of anything already written
# elsewhere. ONLY this set determines multi-select - "more than one option
# to choose from" (len(values) > 1) is not the same as "multiple values
# selectable at once" and must not be conflated. Found live: "lizenz" has 3
# options, but exactly ONE applies per entry - an earlier condition
# "len(values) > 1 or feld in multi_fields" incorrectly set multiple: true
# as soon as a second vocabulary value was added. Decap then stored lizenz
# as a list ("- CC0") instead of a plain string as soon as someone made a
# selection in the CMS - unnoticed until a value was actually changed.
multi_fields = {"zielgruppe", "kategorie", "tags"}
def render_field(feld, indent, label, required_false, values):
lines = [f'{indent}- label: "{label}"',
f'{indent} name: "{feld}"',
f'{indent} widget: "select"']
if required_false:
lines.append(f'{indent} required: false')
if feld in multi_fields:
lines.append(f'{indent} multiple: true')
lines.append(f'{indent} options:')
for wert in values:
lines.append(f'{indent} - "{wert}"')
return "\n".join(lines)
def replace_block(match):
feld = match.group("feld")
indent = match.group("indent")
vokabular_file = vokabular_dir / f"{feld}.txt"
start = match.group("start")
end = match.group("end")
if not vokabular_file.exists():
return match.group(0)
values = [line.strip() for line in vokabular_file.read_text().splitlines() if line.strip()]
if not values:
return match.group(0)
# label/required read straight out of the free-text fallback block
# instead of a second, separately maintained copy (see
# ableger/website#2) - a light regex is enough, no need for a full YAML
# parse of this one block.
fallback = match.group("body")
label_match = re.search(r'label:\s*"([^"]*)"', fallback)
label = label_match.group(1) if label_match else feld
required_false = re.search(r'required:\s*false\b', fallback) is not None
block = render_field(feld, indent, label, required_false, values)
return f"{indent}{start}\n{block}\n{indent}{end}"
pattern = re.compile(
r'(?P<indent>[ \t]*)(?P<start># VOKABULAR:(?P<feld>\w+):START)\n'
r'(?P<body>.*?)\n'
r'[ \t]*(?P<end># VOKABULAR:(?P=feld):END)',
re.DOTALL,
)
result = pattern.sub(replace_block, text)
Path(out_path).write_text(result)
PYEOF
echo "config.yml generiert: $OUT"