fix(cmus): HTML formatting error on notification when text contains &

This commit is contained in:
Yorick Barbanneau 2025-02-03 23:18:45 +01:00
parent 31d15e6416
commit 63581953db

View file

@ -3,73 +3,73 @@
NOTIFICATION_PROGRAM='mako' NOTIFICATION_PROGRAM='mako'
process_args() { process_args() {
while [ "$#" -ge 2 ]; do while [ "$#" -ge 2 ]; do
printf -v "$1" "%s" "$2" printf -v "$1" "%s" "$2"
shift shift
shift shift
done done
} }
escape() { escape() {
local string local string
string=$1 string=$1
string=${string//"<"/"&lt;"} string=${string//"&"/"&amp;"}
string=${string//">"/"&gt;"} string=${string//"<"/"&lt;"}
string=${string//"&"/"&amp;"} string=${string//">"/"&gt;"}
printf "%s" "$string" printf "%s" "$string"
} }
# Just quit this application if our notification program does not exist # Just quit this application if our notification program does not exist
# With this notification will not be displayed on my work laptop. Avoid # With this notification will not be displayed on my work laptop. Avoid
# notification pollution! # notification pollution!
check_notification_program() { check_notification_program() {
if ! command -v "$NOTIFICATION_PROGRAM"; then if ! command -v "$NOTIFICATION_PROGRAM"; then
exit 0 exit 0
fi fi
} }
main() { main() {
options=(--app-name=cmus --transient -u low) options=(--app-name=cmus --transient -u low)
filename=${file##*/} filename=${file##*/}
path=${file%/*} path=${file%/*}
if [[ -f "${path}/cover.png" ]]; then if [[ -f "${path}/cover.png" ]]; then
options+=(-i "${path}/cover.png") options+=(-i "${path}/cover.png")
fi
if [[ -f "${path}/cover.jpg" ]]; then
options+=(-i "${path}/cover.jpg")
fi
## Get title or filename
title=${title:-$filename}
if [[ ${#title} -gt 25 ]]; then
title="${title:0:24}"
fi
title="<b>$(escape "$title")</b>"
if [ -n "$artist" ]; then
if [[ ${#artist} -gt 22 ]]; then
artist="${artist:0:21}"
fi fi
printf -v l_artist "<span size='x-large'>by </span>%s" "$(escape "$artist")"
fi
if [[ -f "${path}/cover.jpg" ]]; then if [ -n "$date" ]; then
options+=(-i "${path}/cover.jpg") date="${date%%-*}"
fi
# Get Album
if [ -n "$album" ]; then
if [[ ${#album} -gt 19 ]]; then
album="${album:0:19}"
fi fi
printf -v l_album "<span size='x-large'>on </span>%-20s <span size='x-large'>%s</span>" "$(escape "$album")" "${date:-}"
fi
## Get title or filename printf -v l_intro "<span variant='small-caps'>cmus:<b>%s</b></span>" "$(escape "$status")"
title=${title:-$filename} printf -v body "%s\n" "${l_intro:-}" "${title:-}" "${l_artist:-}" "${l_album:-}"
if [[ ${#title} -gt 25 ]]; then notify-send "${options[@]}" "useless" "$body"
title="${title:0:24}"
fi
title="<b>$title</b>"
if [ -n "$artist" ]; then
if [[ ${#artist} -gt 22 ]]; then
artist="${artist:0:21}"
fi
printf -v l_artist "<span size='x-large'>by </span>%s" "$(escape "$artist")"
fi
if [ -n "$date" ]; then
date="${date%%-*}"
fi
# Get Album
if [ -n "$album" ]; then
if [[ ${#album} -gt 19 ]]; then
album="${album:0:19}"
fi
printf -v l_album "<span size='x-large'>on </span>%-20s <span size='x-large'>%s</span>" "$(escape "$album")" "${date:-}"
fi
printf -v l_intro "<span variant='small-caps'>cmus:<b>%s</b></span>" "$(escape "$status")"
printf -v body "%s\n" "${l_intro:-}" "${title:-}" "${l_artist:-}" "${l_album:-}"
notify-send "${options[@]}" "useless" "$body"
} }
check_notification_program check_notification_program