Files
ProcessControl/_extensions/notes/handin.sh
T
2026-09-25 13:33:30 +02:00

86 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# handin.sh — extract exercises and their solutions into a PDF to hand back
#
# _extensions/notes/handin.sh [options] SELECTION...
#
# SELECTION (any mix):
# 1.3 an exercise, numbered as in the notes
# 2 every exercise of chapter 2
# 2.Light.qmd every exercise of that chapter file
# '#my-id' the exercise written ::::{.exo #my-id}
# all every exercise of the book
#
# Options:
# -o, --output FILE where to write it (default: _handin/solutions-<selection>.pdf)
# --no-statement only the solutions, not the exercise statements
# --hints keep the hints (dropped by default)
# --subtitle TEXT title page subtitle (default: "Solutions — Exercise 1.3, ...")
# --profile NAME Quarto profile, e.g. the language of a multilingual book
# --to pdf|docx output format (default: pdf)
#
# The whole book is rendered as usual (same config, figures, glossary,
# language), into a temporary folder: _book/ is not touched. Only the selected
# exercises are kept, statements with solutions in place, one part after the
# other. The console lists every extracted exercise and flags empty solutions.
#
# Example: _extensions/notes/handin.sh 1.3 1.5 --subtitle "Series 2"
set -euo pipefail
usage() { sed -n '2,/^$/{s/^# \{0,1\}//;p}' "$0"; }
project="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
[ -f "$project/_quarto.yml" ] || { echo "handin: no _quarto.yml in $project" >&2; exit 1; }
output="" statement=true hints=false subtitle="" profile="" to=pdf
selection=()
while [ $# -gt 0 ]; do
case "$1" in
-h|--help) usage; exit 0 ;;
-o|--output) output="$2"; shift ;;
--no-statement) statement=false ;;
--hints) hints=true ;;
--subtitle) subtitle="$2"; shift ;;
--profile) profile="$2"; shift ;;
--to) to="$2"; shift ;;
-*) echo "handin: unknown option $1 (see --help)" >&2; exit 1 ;;
*) selection+=("$1") ;;
esac
shift
done
[ ${#selection[@]} -gt 0 ] || { usage; exit 1; }
case "$to" in pdf|docx) ;; *) echo "handin: --to must be pdf or docx" >&2; exit 1 ;; esac
if [ -z "$output" ]; then
name="$(printf '%s_' "${selection[@]}" | tr -c 'A-Za-z0-9._-' '_' | sed 's/_*$//')"
output="$project/_handin/solutions-$name.$to"
fi
case "$output" in /*) ;; *) output="$PWD/$output" ;; esac
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
# YAML double-quoted string
yq() { local s="${1//\\/\\\\}"; s="${s//\"/\\\"}"; printf '"%s"' "$s"; }
{
echo "exo-handin: $(yq "${selection[*]}")"
echo "exo-handin-statement: $statement"
echo "exo-handin-hints: $hints"
[ -n "$subtitle" ] && echo "exo-handin-subtitle: $(yq "$subtitle")"
echo "toc: false"
echo "lof: false"
echo "lot: false"
} > "$tmp/handin.yml"
cd "$project"
args=(render --to "$to" --no-clean --metadata-file "$tmp/handin.yml" --output-dir "$tmp/out")
[ -n "$profile" ] && args+=(--profile "$profile")
quarto "${args[@]}"
result="$(find "$tmp/out" -maxdepth 1 -name "*.$to" | head -n 1)"
[ -n "$result" ] || { echo "handin: quarto produced no .$to file" >&2; exit 1; }
mkdir -p "$(dirname "$output")"
cp "$result" "$output"
echo
echo "handin: wrote $output"