38 lines
2.0 KiB
Bash
Executable File
38 lines
2.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Render a report with the report plugin's own Typst document model.
|
|
set -euo pipefail
|
|
root=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)
|
|
usage() { echo "Usage: $0 INPUT.yaml|json [--output PDF] [--typst COMMAND] [--force]" >&2; exit 2; }
|
|
fail() { echo "error: $*" >&2; exit 1; }
|
|
input=${1:-}; [[ -n "$input" ]] || usage; shift
|
|
output= typst_command=typst force=false
|
|
while (($#)); do
|
|
case $1 in
|
|
--output|--typst) (($# >= 2)) || usage
|
|
case $1 in --output) output=$2;; --typst) typst_command=$2;; esac; shift 2;;
|
|
--force) force=true; shift;;
|
|
*) usage;;
|
|
esac
|
|
done
|
|
case $input in *.yaml|*.yml) format=yaml;; *.json) format=json;; *) fail "expected a YAML or JSON report";; esac
|
|
command -v "$typst_command" >/dev/null 2>&1 || fail "Typst executable not found: $typst_command"
|
|
input=$(realpath "$input"); [[ -f "$input" ]] || fail "$input: input does not exist"
|
|
if [[ -z "$output" ]]; then output=${input%.*}.pdf; fi
|
|
output_parent=$(dirname "$output"); mkdir -p "$output_parent"
|
|
output=$(cd "$output_parent" && pwd -P)/$(basename "$output")
|
|
[[ "$input" != "$output" ]] || fail "input and output paths must differ"
|
|
[[ ! -e "$output" || "$force" == true ]] || fail "$output: output exists; use --force to replace it"
|
|
stage=$(mktemp -d "${TMPDIR:-/tmp}/beffroi-report.XXXXXXXX")
|
|
trap 'rm -rf -- "$stage"' EXIT
|
|
mkdir -p "$stage/modules" "$stage/config" "$stage/assets" "$stage/reports" "$stage/visual"
|
|
cp -- "$root/plugins/report/render.typ" "$stage/report.typ"
|
|
cp -- "$root/plugins/report/model.typ" "$stage/modules/model.typ"
|
|
cp -- "$root/visual/themes.yaml" "$stage/config/themes.yaml"
|
|
cp -- "$root/visual/beffroi.png" "$stage/assets/beffroi.png"
|
|
cp -- "$root/visual/elements.typ" "$stage/visual/elements.typ"
|
|
cp -- "$input" "$stage/reports/input.$format"
|
|
"$typst_command" compile --root "$stage" --input "report=reports/input.$format" --input "format=$format" \
|
|
"$stage/report.typ" "$stage/report.pdf" || fail "report rendering failed"
|
|
mv -f -- "$stage/report.pdf" "$output"
|
|
echo "Rendered PDF: $output"
|