Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gdevenyi
Forked from dsanson/bibtex2markdown.rb
Last active August 29, 2015 14:07
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 gdevenyi/0f20543b34cecdceb884 to your computer and use it in GitHub Desktop.
Save gdevenyi/0f20543b34cecdceb884 to your computer and use it in GitHub Desktop.
#!/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