Created
August 30, 2011 20:58
-
-
Save dsanson/1182008 to your computer and use it in GitHub Desktop.
bibtex2markdown: a script for generating markdown formatted bibliographies from bibtex files using pandoc and citeproc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# This script is a wrapper around pandoc that uses pandoc's | |
# builtin citeproc support to generate a markdown bibliography | |
# from bibtex. | |
# | |
# Inspired by Jacob Barney's [bib2mkd][] script. | |
# | |
# [bib2mkd]: http://jmbarney.dyndns.org/?/linux/bib2mkd/ | |
# | |
# Simplest usage: | |
# | |
# cat file.bib | bibtex2markdown | |
# | |
# will send to STDOUT a markdown bibliography containing all the | |
# publications from file.bib on STDOUT. | |
# | |
# Filtering: | |
# | |
# cat file.bib | bibtex2markdown smith jones | |
# | |
# will send to STDOUT a markdown bibliography containing all the | |
# publications from file.bib whose citekeys begin with | |
# 'smith' or 'jones'. | |
# | |
# Filtering with Regexs: | |
# | |
# cat file.bib | bibtex2markdown ".*nes" | |
# | |
# will send to STDOUT a markdown bibliography containing all the | |
# publications from file.bib whose citekeys begin with something | |
# that matches the regex ".*nes", e.g., 'jones1992' and | |
# 'ernest1878'. | |
# | |
# Using a default bibtex file: | |
# | |
# bibtex2markdown -d | |
# | |
# If the first argument is '-d', then will use the file given by | |
# the variable defaultbib, defined below, rather than reading | |
# STDIN. | |
defaultbib="/Users/david/.pandoc/default.bib" | |
input = "" | |
bibfile = "" | |
if ARGV[0] == "-d" | |
bibfile = defaultbib | |
File.open(defaultbib, 'r') { |bibtex| | |
input = bibtex.read | |
} | |
ARGV.shift | |
else | |
input = STDIN.read | |
tmpbibfile = "/tmp/bibtex2markdown" + Process.pid.to_s + ".bib" | |
bibfile = tmpbibfile | |
File.open(tmpbibfile, 'w') { |bibtex| | |
bibtex.puts "#{input}" | |
} | |
end | |
strings=ARGV | |
if strings.length == 0 | |
strings = [ "" ] | |
end | |
keys = [] | |
strings.each { |match| | |
match.chomp | |
keys = keys + input.scan(/@.*?\{\s*(#{match}.*?),/i) | |
} | |
keys.uniq! | |
keys.sort! | |
refs = "@" + keys.join(" @") | |
bibliography = `echo "#{refs}" | pandoc --bibliography="#{bibfile}" -t markdown --no-wrap 2> /dev/null` | |
puts bibliography.lines.to_a[2..-1].join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Redirecting error hides things like:
pandoc: Error running filter pandoc-citeproc
pandoc-citeproc not found in path
Which I needed to install to get the script to work.