Files
linux-bin/sbib

73 lines
2.5 KiB
Plaintext
Raw Normal View History

2021-03-30 00:37:43 -07:00
#!/bin/bash
if [ "$1" == "-h" ] ; then
echo "
sbib - search for citekey in a bibtex.bib (or maybe eventually bibjson.json) file and return a bibliography entry in markdown or possibly other format
usage:
sbib
# use 'a' flag to search all
sbib a
# pass a custom citation style and database
sbib a citeprocStyle.csl bibdFile.json
depends:
fzf
git grep
2021-04-08 20:52:17 -07:00
sed tail
bat or less
2021-03-30 00:37:43 -07:00
pandoc
pandoc-citeproc
2021-04-08 20:52:17 -07:00
echo
2021-03-30 00:37:43 -07:00
wl-copy
defaults:
Set the required default file locations (csl file, bib file)
"
exit 0
fi
#Setup defaults
cslFile=${2:-$HOME/projects/bibd/bibd-md.csl}
bibdFile=${3:-$HOME/projects/bibd/OMEGA.bib}
cd $(dirname $bibdFile)
set -e #exit if an error
#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
# 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)
# str=$(git grep -E --line-number $sPattern $bibdFile | fzf --delimiter : --preview 'nl {1} --body-numbering=a' --preview-window=:up:70%:+{2}-5)
2021-04-08 20:52:17 -07:00
# 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
# extract citation key from the fzf string
citeKey=$(echo $str | sed -E "s|$(basename $bibdFile):[0-9]+:@[a-zA-Z]+\{(.+),|\1|")
#method1
#todo: use json with jq
# outCitation=$(pandoc -f latex <(echo "\cite{$citeKey}") -t plain -o - --bibliography $bibdFile --citeproc --csl $cslFile | tail -n +3)
#method2
#need to use a temporary bib file as input to pandoc because parsing a large bib file is too slow, but json input would be faster
#and because pandoc expects a file input for bibliography
tmpName=$(mktemp --suffix=.bib)
str3=$(grep -A 30 $citeKey $bibdFile)
echo $str3 | sed -E "s|(.+\} ?\}).+|\1|" > $tmpName
outCitation=$(pandoc -f latex <(echo "\cite{$citeKey}") -t plain -o - --bibliography $tmpName --citeproc --csl $cslFile | tail -n +3)
rm $tmpName
#tail -n +61 $fn | sed -E "s#(.+\}\})#\1#"
# fzf --preview="head {}" --preview-window=up:30%
# fzf --preview="file {}" --preview-window=down:1
echo $outCitation
echo $outCitation | wl-copy