Skip to content

Instantly share code, notes, and snippets.

@stephenpaulger
Created May 21, 2025 13:32
Show Gist options
  • Select an option

  • Save stephenpaulger/37b3ff6764c3592b1f142d6fd23ff039 to your computer and use it in GitHub Desktop.

Select an option

Save stephenpaulger/37b3ff6764c3592b1f142d6fd23ff039 to your computer and use it in GitHub Desktop.
#!/bin/bash
# If you want to make a left-to-right sankey diagram that you've downloaded from mermaid.live
# have no text and switch it to a top-to-bottom orientation then this is the script for you.
#
# Tested with Inkscape 1.4 on OSX.
#
# Usage: ./sankey_transform.sh input.svg output.svg|output.png
INPUT="$1"
OUTPUT="$2"
if [[ -z "$INPUT" || -z "$OUTPUT" ]]; then
echo "❌ Usage: $0 input.svg output.(svg|png)"
exit 1
fi
if [[ ! -f "$INPUT" ]]; then
echo "❌ Input file not found: $INPUT"
exit 1
fi
# Detect output type from file extension
EXT="${OUTPUT##*.}"
if [[ "$EXT" != "svg" && "$EXT" != "png" ]]; then
echo "❌ Output must be .svg or .png"
exit 1
fi
# Create temp files with .svg suffix
TMP1="$(mktemp -t sankey1_XXXXXX).svg"
TMP2="$(mktemp -t sankey2_XXXXXX).svg"
TMP3="$(mktemp -t sankey3_XXXXXX).svg"
TMPF="$(mktemp -t sankey_final_XXXXXX).svg"
# Step 1: Rotate 90° clockwise
inkscape "$INPUT" \
--actions='select-all;transform-rotate:90;export-filename:'"$TMP1"';export-do;file-close'
# Step 2: Flip horizontally
inkscape "$TMP1" \
--actions='select-all;object-flip-horizontal;export-filename:'"$TMP2"';export-do;file-close'
# Step 3: Delete all <text> elements
inkscape "$TMP2" \
--actions='select-by-element:text;delete;export-filename:'"$TMP3"';export-do;file-close'
# Step 4: Fit page to drawing
inkscape "$TMP3" \
--actions='page-fit-to-selection;export-filename:'"$TMPF"';export-do;file-close'
# Step 5: Export to requested format
if [[ "$EXT" == "svg" ]]; then
cp "$TMPF" "$OUTPUT"
echo "✅ Exported SVG to $OUTPUT"
elif [[ "$EXT" == "png" ]]; then
inkscape "$TMPF" --export-type=png --export-filename="$OUTPUT"
echo "✅ Exported PNG to $OUTPUT"
fi
# Clean up
rm -f "$TMP1" "$TMP2" "$TMP3" "$TMPF"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment