commit e88b6275d5446f288fb4bd211dc52da91884dc11
parent 3ca17ae565393c25e653b550d3f569e8ed40332d
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Thu, 11 Apr 2019 13:16:16 +0200
Add desktop notifications, support actions in linkhandler
Diffstat:
M | .urlview | | | 29 | ++++------------------------- |
D | bin/getcitation | | | 154 | ------------------------------------------------------------------------------- |
A | bin/getref | | | 161 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | bin/linkhandler | | | 61 | +++++++++++++++++++++++++++++++++++-------------------------- |
M | bin/scholarref | | | 2 | +- |
M | bin/shdl | | | 6 | ++++++ |
6 files changed, 207 insertions(+), 206 deletions(-)
diff --git a/.urlview b/.urlview
@@ -1,29 +1,8 @@
-###############################################################################
-# Urlview configuration file.
# man urlview <Man page>
-#
-# Put this file in: $HOME/.urlview
-# Put url_handler.sh in: /usr/bin
-#
-# You can call 'urlview' while in 'mutt' by pressing the Ctrl b keys.
-# Put these macros in your $HOME/.muttrc file.
-#
-# macro index \cb |urlview\n
-# macro pager \cb |urlview\n
-#
-# You can call 'urlview' while in 'tin' by pressing | then a for article,
-# put urlview as the pipe command.
-#
-# Regular expression to use to match URLs.
+# pattern to match URLs
#REGEXP (((http|https|ftp|gopher)|mailto):(//)?[^ >"\t]*|www\.[-a-z0-9.]+)[^ .,;\t>">\):]
-REGEXP (((http|https|ftp|gopher)|mailto)[.:][^ >"\t]*|www\.[-a-z0-9.]+)[^ .,;\t>">\):]
-
-# Command to invoke for selected URL. Use lynx, netscape, or url_handler.sh
-# shell script. Alternatively, you can leave COMMAND unset and set the BROWSER
-# environment variable instead.
-
-#COMMAND lynx %s
-#COMMAND netscape -remote 'openURL(%s)'
-#COMMAND url_handler.sh
+#REGEXP (((http|https|ftp|gopher)|mailto)[.:][^ >"\t]*|www\.[-a-z0-9.]+)[^ .,;\t>">\):]
+REGEXP (((http|https|ftp|gopher)|mailto):(//)?[^ <>"\t]*|(www|ftp)[0-9]?\.[-a-z0-9.]+)[^ .,;\t\n\r<">\):]?[^, <>"\t]*[^ .,;\t\n\r<">\):]
+COMMAND linkhandler %s
diff --git a/bin/getcitation b/bin/getcitation
@@ -1,154 +0,0 @@
-#!/usr/bin/env bash
-
-version=1.0
-host="https://doi.org"
-
-function show_help {
- echo "usage: ${0##*/} [OPTIONS] DOI1 [DOI2 ...]"
- echo "will attempt to get a BibTeX citation from $host"
- echo "If no DOIs are specified, this program will expect DOIs as stdin."
- echo
- echo "OPTIONS are one or more of the following:"
- echo " -h, --help show this message"
- echo " -v, --version show version and license information"
- echo " -V, --verbose show verbose information"
- echo " -t, --tor-socks use torsocks for HTTP requests"
- echo " -j, --full-journal return full journal name in citation"
- echo " -a, --full-author do not shorten author names"
- echo " -n, --no-newline suppress trailing newline but prepend with newline"
- echo " -- do not consider any following args as options"
-}
-
-function show_version {
- echo "${0##*/} version $version"
- echo "Licensed under the GNU Public License, v3+"
- echo "written by Anders Damsgaard, anders@adamsgaard.dk"
- echo "https://gitlab.com/admesg/dotfiles"
-}
-
-function die {
- printf '%s\n' "$1" >&2
- exit 1
-}
-
-function format_bibtex_key {
- sed '/@/ s/_\([0-9]\)/\1/'
-}
-
-function abbreviate_journal_name {
- sed '/journal = / {
- s/Journal/J./
- s/Geophysical/Geophys./
- s/Research/Res./
- s/Geophysical/Geophys./
- s/Geophysics/Geophys./
- s/Research/Res./
- s/Letters/Lett./
- s/Mechanics/Mech./
- s/Glaciology/Glaciol./
- s/Proceedings/Proc./
- s/Royal/R./
- s/Society/Soc./
- s/Annals/Ann./
- s/Resources/Resour./
- s/Surface/Surf./
- s/Processes/Proc./
- s/National/Nat./
- s/Computers/Comput./
- s/Geotechnics/Geotech./
- s/Academy/Acad./
- s/Sciences/Sci./
- s/Review/Rev./
- s/Quaternary/Quat./
- s/Physical/Phys./
- s/Planetary/Planet./
- s/Quarterly/Q./
- s/Geological/Geol./
- s/Applied/Appl./
- s/Physics/Phys./
- s/Communications/Commun./
- s/Geoscience/Geosci./
- s/Landforms/Land./
- s/Science/Sci./
- s/Annual/Ann./
- s/ of / /
- s/ and / /
- s/ in / /
- }'
-}
-
-function abbreviate_author_name {
- sed '/author = / { s/\([A-Z]\)[A-Za-z]* \([A-Z]\)/\1. \2/g }'
-}
-
-function strip_doi {
- sed 's/^(http:\/\/|https:\/\/)?(dx\.)?(doi\.org\/)//'
-}
-
-function get_citation {
- doi=$(echo "$1" | strip_doi)
- url="$host/$1"
- [ "$verbose" = 1 ] && echo "connecting to $url"
- result=$($prefix curl --location \
- --header "Accept: application/x-bibtex" \
- --silent --show-error "$url")
- result="$(echo "$result" | format_bibtex_key)"
- [ "$fulljournal" = 0 ] && result="$(echo "$result" | abbreviate_journal_name)"
- [ "$fullauthor" = 0 ] && result="$(echo "$result" | abbreviate_author_name)"
- result="$(echo "$result" | sed 's/\t/ /g')"
- if [ "$nonewline" = 1 ]; then
- echo -en "\n$result"
- else
- echo "$result"
- fi
-}
-
-verbose=0
-fulljournal=0
-fullauthor=0
-nonewline=0
-prefix=""
-while :; do
- case "$1" in
- -h|-\?|--help)
- show_help
- exit 0
- ;;
- -v|--version)
- show_version
- exit 0
- ;;
- -j|--full-journal)
- fulljournal=1
- ;;
- -a|--full-author)
- fullauthor=1
- ;;
- -n|--no-newline)
- nonewline=1
- ;;
- -V|--verbose)
- verbose=1
- ;;
- -t|--tor-socks)
- prefix="torsocks"
- ;;
- --) # end all options
- shift
- break
- ;;
- -?*)
- die 'Error: Unknown option specified'
- ;;
- *) # No more options
- break
- esac
- shift
-done
-
-if [ $# -lt 1 ]; then
- doi="$(cat)"
- get_citation "$doi"
-else
- get_citation "$@"
-fi
diff --git a/bin/getref b/bin/getref
@@ -0,0 +1,161 @@
+#!/usr/bin/env bash
+
+version=1.0
+host="https://doi.org"
+
+function show_help {
+ echo "usage: ${0##*/} [OPTIONS] DOI1 [DOI2 ...]"
+ echo "will attempt to get a BibTeX citation from $host"
+ echo "If no DOIs are specified, this program will expect DOIs as stdin."
+ echo
+ echo "OPTIONS are one or more of the following:"
+ echo " -h, --help show this message"
+ echo " -v, --version show version and license information"
+ echo " -V, --verbose show verbose information"
+ echo " -t, --tor-socks use torsocks for HTTP requests"
+ echo " -j, --full-journal return full journal name in citation"
+ echo " -a, --full-author do not shorten author names"
+ echo " -n, --no-newline suppress trailing newline but prepend with newline"
+ echo " -N, --notify send desktop notification when complete"
+ echo " -- do not consider any following args as options"
+}
+
+function show_version {
+ echo "${0##*/} version $version"
+ echo "Licensed under the GNU Public License, v3+"
+ echo "written by Anders Damsgaard, anders@adamsgaard.dk"
+ echo "https://gitlab.com/admesg/dotfiles"
+}
+
+function die {
+ printf '%s\n' "$1" >&2
+ exit 1
+}
+
+function format_bibtex_key {
+ sed '/@/ s/_\([0-9]\)/\1/'
+}
+
+function abbreviate_journal_name {
+ sed '/journal = / {
+ s/Journal/J./
+ s/Geophysical/Geophys./
+ s/Research/Res./
+ s/Geophysical/Geophys./
+ s/Geophysics/Geophys./
+ s/Research/Res./
+ s/Letters/Lett./
+ s/Mechanics/Mech./
+ s/Glaciology/Glaciol./
+ s/Proceedings/Proc./
+ s/Royal/R./
+ s/Society/Soc./
+ s/Annals/Ann./
+ s/Resources/Resour./
+ s/Surface/Surf./
+ s/Processes/Proc./
+ s/National/Nat./
+ s/Computers/Comput./
+ s/Geotechnics/Geotech./
+ s/Academy/Acad./
+ s/Sciences/Sci./
+ s/Review/Rev./
+ s/Quaternary/Quat./
+ s/Physical/Phys./
+ s/Planetary/Planet./
+ s/Quarterly/Q./
+ s/Geological/Geol./
+ s/Applied/Appl./
+ s/Physics/Phys./
+ s/Communications/Commun./
+ s/Geoscience/Geosci./
+ s/Landforms/Land./
+ s/Science/Sci./
+ s/Annual/Ann./
+ s/ of / /
+ s/ and / /
+ s/ in / /
+ }'
+}
+
+function abbreviate_author_name {
+ sed '/author = / { s/\([A-Z]\)[A-Za-z]* \([A-Z]\)/\1. \2/g }'
+}
+
+function strip_doi {
+ sed 's/^(http:\/\/|https:\/\/)?(dx\.)?(doi\.org\/)//'
+}
+
+function get_citation {
+ doi=$(echo "$1" | strip_doi)
+ url="$host/$1"
+ [ "$verbose" = 1 ] && echo "connecting to $url"
+ result=$($prefix curl --location \
+ --header "Accept: application/x-bibtex" \
+ --silent --show-error "$url")
+ result="$(echo "$result" | format_bibtex_key)"
+ [ "$fulljournal" = 0 ] && result="$(echo "$result" | abbreviate_journal_name)"
+ [ "$fullauthor" = 0 ] && result="$(echo "$result" | abbreviate_author_name)"
+ result="$(echo "$result" | sed 's/\t/ /g')"
+ if [ "$nonewline" = 1 ]; then
+ echo -en "\n$result"
+ else
+ echo "$result"
+ fi
+ [ "$notify" = 1 ] && notify-send "${0##*/} added:
+$(echo "$result" | cut -c-80)"
+}
+
+verbose=0
+fulljournal=0
+fullauthor=0
+nonewline=0
+notify=0
+prefix=""
+while :; do
+ case "$1" in
+ -h|-\?|--help)
+ show_help
+ exit 0
+ ;;
+ -v|--version)
+ show_version
+ exit 0
+ ;;
+ -j|--full-journal)
+ fulljournal=1
+ ;;
+ -a|--full-author)
+ fullauthor=1
+ ;;
+ -n|--no-newline)
+ nonewline=1
+ ;;
+ -N|--notify)
+ notify=1
+ ;;
+ -V|--verbose)
+ verbose=1
+ ;;
+ -t|--tor-socks)
+ prefix="torsocks"
+ ;;
+ --) # end all options
+ shift
+ break
+ ;;
+ -?*)
+ die 'Error: Unknown option specified'
+ ;;
+ *) # No more options
+ break
+ esac
+ shift
+done
+
+if [ $# -lt 1 ]; then
+ doi="$(cat)"
+ get_citation "$doi"
+else
+ get_citation "$@"
+fi
diff --git a/bin/linkhandler b/bin/linkhandler
@@ -1,27 +1,36 @@
-#!/bin/sh
+#!/usr/bin/env bash
-# Feed script a url or file location.
-# If an image, it will view in feh,
-# if a video or gif, it will view in mpv
-# if a music file or pdf, it will download,
-# otherwise it opens link in browser.
-
-# Sci-Hub's domain occasionally changes due to shutdowns:
-scihub="http://sci-hub.tw/"
-
-# If no url given. Opens browser. For using script as $BROWSER.
-[ -z "$1" ] && { "$TRUEBROWSER"; exit; }
-
-case "$1" in
- *mkv|*webm|*mp4|*gif|*youtube.com*|*youtu.be*|*hooktube.com*|*bitchute.com*)
- setsid mpv -quiet "$1" >/dev/null 2>&1 & ;;
- *png|*jpg|*jpe|*jpeg)
- setsid sxiv "$1" >/dev/null 2>&1 & ;;
- *mp3|*flac|*opus|*mp3?source)
- setsid tsp curl -LO "$1" >/dev/null 2>&1 & ;;
- *springer.com*)
- setsid curl -sO "$(curl -s "$scihub$*" | grep -Po "(?<=location.href=').+.pdf")" >/dev/null 2>&1 & ;;
- *)
- if [ -f "$1" ]; then "$TERMINAL" -e "$EDITOR $1"
- else setsid "$TRUEBROWSER" "$1" >/dev/null 2>&1 & fi ;;
-esac
+for url in "$@"; do
+ case "$url" in
+ *mkv|*webm|*mp4|*gif|*youtube.com*|*youtu.be*|*hooktube.com*|*bitchute.com*)
+ setsid mpv -quiet "$url" >/dev/null 2>&1 & ;;
+ *png|*jpg|*jpe|*jpeg)
+ setsid sxiv "$url" >/dev/null 2>&1 & ;;
+ *mp3|*flac|*opus|*mp3?source)
+ setsid tsp curl -LO "$url" >/dev/null 2>&1 & ;;
+ *)
+ if [ -f "$url" ]; then
+ "$TERMINAL" -e "$EDITOR $url"
+ else
+ url="$(echo "$url" | sed 's|http.*://scholar\.google\..*url=||;s|&rt=.*$||')"
+ action="$(printf "Open\nOpen (Tor Browser)\nCopy\nDownload\nSci-Hub\nAdd reference" | dmenu_themed -i -p "Action:")"
+ if [ "$action" = "Open" ]; then
+ setsid xdg-open "$url" >/dev/null 2>&1 &
+ elif [ "$action" = "Open (Tor Browser)" ]; then
+ setsid tor-browser "$url" >/dev/null 2>&1 &
+ elif [ "$action" = "Copy" ]; then
+ echo "$url" | xclip
+ elif [ "$action" = "Download" ]; then
+ setsid tsp curl -LO "$url" >/dev/null 2>&1 &
+ elif [ "$action" = "Sci-Hub" ]; then
+ setsid tsp shdl --tor-socks --notify "$url" >/dev/null 2>&1 &
+ elif [ "$action" = "Add reference" ]; then
+ getdoi "$url" | getref --notify >> "$BIB" &
+ else
+ notify-send "Error: Action not understood"
+ exit 1
+ fi
+ fi
+ ;;
+ esac
+done
diff --git a/bin/scholarref b/bin/scholarref
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
set -e
-getdoi "$@" | getcitation -n
+getdoi "$@" | getref -n
diff --git a/bin/shdl b/bin/shdl
@@ -18,6 +18,7 @@ function show_help {
echo " -t, --tor-socks use torsocks for requests to sci-hub"
echo " -b, --browser open sci-hub in browser immediately"
echo " -r, --reference add reference to bibliography using scholarref.py"
+ echo " -n, --notify send desktop notification when complete"
echo " -- do not consider any following args as options"
}
@@ -92,6 +93,7 @@ prefix=""
verbose=0
browser=0
reference=0
+notify=0
while :; do
case "$1" in
-h|-\?|--help)
@@ -114,6 +116,9 @@ while :; do
-r|--reference)
reference=1
;;
+ -n|--notify)
+ notify=1
+ ;;
--) # end all options
shift
break
@@ -133,6 +138,7 @@ if [ $# -lt 1 ]; then
else
for doi in "$@"; do
handle_doi "$doi"
+ [ "$notify" = 1 ] && notify-send "${0##*/}:\n$doi complete"
done
fi
exit "$returnstatus"