Files

92 lines
2.9 KiB
Plaintext
Raw Permalink Normal View History

2021-03-30 00:37:43 -07:00
#!/bin/bash
if [ "$1" == "-h" ] ; then
echo "
2021-05-04 19:23:54 -07:00
sbib - search for citekey in a bibtex.bib or bibjson.json file, preview pdf, or return a bibliography entry in markdown or other format
2021-03-30 00:37:43 -07:00
usage:
sbib
# use 'a' flag to search all
sbib a
2021-05-04 19:23:54 -07:00
# use 'j' flag for bibjson quick search and preview
sbib j
2021-03-30 00:37:43 -07:00
# pass a custom citation style and database
sbib a citeprocStyle.csl bibdFile.json
2023-05-15 10:49:24 -04:00
Then press <enter> to open pdf
and press <ctrl-c> to copy citation to clipboard
note:
# to convert a bib to json, run
# pandoc bibdFile.bib -t csljson -o bibdFile.json
2021-03-30 00:37:43 -07:00
depends:
fzf
git grep
2021-04-08 20:52:17 -07:00
sed tail
bat or less
2023-05-15 10:49:24 -04:00
fetch_citation, pandoc, pandoc-citeproc
2021-05-04 19:23:54 -07:00
zathura (or other fast pdf viewer)
2021-04-08 20:52:17 -07:00
echo
2021-03-30 00:37:43 -07:00
wl-copy
2021-09-18 15:46:10 -07:00
jq
2021-03-30 00:37:43 -07:00
defaults:
Set the required default file locations (csl file, bib file)
"
exit 0
fi
#Setup defaults
2023-05-15 10:49:24 -04:00
cslFile=${2:-$HOME/projects/learn/bibd/bibd-md.csl}
bibdFile=${3:-$HOME/projects/learn/bibd/OMEGA.bib}
# bibdFile=${2:-$HOME/projects/learn/bibd/OMEGA.json}
2021-03-30 00:37:43 -07:00
cd $(dirname $bibdFile)
set -e #exit if an error
2021-05-04 19:23:54 -07:00
view_bib() {
2021-05-29 08:19:30 -04:00
# Use fzf to search citation
# Initial scroll offset is set to the line number of each line
# of git grep output *minus* 5 lines (-5)
# str=$(cat $fn | fzf)
2021-03-30 00:37:43 -07:00
2021-05-29 08:19:30 -04:00
# str=$(git grep -E --line-number $sPattern $bibdFile | fzf --delimiter : --preview 'nl {1} --body-numbering=a' --preview-window=:up:70%:+{2}-5)
# str=$(git grep -E --line-number $sPattern $bibdFile | fzf --delimiter : --preview 'less {1}' --preview-window=:up:70%:+{2}-5)
str=$(git grep -E --line-number $sPattern $bibdFile | fzf --delimiter : --preview 'bat --color=always --style=numbers --line-range=:500 {1}' --preview-window=:up:70%:+{2}-5)
2021-03-30 00:37:43 -07:00
2021-05-29 08:19:30 -04:00
# extract citation key from the fzf string
citeKey=$(echo $str | sed -E "s|$(basename $bibdFile):[0-9]+:@[a-zA-Z]+\{(.+),|\1|")
2023-05-15 10:49:24 -04:00
fetch_citation citeKey
2021-05-04 19:23:54 -07:00
}
view_json() {
2021-05-29 08:19:30 -04:00
# export citeKey=$1
# doiStr=$(jq -r '.[] | select(.id==env.citeKey).DOI' $bibdFile)
# urlStr=$(jq -r '.[] | select(.id==env.citeKey).URL' $bibdFile)
2021-05-04 19:23:54 -07:00
2021-05-29 08:19:30 -04:00
#actually this is the good one, opens pdfs quickly
2023-05-15 10:49:24 -04:00
jq -r '.[] | [.id, .title, .abstract, .keyword, .DOI, .PMID, .author[]?.family, .issued[]?[0]?[0], .["container-title"], .URL] | join(" ")' $bibdFile | fzf --preview 'echo {}' --preview-window=:up:70%:wrap \
--bind "enter:execute-silent(zathura {-1} &)" \
--bind "ctrl-c:execute-silent(fetch_citation {1})"
2021-05-04 19:23:54 -07:00
2023-05-15 10:49:24 -04:00
# --bind "ctrl-c:execute(notify-send {1})"
}
2021-05-04 19:23:54 -07:00
#set pattern to the null character '\0' for search all, else search only cite keys
if [ "$1" == "a" ]; then
sPattern='\0'
else
sPattern="@[a-zA-Z_-]+\{"
fi
if [ "$1" == "j" ]; then
2023-05-15 10:49:24 -04:00
bibdFile=${3:-$HOME/projects/learn/bibd/OMEGA.json}
2021-05-04 19:23:54 -07:00
view_json
else
view_bib
fi