2017-08-24 21:57:19 +02:00
|
|
|
#!/bin/sh
|
2017-12-03 10:50:54 +01:00
|
|
|
set -euo pipefail
|
2017-08-24 21:57:19 +02:00
|
|
|
#
|
|
|
|
# Behavior:
|
|
|
|
# Userscript for qutebrowser which will take the raw JSON text of the current
|
|
|
|
# page, format it using `jq`, will add syntax highlighting using `pygments`,
|
|
|
|
# and open the syntax highlighted pretty printed html in a new tab. If the file
|
|
|
|
# is larger than 10MB then this script will only indent the json and will forego
|
|
|
|
# syntax highlighting using pygments.
|
|
|
|
#
|
|
|
|
# In order to use this script, just start it using `spawn --userscript` from
|
|
|
|
# qutebrowser. I recommend using an alias, e.g. put this in the
|
|
|
|
# [alias]-section of qutebrowser.conf:
|
|
|
|
#
|
|
|
|
# json = spawn --userscript /path/to/json_format
|
|
|
|
#
|
|
|
|
# Note that the color style defaults to monokai, but a different pygments style
|
|
|
|
# can be passed as the first parameter to the script. A full list of the pygments
|
|
|
|
# styles can be found at: https://help.farbox.com/pygments.html
|
|
|
|
#
|
|
|
|
# Bryan Gilbert, 2017
|
|
|
|
|
2017-12-03 10:50:54 +01:00
|
|
|
# do not run pygmentize on files larger than this amount of bytes
|
|
|
|
MAX_SIZE_PRETTIFY=10485760 # 10 MB
|
2017-08-24 21:57:19 +02:00
|
|
|
# default style to monokai if none is provided
|
|
|
|
STYLE=${1:-monokai}
|
|
|
|
|
2017-12-03 10:50:54 +01:00
|
|
|
TEMP_FILE="$(mktemp)"
|
|
|
|
jq . "$QUTE_TEXT" >"$TEMP_FILE"
|
2017-08-24 21:57:19 +02:00
|
|
|
|
2017-12-03 10:50:54 +01:00
|
|
|
# try GNU stat first and then OSX stat if the former fails
|
|
|
|
FILE_SIZE=$(
|
|
|
|
stat --printf="%s" "$TEMP_FILE" 2>/dev/null ||
|
|
|
|
stat -f%z "$TEMP_FILE" 2>/dev/null
|
|
|
|
)
|
|
|
|
if [ "$FILE_SIZE" -lt "$MAX_SIZE_PRETTIFY" ]; then
|
|
|
|
pygmentize -l json -f html -O full,style="$STYLE" <"$TEMP_FILE" >"${TEMP_FILE}_"
|
|
|
|
mv -f "${TEMP_FILE}_" "$TEMP_FILE"
|
2017-08-24 21:57:19 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# send the command to qutebrowser to open the new file containing the formatted json
|
|
|
|
echo "open -t file://$TEMP_FILE" >> "$QUTE_FIFO"
|