#!/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::START/END" markers are replaced by a # closed selection list if a vocabulary entry for 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[ \t]*)(?P# VOKABULAR:(?P\w+):START)\n' r'(?P.*?)\n' r'[ \t]*(?P# VOKABULAR:(?P=feld):END)', re.DOTALL, ) result = pattern.sub(replace_block, text) Path(out_path).write_text(result) PYEOF echo "config.yml generiert: $OUT"