Last active
April 19, 2025 07:21
-
-
Save baptiste/071a520e3f1c93f07632c1699c86821f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Exit on error | |
set -e | |
# Parse options | |
LAYOUT="3x4" # default | |
while getopts "l:" opt; do | |
case ${opt} in | |
l ) LAYOUT="$OPTARG" ;; | |
\? ) echo "Usage: nup [-l layout] document.typ"; exit 1 ;; | |
esac | |
done | |
shift $((OPTIND -1)) | |
# Input file | |
INPUT_FILE="$1" | |
if [ -z "$INPUT_FILE" ]; then | |
echo "Error: No Typst input file provided." | |
exit 1 | |
fi | |
# Filenames and paths | |
TMPDIR=$(mktemp -d) | |
BASENAME=$(basename "$INPUT_FILE" .typ) | |
NUP_FILE="${TMPDIR}/nup_typst_document.typ" | |
# Absolute path to output | |
OUTPUT_PDF="$(pwd)/${BASENAME}-nup.pdf" | |
# Compile original document to SVG pages | |
typst compile "$INPUT_FILE" "${TMPDIR}/preview{p}.svg" | |
# Count number of SVG files | |
NUM_PAGES=$(ls "$TMPDIR"/preview*.svg | wc -l) | |
UPPER_BOUND=$((NUM_PAGES + 1)) | |
# Generate Typst file to n-up the pages | |
cat > "$NUP_FILE" <<EOF | |
#import "@preview/nup:0.1.1": nup | |
#let pages = range(1, ${UPPER_BOUND}).map( | |
i => image("preview" + str(i) + ".svg")) | |
#nup("${LAYOUT}", pages) | |
EOF | |
# Move into TMPDIR and compile the nup document from there | |
( | |
cd "$TMPDIR" | |
typst compile "$(basename "$NUP_FILE")" "$OUTPUT_PDF" | |
) | |
# Clean up | |
rm -r "$TMPDIR" | |
echo "Done! Output written to $OUTPUT_PDF" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment