#!/usr/bin/env bash # # Visual review helper for the SVG exporter's `insta` snapshots. # # This script renders the insta snapshots into an HTML gallery # so you can judge whether a change is *visually* valid # before accepting it. # # Usage: # ./preview-snapshots # build the gallery and print its path # ./preview-snapshots --open # also open it in the default browser # # Text snapshots reference `fonts/sourcesanspro-regular.ttf`; this script copies # the bundled font into `target/svg-preview/fonts/` so the gallery renders text. # # When a test produced a pending change there will be a `*.snap.new` next to the # accepted `*.snap`; the gallery then shows "accepted" vs "new" side by side. # Once a change looks correct, accept it (rename `*.snap.new` -> `*.snap`, or # `cargo insta accept`) and re-run the tests. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" SNAP_DIR="$SCRIPT_DIR/src/render/svg/tests/snapshots" OUT_DIR="$SCRIPT_DIR/target/svg-preview" OUT="$OUT_DIR/index.html" FONT_SRC="$SCRIPT_DIR/src/fonts/sourcesanspro-regular.ttf" FONT_DIR="$OUT_DIR/fonts" mkdir -p "$OUT_DIR" mkdir -p "$FONT_DIR" cp "$FONT_SRC" "$FONT_DIR/" # Prints the SVG body of a snapshot file: everything after the second `---` # line (the YAML front matter insta writes). extract_body() { awk 'd>=2{print} /^---$/{d++}' "$1" } # Embeds a snapshot's SVG via pointing at a standalone .svg file. # # Snapshots must NOT be inlined together into one HTML document: each SVG # restarts its def ids at `clip0`/`blur0`/..., so inlining several in the same # document makes `url(#clip0)` collide and resolve to the wrong (or empty) clip # path. Writing each SVG to its own file isolates ids per document. # We use (not ) so linked @font-face rules and other external # resources resolve inside the SVG document. svg_counter=0 read_svg_dims() { local file="$1" local tag tag=$(grep -m1 '.*//') SVG_W=$(echo "$tag" | sed -n 's/.*[[:space:]]width="\([^"]*\)".*/\1/p') SVG_H=$(echo "$tag" | sed -n 's/.*[[:space:]]height="\([^"]*\)".*/\1/p') } emit_svg_box() { local body_file="$OUT_DIR/svg-$svg_counter.svg" local name svg_counter=$((svg_counter + 1)) name="$(basename "$body_file")" extract_body "$1" > "$body_file" read_svg_dims "$body_file" echo "
" } { cat <<'HTML' render-wasm SVG snapshot preview

render-wasm SVG snapshot preview

HTML shopt -s nullglob for snap in "$SNAP_DIR"/*.snap; do name="$(basename "$snap" .snap)" new="$snap.new" echo "

$name

" if [ -f "$new" ]; then echo '

Pending change: review before accepting

' echo '
' echo '
accepted (current .snap)
' emit_svg_box "$snap" echo '
' echo '
new (.snap.new)
' emit_svg_box "$new" echo '
' echo '
' echo '
text diff
'
            diff -u "$snap" "$new" | sed 's/&/\&/g; s//\>/g' || true
            echo '
' else emit_svg_box "$snap" fi echo '
' done # New tests whose snapshot has never been accepted yet. for new in "$SNAP_DIR"/*.snap.new; do base="${new%.new}" [ -f "$base" ] && continue name="$(basename "$new" .snap.new)" echo "

$name

" echo '

new snapshot (no accepted version yet)

' emit_svg_box "$new" echo '
' done } > "$OUT" echo "Wrote $OUT" if [ "${1:-}" = "--open" ]; then xdg-open "$OUT" >/dev/null 2>&1 || open "$OUT" >/dev/null 2>&1 || true fi