AISE502/tools/md2docx.sh

27 lines
1.3 KiB
Bash
Executable File

#!/bin/zsh
# AISE502: Markdown-Modulbeschreibung -> Word (.docx), A4, Arial, mit Inhaltsverzeichnis.
# ./tools/md2docx.sh Modulbeschreibung_AISE502_v3_2026.md [ausgabe.docx]
# Benötigt pandoc; die Referenzvorlage tools/reference_fhgr.docx liefert Schriften, Ränder und A4.
set -e
SRC="${1:?Markdown-Datei angeben}"; OUT="${2:-${SRC:r}.docx}"
REF="$(dirname "$0")/reference_fhgr.docx"
# eigenstaendige "---"-Trennlinien entfernen: Word stellt sie als Absatz mit Punkt dar
sed '/^-\{3,\}$/d' "$SRC" | pandoc -f markdown+pipe_tables+smart -t docx \
--reference-doc="$REF" --toc --toc-depth=2 --metadata toc-title="Inhaltsverzeichnis" -o "$OUT"
python3 - "$OUT" <<'PY'
import re, shutil, sys, zipfile
p = sys.argv[1]; tmp = p + ".tmp"
with zipfile.ZipFile(p) as zin, zipfile.ZipFile(tmp, "w", zipfile.ZIP_DEFLATED) as zout:
for it in zin.infolist():
data = zin.read(it.filename)
if it.filename == "word/settings.xml":
s = data.decode("utf-8")
if "updateFields" not in s: # Word aktualisiert das Inhaltsverzeichnis beim Öffnen
s = re.sub(r"(<w:settings[^>]*>)", r'\1<w:updateFields w:val="true"/>', s, count=1)
data = s.encode("utf-8")
zout.writestr(it, data)
shutil.move(tmp, p)
print(f"{p}: Feldaktualisierung aktiviert")
PY
echo "fertig: $OUT"