dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | README | LICENSE Back to index

commit 5e8333356b664fa7b394fbcc590d2d2ba91b0a30
parent fa0603a5aca02ab8cd255741805c9bcd6bd301c8
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Wed, 10 Apr 2019 13:56:42 +0200

Rewrite scholarref functionality to UNIX style, update Vim bindings

Diffstat:
M.commands.sh | 1-
M.config/newsboat/config | 1+
M.vim/plugin/keybinds.vim | 4++--
M.vim/plugins.vim | 1-
Mbin/getcitation | 14+++++++++++++-
Mbin/getdoi | 4++--
Mbin/scholarref | 2+-
Dbin/scholarref.py | 120-------------------------------------------------------------------------------
Mbin/shdl | 145+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
9 files changed, 150 insertions(+), 142 deletions(-)

diff --git a/.commands.sh b/.commands.sh @@ -22,7 +22,6 @@ c() { ## ls - if [[ "$(uname)" != 'Darwin' ]]; then alias ls='ls --color=auto -F' else diff --git a/.config/newsboat/config b/.config/newsboat/config @@ -67,3 +67,4 @@ macro r set browser "rtv"; open-in-browser ; toggle-article-read ; set browser o # macro p set browser "dmenuhandler"; open-in-browser ; set browser xdg-open macro y set browser "echo -n %u | xclip -selection clipboard" ; open-in-browser ; set browser xdg-open macro p set browser "wkhtmltopdf %u out.pdf && mv out.pdf ~/tmp/$(basename $(mktemp)).pdf" ; open-in-browser ; set browser xdg-open +macro s set browser "cd ~/tmp && tsp shdl -t %u" ; open-in-browser ; set browser xdg-open diff --git a/.vim/plugin/keybinds.vim b/.vim/plugin/keybinds.vim @@ -54,9 +54,9 @@ nnoremap <leader>CA :e ~/.vim/plugin/appearance.vim<CR> nnoremap <leader>CG :e ~/.vim/plugin/00-general.vim<CR> nnoremap <leader>CP :e ~/.vim/plugins.vim<CR> nnoremap <leader>T :e ~/doc/todo.md<CR> -nnoremap <leader>B :e ~/articles/own/BIB.bib<CR> +nnoremap <leader>B :e ~/articles/own/BIBnew.bib \| :windo normal G<CR> -nnoremap <leader>r :ScholarRef +nnoremap <leader>r :read !scholarref " insert date and time nnoremap <leader>D :read !date +"\# \%Y-\%m-\%d \%T \%Z (\%z UTC)"<CR> diff --git a/.vim/plugins.vim b/.vim/plugins.vim @@ -89,7 +89,6 @@ Plug 'vim-scripts/mutt-aliases', { 'for': 'mail' } " complete aliases w C-x C-u " LaTeX Plug 'lervag/vimtex' -Plug 'anders-dc/vim-scholarref' " search bibtex refs from web " Vimscript Plug 'junegunn/vader.vim' " testing framework diff --git a/bin/getcitation b/bin/getcitation @@ -15,6 +15,7 @@ function show_help { 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" } @@ -68,6 +69,9 @@ function abbreviate_journal_name { s/Communications/Commun./ s/Geoscience/Geosci./ s/Landforms/Land./ + s/ of / / + s/ and / / + s/ in / / }' } @@ -89,12 +93,17 @@ function get_citation { result="$(echo "$result" | format_bibtex_key)" [ "$fulljournal" = 0 ] && result="$(echo "$result" | abbreviate_journal_name)" [ "$fullauthor" = 0 ] && result="$(echo "$result" | abbreviate_author_name)" - echo "$result" + 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 @@ -112,6 +121,9 @@ while :; do -a|--full-author) fullauthor=1 ;; + -n|--no-newline) + nonewline=1 + ;; -V|--verbose) verbose=1 ;; diff --git a/bin/getdoi b/bin/getdoi @@ -83,8 +83,8 @@ while :; do done if [ $# -lt 1 ]; then - doi="$(cat)" - get_doi "$doi" + query="$(cat)" + get_doi "$query" else get_doi "$@" fi diff --git a/bin/scholarref b/bin/scholarref @@ -1,3 +1,3 @@ #!/usr/bin/env bash set -e -getdoi "$@" | getcitation +getdoi "$@" | getcitation -n diff --git a/bin/scholarref.py b/bin/scholarref.py @@ -1,120 +0,0 @@ -#!/usr/bin/env python2 -""" -scholarref.py: - -This script searches an online database and returns a full BibTeX reference to -the command line for the publication most closely matching the query. - -Usage: - ./scholarref.py <query> - -Example: - ./scholarref.py water flow in glaciers - @incollection{Hooke, - doi = {10.1017/cbo9780511614231.012}, - publisher = {Cambridge University Press ({CUP})}, - pages = {197--251}, - author = {Roger LeB. Hooke}, - title = {Water flow in and under glaciers: geomorphic implications}, - booktitle = {Principles of Glacier Mechanics} - } - -Requirements are the `requests` Python library which can be installed with `pip -install requests`. - -If you use Vim as your text editor, you can directly insert the output into the -current document with the following binding: - nmap <leader>r :read !scholarref.py - -Written by Anders Damsgaard, derived from doi2bib.py by Andrew Ning. -""" - -import requests -import sys -import re - -verbose = False - -query = ' '.join(sys.argv[1:]) -nhits = 1 - -# Use crossref metadata search (beta) to get the DOI -params = {'q': query, 'rows': str(nhits)} -doi_search_url = 'http://search.crossref.org/dois' -r = requests.get(doi_search_url, params=params) - -dois = [] -try: - for j in r.json(): - dois.append(j['doi'].split('dx.doi.org/')[1]) -except Exception as e: - print('Error: ' + str(e)) - print('Maybe DOI service at ' + doi_search_url + ' is unavailable?') - print('Server response: ' + str(r)) - sys.exit(1) - -# get the DOI of the first result -doi = dois[0] -if verbose: - print('Received DOI from ' + doi_search_url) - -# fix escaped chars -doi = doi.replace('\\', '') - - -# use REST API (see http://crosscite.org/cn/) -headers = {'Accept': 'application/x-bibtex'} -r = requests.post('http://dx.doi.org/' + doi, headers=headers) -if verbose: - print('Received bibtex entry from dx.doi.org') - -# extract bibtex -r.encoding = 'utf-8' -bibtex = r.text -bibtex = bibtex.replace('&amp;', '&') -bibtex = bibtex.replace('\t', ' ') -bibtex = bibtex.replace('_', '', 1) # strip _ from cite key -bibtex = re.sub('.*url.*\n', '', bibtex) # remove url field -bibtex = re.sub('.*month.*\n', '', bibtex) # remove month field - -# appreviate journal names -bibtex = bibtex.replace('Journal', 'J.') -#bibtex = bibtex.replace('of ', '') -#bibtex = bibtex.replace('and ', '') -#bibtex = bibtex.replace('the ', '') -bibtex = bibtex.replace('Geophysical', 'Geophys.') -bibtex = bibtex.replace('Geophysics', 'Geophys.') -bibtex = bibtex.replace('Research', 'Res.') -bibtex = bibtex.replace('Letters', 'Lett.') -bibtex = bibtex.replace('Mechanics', 'Mech.') -bibtex = bibtex.replace('Glaciology', 'Glaciol.') -bibtex = bibtex.replace('Proceedings', 'Proc.') -bibtex = bibtex.replace('Royal', 'R.') -bibtex = bibtex.replace('Society', 'Soc.') -bibtex = bibtex.replace('Annals', 'Ann.') -bibtex = bibtex.replace('Resources', 'Resour.') -bibtex = bibtex.replace('Surface', 'Surf.') -bibtex = bibtex.replace('Processes', 'Proc.') -bibtex = bibtex.replace('National', 'Nat.') -bibtex = bibtex.replace('Computers', 'Comput.') -bibtex = bibtex.replace('Geotechnics', 'Geotech.') -bibtex = bibtex.replace('Academy', 'Acad.') -bibtex = bibtex.replace('Sciences', 'Sci.') -bibtex = bibtex.replace('Review', 'Rev.') -bibtex = bibtex.replace('Quaternary', 'Quat.') -bibtex = bibtex.replace('Physical', 'Phys.') -bibtex = bibtex.replace('Planetary', 'Planet.') -bibtex = bibtex.replace('Quarterly', 'Q.') -bibtex = bibtex.replace('Geological', 'Geol.') -bibtex = bibtex.replace('Applied', 'Appl.') -bibtex = bibtex.replace('Physics', 'Phys.') -bibtex = bibtex.replace('Communications', 'Commun.') -bibtex = bibtex.replace('Geoscience', 'Geosci.') -bibtex = bibtex.replace('Landforms', 'Land.') - -bibtex = bibtex.strip() - -if bibtex.find('<!DOCTYPE html>') != -1: - sys.stderr.write('Entry not found\n') -else: - print(bibtex.encode('utf-8')) diff --git a/bin/shdl b/bin/shdl @@ -1,21 +1,138 @@ #!/usr/bin/env bash -set -e -read -p "use torsocks? [Y/n] " -n 1 -r -echo -if [[ $REPLY =~ ^[Nn]$ ]]; then - prefix="" -else - prefix="torsocks" -fi +version=1.0 +shhost="http://sci-hub.tw" -shdl_fetch() { - url="$($prefix curl --connect-timeout 30 --max-time 60 --silent \ - "http://sci-hub.tw/$*" | \ +function show_help { + echo "usage: ${0##*/} [OPTIONS] DOI1 [DOI2...[DOI N]]" + echo "will fetch each DOI from $shhost" + echo "If no DOI is specified, this program will expect DOIs as stdin." + echo "The program extracts the DOI if a full doi.org URL is passed to it." + echo "If the PDF download fails, e.g. due to captias, a tor-browser or " + echo "alternatively the system default browser will open sci-hub." + 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 requests to sci-hub" + echo " -b, --browser open sci-hub in browser immediately" + echo " -r, --reference add reference to bibliography using scholarref.py" + 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 strip_doi { + sed 's/^(http:\/\/|https:\/\/)?(dx\.)?(doi\.org\/)//' +} + +function browser_open { + if command -v tor-browser 2>/dev/null; then + [ "$verbose" = 1 ] && echo "attempting to launch tor-browser" + tor-browser "$1" + elif command -v xdg-open 2>/dev/null; then + [ "$verbose" = 1 ] && echo "launching default browser" + xdg-open "$1" + elif command -v open 2>/dev/null; then + [ "$verbose" = 1 ] && echo "launching default browser" + open "$1" + else + die 'Error: could not open a browser' + fi +} + +function shdl_fetch { + doi=$(echo "$1" | strip_doi) + shurl="$shhost/$doi" + [ "$verbose" = 1 ] && echo "processing $doi" + [ "$verbose" = 1 ] && echo "connecting to $shurl $prefix" + pdfurl="$($prefix curl --connect-timeout 30 --silent "$shurl" | \ grep location.href | \ grep -o '//.*pdf' | sed 's/^/http:/')" - echo "$url" - $prefix curl --connect-timeout 30 --max-time 60 -O "$url" + [ "$verbose" = 1 ] && echo "fetching pdf from $pdfurl" + if ! $prefix curl --connect-timeout 30 -O "$pdfurl"; then + if [ "$verbose" = 1 ]; then + (echo "Error: could not fetch $doi PDF from $pdfurl" >&2) + fi + browser_open "$shurl" + fi +} + +function handle_doi { + if [ "$browser" = 1 ]; then + browser_open "$1" + else + shdl_fetch "$1" + fi + if [ "$reference" = 1 ]; then + if command -v scholarref.py 2>/dev/null; then + if [ "$verbose" = 1 ]; then + echo "adding $1 to bibliography using scholarref.py" + fi + scholarref.py "$1" + else + die 'Error: scholarref.py not found in PATH' + fi + fi +} + +function die { + printf '%s\n' "$1" >&2 + exit 1 } -shdl_fetch "$@" +returnstatus=0 +prefix="" +verbose=0 +browser=0 +reference=0 +while :; do + case "$1" in + -h|-\?|--help) + show_help + exit 0 + ;; + -v|--version) + show_version + exit 0 + ;; + -V|--verbose) + verbose=1 + ;; + -b|--browser) + browser=1 + ;; + -t|--tor-socks) + prefix="torsocks" + ;; + -r|--reference) + reference=1 + ;; + --) # end all options + shift + break + ;; + -?*) + die 'Error: Unknown option specified' + ;; + *) # No more options + break + esac + shift +done + +if [ $# -lt 1 ]; then + doi="$(cat)" + handle_doi "$doi" +else + for doi in "$@"; do + handle_doi "$doi" + done +fi +exit "$returnstatus"