Skip to content

Instantly share code, notes, and snippets.

@Colelyman
Created December 6, 2019 21:55
Show Gist options
  • Save Colelyman/49a70793895ec81027e6d0aff46a04e1 to your computer and use it in GitHub Desktop.
Save Colelyman/49a70793895ec81027e6d0aff46a04e1 to your computer and use it in GitHub Desktop.
Generate a citation in Markdown from a BibTeX entry.
#!/bin/sh
TEMP_BIB_PATH=".mybib.bib"
TEMP_MD_PATH=".mybib.md"
TEMP_CITATION_PATH=".mycitation.md"
clean() {
rm -rf $TEMP_BIB_PATH $TEMP_MD_PATH $TEMP_CITATION_PATH
}
clean
while read line
do
echo "$line" >> $TEMP_BIB_PATH
done < "${1:-/dev/stdin}"
cat > $TEMP_MD_PATH <<- EOF
---
title: Citations
bibliography: $TEMP_BIB_PATH
nocite: '@*'
...
EOF
pandoc --filter=pandoc-citeproc -t markdown-citations --standalone $TEMP_MD_PATH | tail -n +9 > $TEMP_CITATION_PATH
cat $TEMP_CITATION_PATH | head -$(($(wc -l < $TEMP_CITATION_PATH) - 2)) $TEMP_CITATION_PATH
clean
@Colelyman
Copy link
Author

This depends on pandoc being installed.

The BibTeX entry can be input from stdin or a file can be provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment