summaryrefslogtreecommitdiffhomepage
path: root/build/spec-pdf.sh
blob: 687065486dadf983d9833b7fe2109690cb5cdbb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/sh
# shellcheck shell=sh
#
# spec-pdf.sh - typeset formal/zash-spec.typ into formal/zash-spec.pdf
#
# usage: spec-pdf.sh [-v] [out.pdf]
#
# Run from anywhere inside the repository.  The root passed to typst is the
# repository root, because the spec reads ../packaging/zash-logo.svg and
# typst refuses files outside its root.
#
# Uses the typst CLI if it is on PATH, otherwise the Python package
# (pip install typst), which is how the PDF was first built.

set -eu

verbose=0
if [ "${1-}" = -v ]; then
    verbose=1
    shift
fi

root=$(cd "$(dirname "$0")/.." && pwd)
src=$root/formal/zash-spec.typ
out=${1:-$root/formal/zash-spec.pdf}

[ -f "$src" ] || { echo "spec-pdf.sh: $src: not found" >&2; exit 1; }

if command -v typst >/dev/null 2>&1; then
    [ "$verbose" = 1 ] && echo "typst CLI: $(typst --version)" >&2
    typst compile --root "$root" "$src" "$out"
elif python3 -c 'import typst' 2>/dev/null; then
    [ "$verbose" = 1 ] && echo "typst Python package" >&2
    python3 - "$src" "$out" "$root" <<'EOF'
import sys
import typst
src, out, root = sys.argv[1:]
typst.compile(src, output=out, root=root)
EOF
else
    echo "spec-pdf.sh: need typst (CLI) or 'pip install typst'" >&2
    exit 1
fi

[ "$verbose" = 1 ] && echo "wrote $out" >&2
exit 0