Skip to content

Instantly share code, notes, and snippets.

@hypnoJerk
Last active April 4, 2023 05:59
Show Gist options
  • Save hypnoJerk/781d9c33bd64d059bc467ed9e2e54fb2 to your computer and use it in GitHub Desktop.
Save hypnoJerk/781d9c33bd64d059bc467ed9e2e54fb2 to your computer and use it in GitHub Desktop.
Convert asciinema public share URL to iframe embed
#!/bin/bash
# https://gist.github.com/hypnoJerk/781d9c33bd64d059bc467ed9e2e54fb2
#
# Example usage:
# ./generate_iframe.sh https://asciinema.org/a/335480 -r 20 -c 90 -h 480px
# You may need to adjust the height, rows, or columns
# Extract the Asciicast ID from the URL
url=$1
asciicast_id=$(echo "$url" | sed -n 's|.*\/a\/\([0-9]*\)\/.*|\1|p')
# Extract the optional arguments
shift
while getopts ":r:c:h:" opt; do
case ${opt} in
r ) rows="$OPTARG";;
c ) cols="$OPTARG";;
h ) height="$OPTARG";;
\? ) echo "Invalid option: -$OPTARG" 1>&2; exit 1;;
: ) echo "Option -$OPTARG requires an argument." 1>&2; exit 1;;
esac
done
# Set default values for rows, columns, and height
rows=${rows:-24}
cols=${cols:-80}
height=${height:-480px}
# Generate the iframe code
iframe="<iframe src=\"$url/iframe?size=normal&amp;speed=1&amp;autoplay=false&amp;loop=false&amp;theme=asciinema&amp;t=0&amp;startAt=0&amp;preload=true&amp;cols=$cols&amp;rows=$rows&amp;i=true&amp;idleTimeLimit=2\" id=\"asciicast-iframe-$asciicast_id\" name=\"asciicast-iframe-$asciicast_id\" scrolling=\"no\" allowfullscreen=\"true\" style=\"overflow: hidden; margin: 0px; border: 0px; display: inline-block; width: 100%; float: none; visibility: visible; height: $height;\"></iframe>"
echo "$iframe"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment