#!/usr/bin/env bash # Render a Reportist v1 YAML/JSON document without a Python runtime. set -euo pipefail root=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P) usage() { echo "Usage: $0 INPUT [--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) (($# >= 2)) || usage; output=$2; shift 2 ;; --typst) (($# >= 2)) || usage; typst_command=$2; shift 2 ;; --force) force=true; shift ;; *) usage ;; esac done case $input in *.json) format=json;; *.yaml|*.yml) format=yaml;; *) fail "expected a .yaml, .yml, 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: report does not exist" "$root/tools/validate.sh" "$input" >/dev/null || fail "invalid report" 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" if [[ -e "$output" && "$force" == false ]]; then fail "$output: output exists; use --force to replace it"; fi stage=$(mktemp -d "${TMPDIR:-/tmp}/reportist.XXXXXXXX") trap 'rm -rf "$stage"' EXIT cp -- "$input" "$stage/report.$format" cp -- "$root/templates/report.typ" "$stage/report.typ" cp -- "$root/tools/report-value.typ" "$stage/report-value.typ" logo=$( "$typst_command" query --root "$stage" --input "report=report.$format" --input "format=$format" \ "$stage/report-value.typ" '' --field value --one 2>/dev/null ) || fail "unable to read report input" if [[ "$logo" != null ]]; then logo=${logo#\"}; logo=${logo%\"} [[ -n "$logo" && "$logo" != /* && "$logo" != *\\* && "$logo" != *$'\n'* ]] || fail "/branding/logo: invalid relative asset path" IFS=/ read -r -a parts <<<"$logo" source_dir=$(dirname "$input"); cursor=$source_dir for part in "${parts[@]}"; do [[ -n "$part" && "$part" != . && "$part" != .. ]] || fail "/branding/logo: invalid relative asset path" cursor=$cursor/$part [[ ! -L "$cursor" ]] || fail "/branding/logo: symlinks are not accepted: $logo" done [[ -f "$cursor" ]] || fail "/branding/logo: asset does not exist: $logo" mkdir -p "$stage/$(dirname "$logo")" cp -- "$cursor" "$stage/$logo" fi if ! "$typst_command" compile --root "$stage" --input "report=report.$format" --input "format=$format" \ "$stage/report.typ" "$stage/report.pdf"; then fail "invalid report or Typst rendering failed" fi if [[ "$force" == true ]]; then mv -f -- "$stage/report.pdf" "$output"; else mv -- "$stage/report.pdf" "$output"; fi echo "Rendered PDF: $output"