From ada7c6976f9a4fbc59ba49861415d8bf43c75889 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 22 Mar 2023 14:38:04 -0400 Subject: [PATCH] added scheme extensions and fixed URIs only remembering their domains when replaced. --- README.md | 26 +++++++++++++------ example-extensions/youtube-mime.toml | 7 ----- xdg-sanity | 39 +++++++++++++++++++++------- 3 files changed, 47 insertions(+), 25 deletions(-) delete mode 100644 example-extensions/youtube-mime.toml diff --git a/README.md b/README.md index c9aa94e..e31533e 100644 --- a/README.md +++ b/README.md @@ -39,29 +39,39 @@ Extensions are written using TOML and are stored in either Extension support requires the installation of the `tomcat(1)` tool, which parses TOML for the command line. -There are two types of extensions: MIME and replace. MIME extensions are parsed -first and replace the MIME type of the content being fetched. Replace extensions -change the URI passed to the command to another. +There are three kinds of extensions: MIME, replace, and scheme. MIME extensions +are parsed first and replace the MIME type of the content being fetched. Replace +extensions change the URI passed to the command to another. Scheme extensions +replace the protocol by which the URI hostname is being accessed. The type of the extension depends on the file name. MIME extensions should have -a name ending in `-mime.toml` and replace extensions should have -`-replace.toml`. +a name ending in `.mime.toml`, replace extensions should have +`.replace.toml`, and scheme extensions should end with `.scheme.toml`. Here's what a MIME extension looks like: ``` -$ cat $XDG_DATA_HOME/xdg-sanity/youtube-mime.toml +$ cat $XDG_DATA_HOME/xdg-sanity/youtube.mime.toml [replace] urls = [ "youtube.com", "youtu.be" ] [with] mime = "video/vnd.youtube.yt" ``` -and here's what a replace extension looks like: +what a replace extension looks like: ``` -$ cat $XDG_DATA_HOME/xdg-sanity/youtube-replace.toml +$ cat $XDG_DATA_HOME/xdg-sanity/youtube.replace.toml [replace] urls = [ "youtube.com", "youtu.be" ] [with] url = "https://piped.mint.lgbt/" ``` +and what a scheme extension looks like: +``` +$ cat $XDG_DATA_HOME/xdg-sanity/spotify.scheme.toml +[replace] +urls = [ "spotify.com" ] + +[with] +scheme = "spotify://" +``` diff --git a/example-extensions/youtube-mime.toml b/example-extensions/youtube-mime.toml deleted file mode 100644 index 0b6ac6b..0000000 --- a/example-extensions/youtube-mime.toml +++ /dev/null @@ -1,7 +0,0 @@ -# xdg-sanity mime extension - -[replace] # replaces these urls -urls = [ "youtube.com", "youtu.be" ] - -[with] # replaces the mime type of the above -mime = "video/vnd.youtube.yt" diff --git a/xdg-sanity b/xdg-sanity index 9a00659..8d4724f 100755 --- a/xdg-sanity +++ b/xdg-sanity @@ -63,17 +63,22 @@ while test -n "$1"; do MIME="$(curl -Ls -o /dev/null -w '%{content_type}' "$1" | sed 's/\;.*//' \ | xargs echo)" + # get the URL with no scheme + NO_SCHEME="$(printf "%s\n" "$URL" | sed -n 's/^h.\+\/\///p')" + # get the pattern for the extensions to MATCH - MATCH="$(printf "%s\n" "$URL" | sed -ne 's/^h.\+\/\///p' \ - | sed -e 's/\/.*\+//g')" + MATCH="$(printf "%s\n" "$NO_SCHEME" | sed 's/\/.*\+//g')" + + # get the non-domain of the URI for tacking onto the end of replaced URIs + URI="$(printf "%s\n" "$NO_SCHEME" | sed -n 's/^[^/]*\///p')" # only check for extensions if tomcat(1) is installed if command -v tomcat >/dev/null 2>&1; then # run through MIME extensions for file in \ - "$XDG_DATA_HOME"/xdg-sanity/*-mime.toml \ - /usr/share/xdg-sanity/*-mime.toml \ - /usr/local/share/xdg-sanity/*-mime.toml + "$XDG_DATA_HOME"/xdg-sanity/*.mime.toml \ + /usr/share/xdg-sanity/*.mime.toml \ + /usr/local/share/xdg-sanity/*.mime.toml do if test -e "$file"; then get_urls @@ -85,21 +90,35 @@ while test -n "$1"; do fi done - # and the replace extensions + # then replace extensions for file in \ - "$XDG_DATA_HOME"/xdg-sanity/*-replace.toml \ - /usr/share/xdg-sanity/*-replace.toml \ - /usr/local/share/xdg-sanity/*-replace.toml + "$XDG_DATA_HOME"/xdg-sanity/*.replace.toml \ + /usr/share/xdg-sanity/*.replace.toml \ + /usr/local/share/xdg-sanity/*.replace.toml do if test -e "$file"; then get_urls for url in $URLS; do if [ "$MATCH" = "$url" ]; then - URL="$(tomcat with.url "$file")" + URL="$(tomcat with.url "$file")$URI" fi done fi done + + # and the scheme extensions + for file in \ + "$XDG_DATA_HOME"/xdg-sanity/*.scheme.toml \ + /usr/share/xdg-sanity/*.scheme.toml \ + /usr/local/share/xdg-sanity/*.scheme.toml + do + get_urls + for url in $URLS; do + if [ "$URL" = "$url" ]; then + URL="$(tomcat with.scheme "$file")$MATCH" + fi + done + done fi # these commands may fail; this is intentional