Skip to content

Instantly share code, notes, and snippets.

@jeromyanglim
Created June 10, 2012 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeromyanglim/2905065 to your computer and use it in GitHub Desktop.
Save jeromyanglim/2905065 to your computer and use it in GitHub Desktop.
script for extracting apa; I might write a post at some point about how it all works. it's mainly a personal script; it's very raw; use at own risk
#!/bin/bash
bibtexstylefile=apalike
bibtexdatabase=$BIB
pdfdirectory=$BIBDIR
pdfprogram=evince
pandocoutputformat=plain
# returntask
# bibtex : return bibtex record (default)
# format : return
# key : return citation keys
# pdf : open pdfs
returntask=bibtex
# summary files
extractedbibtexfile=$(tempfile).bib
extractedkeyfile=$(tempfile)
extractedformatfile=$(tempfile)
extractedpdfnamesfile=$(tempfile)
help()
{
cat <<END
Usage: bibmash [options] search-terms
-h, --help get this help
-p, --pdf open pdfs
-f, --format <format> format reference output using pandoc
e.g., plain, markdown, html, latex
-t, format references using pandoc in plain text
same as -f plain
-k, --keys return bibtex keys
-e <search-term> search terms
-s, --select select one pdf to open, citation to return, or bibtex to
return from list
-b <bibtex-database> specificy bibtex database
defaults to script configured setting
END
exit
}
# OPTS=$(getopt -n bibmash -o f:k:e:b:hpt --long help,format,keys,select "$@")
# if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# eval set -- "$OPTS"
for i in "$@"
do
case "$i" in
-p|--pdf) returntask=pdf; shift ;;
-e) searchterm="$2"; shift 2 ;;
-f|--format) pandocoutputformat="$2"; returntask=format; shift 2 ;;
-k|--keys) returntask=key; shift ;;
-t) returntask=format; pandocoutputformat=plain; shift ;;
-h|--help) help ;;
esac
done
# cat $extractedbibtexfile
# extract subset of bibtex
bibtool '--select{"'"$searchterm"'"}' $bibtexdatabase > $extractedbibtexfile
bibtool $extractedbibtexfile | grep '^@' | awk '{print $2}' | \
sed 's/,//' | tr '\n' ' ' > $extractedkeyfile
bibtex2html -nokeys -o - -s $bibtexstylefile -nodoc -q $extractedbibtexfile | \
sed '/file was generated/,$d; 1,9d' | \
pandoc -r html -w $pandocoutputformat > $extractedformatfile
# Format extracted bibtex using bibtex style with html markup
# Use sed to remove supplementary html output provided by bibtex2html
if [ $returntask = bibtex ]; then
cat $extractedbibtexfile
exit
fi
if [ $returntask = format ]; then
cat $extractedformatfile
exit
fi
if [ $returntask = pdf ]; then
for i in $(cat $extractedkeyfile)
do
find $pdfdirectory -iname $i.pdf >> $extractedpdfnamesfile
done
# if only one result is returned, then open it
#if [ $(echo results | wc -w) -eq 1 ]
#then
#$pdfprogram $results
#exit
#fi
# open selected matches in browser
select i in $(cat $extractedpdfnamesfile | tr '\n' ' ' )
do
$pdfprogram $i 2> /dev/null &
done
exit
fi
if [ $returntask = key ]; then
cat $extractedkeyfile
exit
fi
# Creating a local bibtex file based on aux file
# bibtool -x document.aux -o local.bib
# Extracting a single bibtex reference based on a part or all of a bibtex key
# bibtool -X smith $BIB
# ideas for converting article output to plain text or markdown format
# bibmash warning|pandoc -f html -w plain
# bibmash warning|pandoc -f html -w markdown
# TODO
# * Extract just keys
# * Extract keys in \cite command
# * Open pdf
# modify output formatting: remove pdf note and abstract
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment